Android / iOS - Custom URI / Protocol Handling

后端 未结 3 1445
名媛妹妹
名媛妹妹 2020-11-30 17:47

Is there a way to define some kind of handling mechanism in Android and iOS that would allow me to do intercept either of the following:

mya         


        
3条回答
  •  甜味超标
    2020-11-30 18:05

    EDIT 5/2014, as this seems to be a popular question I've added much detail to the answer:

    Android:

    For Android, refer to Intent Filter to Launch My Activity when custom URI is clicked.

    You use an intent-filter:

    
       
       
       
       
    
    

    this is attached to the Activity that you want launched. For example:

    
      
          
          
      
      
          
          
           
          
      
    
    

    Then, in your activity, if not running, the activity will be launched with the URI passed in the Intent.

    Intent intent = getIntent();
    Uri openUri = intent.getData();
    

    If already running, onNewIntent() will be called in your activity, again with the URI in the intent.

    Lastly, if you instead want to handle the custom protocol in UIWebView's hosted within your native app, you can use:

    myWebView.setWebViewClient(new WebViewClient()
    {
     public Boolean shouldOverrideUrlLoading(WebView view, String url)
     {
      // inspect the url for your protocol
     }
    });
    

    iOS:

    For iOS, refer to Lauching App with URL (via UIApplicationDelegate's handleOpenURL) working under iOS 4, but not under iOS 3.2.

    Define your URL scheme via Info.plist keys similar to:

    CFBundleURLTypes
        
            
                CFBundleURLName
                com.yourcompany.myapp
            
            
                CFBundleURLSchemes
                
                    myapp
                
            
        
    

    Then define a handler function to get called in your app delegate

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
     // parse and validate the URL
    }
    

    If you want to handle the custom protocol in UIWebViews hosted within your native app, you can use the UIWebViewDelegate method:

    - (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"]) 
        {
          ...
        }
      }
    }
    

    }

    For WKWebView (iOS8+), you can instead use a WKNavigationDelegate and this method:

    - (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);
    }
    

提交回复
热议问题