Cordova的自定义URL方案

Mat*_*ews 6 android ios cordova phonegap-build

我试图找到一个关于"如何为Cordova应用程序定义自定义URL方案(在iOS和Android平台上)"的好文档.

我在网上花了好几个小时但找不到好的答案.我得到了一些相关但却没有帮助我的链接.

Mine是一款在iOS和Android平台上运行的Cordova应用程序.我需要在从电子邮件调用URL时启动我的应用程序(例如:Myapp://).

请告诉我应该使用我的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>
Run Code Online (Sandbox Code Playgroud)

网址

<a href="com.test.simple://">Launch The App</a>
Run Code Online (Sandbox Code Playgroud)

Mik*_*lor 5

我知道您特别要求有关如何自己手动编码的文档,但仅供参考,有一个很好的插件可以为您完成所有(相当多的!)工作:

https://github.com/EddyVerbruggen/Custom-URL-scheme
Run Code Online (Sandbox Code Playgroud)

安装时,您只需提供要用于启动应用程序的 URL 方案:

$ cordova plugin add https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git --variable URL_SCHEME=myCustomUrlScheme
Run Code Online (Sandbox Code Playgroud)

这几乎就是全部内容了。适用于 Android 和 iOS。

  • 我按照指定使用这个插件,它在 Android 上没有任何作用,在 iOS 上工作。 (2认同)

Moh*_*n N 0

安卓 :

在清单中:

      <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>
Run Code Online (Sandbox Code Playgroud)

android:launchMode="singleTask"

尝试使用singleTask和singleTop并找出区别。两者都有不同的功能。

对于 iOS :

在此输入图像描述

因此,在您的应用程序页面中创建一个名为“重定向到我的应用程序”的按钮,其 href 为“yourappscheme://”。

<a href="yourappscheme://">Open my app</a>
Run Code Online (Sandbox Code Playgroud)

还有Scheme、host等其他部分。

考虑如果你想从 url 获取参数,你必须使用 Native 代码。

假设您的网址类似于: 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
    }
}
Run Code Online (Sandbox Code Playgroud)

对于 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;
}
Run Code Online (Sandbox Code Playgroud)

希望这一点是清楚的。