html5 唤起app

html5 测试代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<h1>Test Scheme</h1>
<!--自动加载隐藏页面跳转-->
<a id="100" class="arouseApp" href="myscheme://http://196.168.2.1?key=mykey"><span>启动app</span></a>
<script>
    var arouseApp = document.getElementsByClassName("arouseApp");
    alert(arouseApp["0"].href);
    arouseApp["0"].href =  "myscheme://" + location.href;
    alert(arouseApp["0"].href);
</script>
</body>
</html>


//android配置
AndroidManifest.xml
<activity
        <!--html切换到app时唤起的activity-->
        android:name=".MainActivity"  
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        android:exported="true">   //是否允许被其他Application的组件启动
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!--需要添加下面的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:scheme="myscheme"/>
        </intent-filter>
</activity>
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent =getIntent();
        Log.e(TAG, "scheme:" +intent.getScheme());
        Uri uri =intent.getData();
        Log.e(TAG, "scheme: "+uri.getScheme());
        Log.e(TAG, "host: "+uri.getHost());
        Log.e(TAG, "port: "+uri.getPort());
        Log.e(TAG, "path: "+uri.getPath());
        Log.e(TAG, "queryString: "+uri.getQuery());
        Log.e(TAG, "queryParameter: "+uri.getQueryParameter("key"));
    }
}

//ios
配置scheme info — urlTypes
urlTypes
— 点击加号 添加一个URL Schemes
Schemes

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    NSLog(@"application ---  openURL  scheme  = %@" ,[url scheme]);
    NSLog(@"application ---  openURL  query  = %@" ,[url query]);
    NSString * scheme1 = [url scheme];
    NSString * scheme2 = [NSString stringWithFormat:@"myscheme"]; //填写在urlTyoes里添加的scheme

    if([scheme1 isEqualToString:scheme2])//这两个相等的时候证明唤起的app是我们自定义的    
    {
     NSString *itemId = [[url query] substringFromIndex:[[url query]  rangeOfString:@"roomId"].location];
     //itemId 就是从页面上跳转过来的参数
    }
### H5页面唤起App的实现方式 #### 使用JavaScript检测环境并调用App Scheme URL 为了实现在H5页面上唤起App的功能,可以通过判断用户的设备和浏览器环境来决定是否尝试唤起App。以下是具体的实现方法: 1. **检测当前是否处于微信环境中** 需要编写一个`isWechatBrowser`函数用于检测用户是否正在使用微信内置浏览器访问网页[^1]。 2. **定义AppScheme URL** App开发者通常会提供一个特定的URL scheme(例如`myapp://`),该scheme可以被用来直接启动已安装的应用程序。 3. **创建隐藏的iframe元素触发唤起行为** 当确认是在微信环境下时,可以在HTML文档中动态插入一个不可见的iframe标签,并将其src属性设置为目标应用的scheme地址。这样做的目的是模拟点击动作从而激活目标应用程序。 4. **设定超时时限处理失败场景** 如果在一定时间内(比如两秒钟)没有成功打开指定的应用,则认为此次唤醒请求失败。此时可以选择引导用户前往下载页面或者采取其他补救措施。 ```javascript function isWechatBrowser() { var ua = navigator.userAgent.toLowerCase(); return /micromessenger/.test(ua); } function openApp(appUrl, fallbackUrl) { if (isWechatBrowser()) { // 判断是否为微信浏览器 const iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = appUrl; document.body.appendChild(iframe); setTimeout(() => { window.location.href = fallbackUrl; // 超过时间未跳转则重定向至备用链接 }, 2000); // 设置超时时间为2秒 } else { alert("请在微信客户端内打开此页面"); } } ``` 对于iOS平台来说,除了上述方案外还可以考虑利用中间页的方式来进行更复杂的逻辑控制[^2]。即先导航到某个临时页面,在这个页面里再完成实际的跳转过程,这种方法能够提供更多灵活性以及错误恢复机制的支持。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值