最近,我们将应用程序升级到Cordova 6.0.0并更新了iOS和Android平台(分别为4.1.0和5.1.1版本)。在升级之后,我们必须处理链接的旧代码停止工作。
我们希望找到一种方法:
我们不想使用inappbrowser插件(我们实际尝试了它但它只提供了部分解决方案,因此我们删除了它)。在这个问题上花了一些时间之后我们找到了这样做的方法,所以我们在这里发布答案,希望它能帮助任何其他开发人员,他们只想在适当的应用程序中打开外部链接。
答案 0 :(得分:1)
以下将演示我们如何在iOS和Android中完成此操作。您可以将SomethingCom替换为您希望在外部打开的任何其他域。希望这些例子能够自我解释: - )
<强>的iOS 强>
这样做的地方是 shouldStartLoadWithRequest 功能。正如我们所发现的,这个函数的位置在各种Cordova版本中都发生了变化,所以找到它的最简单方法是使用xcode&#34; find&#34;并寻找 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
<强>的Android 强>
我们添加代码的地方在我们的app Java类中(在android&gt; Java&gt; com&gt;我们的类包下)。
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);
}}