![](/img/trans.png)
[英]cordova-plugin-inappbrowser not opening links from APP to IOS Device default Browser
[英]Opening links from Cordova app in external app - facebook, twitter or browser - both for iOS and Android
最近我們將我們的應用程序升級到 Cordova 6.0.0 並更新了我們的 iOS 和 Android 平台(分別到版本 4.1.0 和 5.1.1)。 在這次升級之后,我們必須處理鏈接的舊代碼停止工作。
我們想找到一種方法:
我們不想使用 inappbrowser 插件(我們實際上嘗試過,但它只給出了部分解決方案,因此我們將其刪除)。 在此問題上花了一些時間后,我們找到了解決方法,因此我們將在此處發布答案,希望它可以幫助任何其他只想在適當的應用程序中從外部打開鏈接的開發人員。
下面將演示我們如何在 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);
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.