【问题标题】:Opening links from Cordova app in external app - facebook, twitter or browser - both for iOS and Android在外部应用程序中打开 Cordova 应用程序的链接 - facebook、twitter 或浏览器 - 适用于 iOS 和 Android
【发布时间】:2016-07-05 04:01:38
【问题描述】:

最近我们将应用升级到 Cordova 6.0.0 并更新了我们的 iOS 和 Android 平台(分别升级到版本 4.1.0 和 5.1.1)。此次升级后,我们必须处理链接的旧代码停止工作。

我们想找到一种方法:

  • 我们的应用程序中到 facebook 的链接将在 facebook 应用程序(如果已安装)中打开
  • 我们的应用程序中指向 twitter 的链接将在 twitter 应用程序中打开(如果已安装)
  • 其他链接将在外部浏览器中打开,而不是在我们的应用程序 (safari/chrome) 中打开

我们不想使用 inappbrowser 插件(我们实际上尝试过,但它只提供了部分解决方案,因此我们将其删除)。在这个问题上花了一些时间后,我们找到了解决方法,因此我们将在此处发布答案,希望它能帮助任何其他只想在适当的应用程序中从外部打开链接的开发人员。

【问题讨论】:

    标签: android ios facebook cordova twitter


    【解决方案1】:

    下面将演示我们如何在 iOS 和 Android 中做到这一点。您可以将 SomethingCom 替换为您希望在外部打开的任何其他域。希望这些例子能自我解释:-)

    iOS

    要做的地方在shouldStartLoadWithRequest函数中。正如我们所发现的,这个函数的位置在不同的 Cordova 版本中发生了变化,所以找到它的最简单方法是使用 xcode “find” 并查找 shouldStartLoadWithRequest。在当前版本中(问题中提到),它是 CDVUIWebViewDelegate.m 的一部分。

    (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
    
    //START HERE: ADD THE FOLLOWING CODE TO OPEN LINKS
    NSURL *url = [request URL];    
    
    NSRange isFacebook = [[url absoluteString] rangeOfString:@"facebook.com"
                                                     options:NSCaseInsensitiveSearch];
    
    NSRange isTwitter = [[url absoluteString] rangeOfString:@"twitter.com"
                                                    options:NSCaseInsensitiveSearch];
    
    NSRange isSomethingCom = [[url absoluteString] rangeOfString:@"something.com"
                                                   options:NSCaseInsensitiveSearch];
    
    if(isFacebook.location != NSNotFound)
    {
        NSURL *fbAppurl = [NSURL URLWithString:@"fb://profile/YOUR_PAGE_ID"];//Notice you need to replace YOUR_PAGE_ID with the ID number of your page
    
        if ([[UIApplication sharedApplication] canOpenURL:fbAppurl]) {
            [[UIApplication sharedApplication] openURL:fbAppurl];
        } else {
            [[UIApplication sharedApplication] openURL:url];
        }
    
        return NO;
    }
    else if(isTwitter.location != NSNotFound)
    {
        NSURL *twitterAppurl = [NSURL URLWithString:@"twitter://user?id=YOUR_USER_ID"];//Notice you need to replace YOUR_USER_ID with the ID number of your user
    
        if ([[UIApplication sharedApplication] canOpenURL:twitterAppurl]) {
            [[UIApplication sharedApplication] openURL:twitterAppurl];
        } else {
            [[UIApplication sharedApplication] openURL:url];
        }
    
        return NO;
    }
    else if(isSomethingCom.location != NSNotFound)
    {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }    
    //END HERE 
    ...here comes the rest of this function which we left untouched
    

    安卓

    我们添加代码的地方在我们的应用Java类中(在android>Java>com>我们的类包下)。

    import android.content.Intent;
    import android.content.pm.ApplicationInfo;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.webkit.WebView;
    import org.apache.cordova.*;
    import org.apache.cordova.engine.*;
    public class MyClass extends CordovaActivity 
    {
    boolean isFacebookInstalled = false;
    boolean isGooglePlusInstalled = false;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    
        // Check if facebook app is installed
        try {
            ApplicationInfo info = getPackageManager().getApplicationInfo(
                    "com.facebook.katana", 0);
            isFacebookInstalled = true;
        } catch (PackageManager.NameNotFoundException e) {
            isFacebookInstalled = false;
        }
    
        // Check if Google Plus app is installed
        try {
            ApplicationInfo info = getPackageManager().getApplicationInfo(
                    "com.google.android.apps.plus", 0);
            isGooglePlusInstalled = true;
        } catch (PackageManager.NameNotFoundException e) {
            isGooglePlusInstalled = false;
        }
    
        LOG.e("MyLog", "isFacebookInstalled = " + isFacebookInstalled + " ; isGooglePlusInstalled = " + isGooglePlusInstalled);
    
        init();
        WebView myWebView = (WebView) this.appView.getView();
    
        myWebView.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine) this.appView.getEngine()) {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LOG.e("MyLog", "shouldOverrideUrlLoading = " + url);
    
                boolean isFacebook = (url.indexOf("facebook.com") != -1) ? true : false;
                boolean isGooglePlus = (url.indexOf("plus.google.com") != -1) ? true : false;
                boolean isGooglePlay = (url.indexOf("market://") != -1) ? true : false;
                boolean isSomethingCom = (url.indexOf("something.com") != -1) ? true : false;
    
                if (isFacebook) {
                    if (isFacebookInstalled) {
                        url = "fb://page/YOUR_PAGE_ID";//Notice you need to replace YOUR_PAGE_ID with the ID number of your page
                    }
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(browserIntent);
                    return true;
                } else if (isGooglePlus) {
                    if (isGooglePlusInstalled) {
                        url = "https://plus.google.com/+YOUR_PAGE_NAME/posts";//Notice you need to replace YOUR_PAGE_NAME with the name of your page
                    }
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(browserIntent);
                    return true;
                } else if (isGooglePlay || isSomethingCom) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(browserIntent);
                    return true;
                } else {
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }
        });
    
        loadUrl(launchUrl);
    }}
    

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 2014-09-02
      • 2016-01-09
      • 1970-01-01
      相关资源
      最近更新 更多