Android与iOS自定义URL Scheme唤醒app

1 篇文章 0 订阅

Android与iOS自定义URL Scheme唤醒app

需求

手机浏览器中的网页需要唤醒app的特定页面及向app页面传递参数。

实现方案

Android与iOS端均可通过配置自定义的URL Scheme来达到外部唤醒页面的目的。

Android

定义 URL Scheme

在AndroidManifest.xml中对需要唤醒的Activity配置<intent-filter>

  <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:host="article" android:scheme="jaren" />
      </intent-filter>

通过配置host与scheme就可以定位到此页面,唤醒协议jaren://article,需要传递参数可拼接query参数
jaren://article?tid=20

获取参数

在Activity的onCreate方法中获取网页传递的参数,如获取文章的id然后执行网络请求获取文章详情的数据。

    Intent intent = getIntent();
        if (Intent.ACTION_VIEW.equals(intent.getAction())){
            Uri uri=intent.getData();
            if (uri!=null){
                articleId=uri.getQueryParameter("tid");
            }
        }

iOS

定义 URL Scheme

iOS在info.plist文件URL types节点下添加需要自定义的URL Schemejaren

获取参数并跳转到相应页面

AppDelegate.m实现方法

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.scheme isEqualToString:@"jaren"]) {

        [self jumpPage:url];
        return true;
    }
    return false;
}

jumpPage方法则是匹配url字符串来定位特定页面,url.query是传递的参数字段。

网页调用协议

网页端调用定义的协议

    <div>
        <a href="jaren://article?aid=20">app打开文章</a>
    </div>

这样在浏览器中打开此网页,点击app打开文章就可以唤醒app的文章详情页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native 可以通过配置 URL Scheme 实现从外部应用程序打开应用程序并传递参数。下面是 iOSAndroid配置 URL Scheme 的步骤: ## iOS 1. 在 Xcode 中选择项目,进入 TARGETS -> Info -> URL Types,点击“+”按钮添加 URL Scheme。 2. 在 URL Scheme 输入框中填写自定义URL Scheme,例如:myapp。 3. 在 URL Schemes 中填写应用程序的 Bundle ID。 4. 在 Xcode 中保存并编译项目。 5. 在应用程序中,可以通过 Linking 模块的 getInitialURL 方法获取启动应用程序的 URL,并通过 parse 函数解析 URL 中的参数。 示例代码: ```javascript import { Linking } from 'react-native'; Linking.getInitialURL().then(url => { if (url) { const { queryParams } = Linking.parse(url); console.log(queryParams); } }); ``` ## Android 1. 在 AndroidManifest.xml 中添加 intent-filter。 ```xml <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" /> </intent-filter> ``` 2. 在 data 标签中填写自定义URL Scheme。 3. 在应用程序中,可以通过 Linking 模块的 getInitialURL 方法获取启动应用程序的 URL,并通过 parse 函数解析 URL 中的参数。 示例代码: ```javascript import { Linking } from 'react-native'; Linking.getInitialURL().then(url => { if (url) { const { queryParams } = Linking.parse(url); console.log(queryParams); } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值