[英]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.