EDIT 5/2014, vì điều này có vẻ là một câu hỏi phổ biến Tôi đã thêm nhiều chi tiết để câu trả lời:
Android:
Đối với Android, hãy tham khảo Intent Filter to Launch My Activity when custom URI is clicked.
Bạn sử dụng một ý định lọc:
<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>
này được gắn vào Hoạt động mà bạn muốn đưa ra. Ví dụ:
<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>
Sau đó, trong hoạt động của bạn, nếu không chạy, hoạt động sẽ được khởi chạy với URI được chuyển trong mục đích.
Intent intent = getIntent();
Uri openUri = intent.getData();
Nếu đã chạy, trênNewIntent() sẽ được gọi lại trong hoạt động của bạn, với URI theo ý định.
Cuối cùng, nếu bạn thay vì muốn xử lý giao thức tùy chỉnh trong UIWebView của tổ chức trong ứng dụng bản địa, bạn có thể sử dụng:
myWebView.setWebViewClient(new WebViewClient()
{
public Boolean shouldOverrideUrlLoading(WebView view, String url)
{
// inspect the url for your protocol
}
});
iOS:
Đối với iOS, hãy tham khảo Lauching App with URL (via UIApplicationDelegate's handleOpenURL) working under iOS 4, but not under iOS 3.2.
Xác định lược đồ URL của bạn thông qua Thông tin.phím plist tương tự như:
<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>
Sau đó xác định một hàm điều khiển để gọi trong ứng dụng của đại biểu
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// parse and validate the URL
}
Nếu bạn muốn xử lý giao thức tùy chỉnh trong UIWebViews lưu trữ trong ứng dụng bản địa, bạn có thể sử dụng phương pháp 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"])
{
...
}
}
}
}
Đối WKWebView (iOS8 +), bạn thay vì có thể sử dụng một WKNavigationDelegate và phương pháp này:
- (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);
}
Câu trả lời được chấp nhận thực sự nên giải quyết toàn bộ câu hỏi. Câu trả lời của bạn không đề cập đến Android. Câu hỏi có chữ in đậm "và". – ToothlessRebel
Câu hỏi thực sự cần phải được chia thành hai câu hỏi - không có cách nào để xử lý các URI tùy chỉnh trên cả hai nền tảng. –