Sean's Note: 2月 2016

2016年2月11日 星期四

什麼是 Outlets 和 Referencing Outlets

在 IB 裡的 Show the Connections inspector 頁面,可以看到 Outlets 和 Referencing Outlets,每次基本一定要設定的就是 UIViewController 與 UIView 與之間的連接。

對於 UIViewController 來說,Outlets 可以說是底下的 view 和為 IBOutlet 的那些 properties,所以從圖1 中可以看到 UIViewController 有兩個 Outlets 分別為 view 和 simpleTableView 對應著這個 nib 檔中別名為 View 和 Simple Table View 的元件。而 Referencing Outlets 則為自己被外部的哪些 Objects 所引用,以圖1來說,因為這個 UIViewController 放了一個 UITableView,而 UITableView 擁有 dataSource 和 delegate 的 properties,需要讓他們引用 UIViewController,這也是為什麼需要讓 UIViewController 實作 UITableViewDelegate 和 UITableViewDataSource 。

圖1. File's Owner Connections

另外從 UITableView 的角度來看,他的 Outlets 則是 dataSource 和 delegate,而 Referencing Outlets 則是 simpleTableView。

圖2. UITableView Connections

2016年2月1日 星期一

How to handle app links?

App Link 可以用來讓程式 A 把 程式 B 的某個 Activity 叫起來。
首先,需要在 AndroidManifest.xml 裡的 Activity 區段宣告以下:
<activity ...>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="myApp" />
        <data android:host="profilePage" />
    </intent-filter>
</activity>
一個 URI 是由以下所組成:
<scheme>://<host>:<port>[<path>|<pathPrefix>|<pathPattern>]

所以程式 A 就可以透過 URI myApp://profilePage 把程式 B 給叫起來:
// Application A start application B via the intent with URI. 
Intent i = new Intent();
i.setData(Uri.parse("myApp://profilePage"));
// Check whether the intent can be resolved.
if (i.resolveActivity(getPackageManager()) != null) {
  startActivity(i);
}

甚至 URI 後面也可以帶 query:
myApp://profilePage?uid=12&uname=Sean
再透過 Uri 的方法 getQueryParameterNames 和 getQueryParameter 來取得 key 和 value。


測試的方法

測試的方法有三種:
  1. 直接寫程式碼執行。
  2. 用 Android Studio 來執行
    Run -> Edit Configuration... -> Launch Options -> URL
    在 URL 的欄位輸入 URI 然後執行。
    用這個方法的缺點是只能測 action=android.intent.action.VIEW 而且 category=android.intent.category.BROWSER 的 activity。

  3. 透過 adb command line (推薦的方法)
    輸入 adb 指令:
    adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSER -d myApp://profilePage

Ref:
  1. <data>
    https://developer.android.com/guide/topics/manifest/data-element.html
  2. Support HTTP URLs in Your App
    https://firebase.google.com/docs/app-indexing/android/app?hl=zh-tw#reference-the-noindexxml-file
  3. Testing URLs with Android Studio
    https://firebase.google.com/docs/app-indexing/android/test#lint-checks-for-links