EDIT 5/2014、これはよくある質問のように思われるので、回答に詳細を追加しました:
アンドロイド:
Android の場合は、カスタム URI がクリックされたときにマイ アクティビティを起動するインテント フィルター を参照してください。
インテント フィルターを使用します。
<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" />
</intent-filter>
これは、起動するアクティビティに添付されます。例えば:
<activity android:name="com.MyCompany.MyApp.MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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" android:host="com.MyCompany.MyApp" />
</intent-filter>
</activity>
次に、アクティビティで、実行されていない場合、アクティビティはインテントで渡された URI で起動されます。
Intent intent = getIntent();
Uri openUri = intent.getData();
すでに実行されている場合は、アクティビティで onNewIntent() が呼び出され、インテント内の URI が再び使用されます。
最後に、代わりにネイティブ アプリ内でホストされている UIWebView のカスタム プロトコルを処理する場合は、次を使用できます。
myWebView.setWebViewClient(new WebViewClient()
{
public Boolean shouldOverrideUrlLoading(WebView view, String url)
{
// inspect the url for your protocol
}
});
iOS:
iOS については、iOS 4 では動作するが iOS 3.2 では動作しない (UIApplicationDelegate の handleOpenURL を介した) URL を使用したアプリの起動を参照してください。
次のような Info.plist キーを使用して URL スキームを定義します。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.yourcompany.myapp</string>
</dict>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
次に、アプリのデリゲートで呼び出されるハンドラー関数を定義します
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// parse and validate the URL
}
ネイティブ アプリ内でホストされている UIWebViewsでカスタム プロトコルを処理する場合は、UIWebViewDelegate メソッドを使用できます。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *urlPath = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
// inspect the [URL scheme], validate
if ([[urlPath scheme] hasPrefix:@"myapp"])
{
...
}
}
}
}
WKWebView ( iOS8 +) の場合、代わりに WKNavigationDelegate と次のメソッドを使用できます。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSURL *urlPath = navigationAction.request.URL;
if (navigationAction.navigationType == WKNavigationTypeLinkActivated)
{
// inspect the [URL scheme], validate
if ([[urlPath scheme] hasPrefix:@"myapp"])
{
// ... handle the request
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
//Pass back to the decision handler
decisionHandler(WKNavigationActionPolicyAllow);
}