如果你有自己寫的 Cordova project,需要在 cordova 裡開啟外部的App, 請參考下面的範例:
The following will demonstrate how we did it both in iOS and Android. You can replace SomethingCom with any other domain you’d like to be opened externally. Hopefully the examples will be self explained 🙂
iOS
The place to do it is in the shouldStartLoadWithRequest function. As we found, the location of this function has changed in various Cordova versions, so the simplest way to find it is using xcode “find” and look for shouldStartLoadWithRequest. In the current versions (mentioned in the question) it is part of 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
The place we added the code is within our app Java class (under android > Java > com> our class package).
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);
}}