[英]Custom URL scheme for Cordova
我試圖找到一份關於“如何為 Cordova 應用程序(在 iOS 和 Android 平台上)定義自定義 URL 方案”的好文檔。
我在 inte.net 上花了幾個小時,但找不到好的答案。 我得到了一些相關但對我幫助不大的鏈接。
我的是一個 Cordova 應用程序,它在 iOS 和 Android 平台上運行。 我需要在從電子郵件(例如:Myapp://)調用 URL 時啟動我的應用程序。
請告訴我應該對我的 Cordova 應用程序進行哪些配置更改才能啟用此功能。
編輯: Android 清單
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.simple.app" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="com.test.simple" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
Url
<a href="com.test.simple://">Launch The App</a>
我知道您特別要求提供有關如何自己手動編碼的文檔,但僅供參考,有一個不錯的插件可以為您完成所有(相當多的!)工作:
https://github.com/EddyVerbruggen/Custom-URL-scheme
安裝時,您只需提供要用於啟動應用程序的 URL 方案:
$ cordova plugin add https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git --variable URL_SCHEME=myCustomUrlScheme
這就是它的全部內容。 適用於 Android 和 iOS。
安卓 :
在清單中:
<activity
android:name=".MainActivity"
android:label="@string/activity_name"
android:launchMode="singleTask"
<intent-filter android:label="@string/launcher_name" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="yourappscheme"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
android:launchMode="singleTask"
嘗試使用 singleTask 和 singleTop 並找出不同之處。 兩者都有不同的功能。
對於 iOS:
因此,在您的應用程序頁面中創建一個名為“重定向到我的應用程序”的按鈕,其 href 為“yourappscheme://”。
<a href="yourappscheme://">Open my app</a>
還有其他部分,如 Scheme、host。
考慮如果您想從 url 獲取參數,則必須使用本機代碼。
假設您的網址類似於: yourappscheme://?username="xxx@gmail.com" Android:將此代碼放在 OnCreate 中。
Intent intent = getIntent();
Uri data = intent.getData();
if(data!=null) { //
//get schma
String scheme = data.getScheme(); //
Toast.makeText(getActivity(), scheme, Toast.LENGTH_LONG).show();
if(scheme.contains("yourappscheme")) {
//get parameter
String urltextboxname = data.getQueryParameter("username");
system.out.println(urltextboxname) // it will print xxx@gmail.com
}
}
對於 iOS:在您的 App Delegate 中:
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
if (!url) {
return NO;
}
// calls into javascript global function 'handleOpenURL'
NSString* jsString = [NSString stringWithFormat:@"handleOpenURL(\"%@\");", url];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];
// all plugins will get the notification, and their handlers will be called
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
NSString *myUrlString = [url absoluteString];
if ([myUrlString containsString:@"yourappscheme://"])
{
NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
NSLog(@"URL scheme:%@", [url scheme]);
NSLog(@"URL query: %@", [url query]);
}
return YES;
}
希望這很清楚。
我這樣做:
cordova 插件添加 cordova-plugin-customurlscheme --variable URL_SCHEME=mycoolapp
而不是 mycoolapp 寫你的應用程序名稱,但是當我粘貼這個時我只是在 cmd 上刪除 (app) 但我不確定它是否重要,所以寫在你的 (index.html) 文件夾 (www) 上。
<script type="text/javascript" src="js/plugins/LaunchMyApp.js"></script>
接下來,當您添加插件(customurlschema)時,它已添加到您項目的插件文件夾中,因此請轉到
cordova-plugin-customurlscheme->www->android->LaunchMyApp.js
並復制(LaunchMyApp.js),現在你看到了
<script type="text/javascript" src="js/plugins/LaunchMyApp.js"></script>
(src) 是 "js/plugins/LaunchMyApp.js" ,所以你在你的 (www) 文件夾上創建這個路徑並粘貼 (LaunchMyApp.js) 。現在構建你的應用程序。如果第一次你的應用程序不是t build 所以刪除android
cordova platform rm android
並將其再次添加到您的項目中,並構建兩次。可能它對您有用,因為我在項目上進行了測試,為了使用此插件,您應該在您的 html 頁面上放置如下標簽
<a href="mycoolapp://">Open my app</a>
例子:我在下面寫這個:
cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=mycoolenglish
所以我的(href)如下所示:
<a href="mycoolenglish://index.html">Open my app</a>
我測試它。
.我的英語不是很好,所以首先請原諒我。
自 2023 年起,PhoneGap 已被棄用,因此每次使用已接受答案建議的安裝腳本都會失敗。 沒有安裝 PhoneGap 的候選。
如果您嘗試在您的終端中運行它:
cordova plugin add https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git --variable URL_SCHEME=myCustomUrlScheme
這將返回此錯誤:
無法通過注冊表獲取插件https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git 。 這可能是連接問題,或者插件規范不正確。 檢查您的連接和插件名稱/版本/URL。 CordovaError:錯誤:在 $PATH 中找不到 git 二進制文件
要安裝自定義 url 方案插件,請運行以下命令:
sudo cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=myapp://redirect.html?redirect&return=true
然后在你的服務器上你應該有一個頁面:
重定向.html
<a href="myapp://redirect.html?redirect&return=true">Click here to open app</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.