如何在HTML网页中调起APP

如何在HTML网页中调起APP,第1张

对于app打开而言最常规的打开就是通过url scheme的方式去打开你的app,如下的

myapp://

myapp://open

myapp://type=1&id=2sdeo223lwe

这些抛出都是以url的方式进行抛出,app捕捉到这些抛出去做相应的处理,本文对app的处理不做详细描述,app开发请自行谷歌百度。对于前端而言抛出的方式也有很多,而最理想的方式是通过iframe的src对其进行链抛出,来!说的在多都没有代码来的清晰,请看下面。

//实际上就是新建一个iframe的生成器

var  createIframe=(function(){

  var iframe

    return function(){

        if(iframe){

            return iframe

        }else{

            iframe = document.createElement('iframe')

            iframe.style.display = 'none'

            document.body.appendChild(iframe)

            return iframe      

        }

    }

})()

之后我们还需要一个url scheme:

//生成一个url scheme,假设我们约定的scheme是myApp://type=1&id=iewo212j32这种形式的

var baseScheme = "myApp://"

var createScheme=function(options){

    var urlScheme=baseScheme

    for(var item in options){

        urlScheme=urlScheme+item + '=' + encodeURIComponent(options[item]) + "&"

    }

    urlScheme = urlScheme.substring(0, urlScheme.length - 1)

    return encodeURIComponent(urlScheme)

}

这种scheme形式的其实不是最好的,根据我们踩过的坑,觉得约定为与http协议相近可能更好一些,具体的协议需要前端人员自己去和app端人员约定。

ok万事具备,iframe有了,urlScheme也有了,该去打开app了

var openApp=function(){

    var localUrl=createScheme()

    var openIframe=createIframe()

    if(isIos()){

        //判断是否是ios,具体的判断函数自行百度

        window.location.href = localUrl

        var loadDateTime = Date.now()

        setTimeout(function () {

            var timeOutDateTime = Date.now()

            if (timeOutDateTime - loadDateTime < 1000) {

                window.location.href = "你的下载页面"

            }

        }, 25)

    }else if(isAndroid()){

        //判断是否是android,具体的判断函数自行百度

        if (isChrome()) {

            //chrome浏览器用iframe打不开得直接去打开,算一个坑

            window.location.href = localUrl

        } else {

            //抛出你的scheme

            openIframe.src = localUrl

        }

        setTimeout(function () {

            window.location.href = "你的下载页面"

        }, 500)

    }else{

        //主要是给winphone的用户准备的,实际都没测过,现在winphone不好找啊

        openIframe.src = localUrl

        setTimeout(function () {

            window.location.href = "你的下载页面"

        }, 500)

    }

}

以上就是你要打开scheme的主要代码了,好吧,实际上不只是打开app,还要实现未打开的时候跳到下载页去。其中安卓实际上无论有没有打开都会跳到下载页去,而ios........好吧!按照网上的说法是浏览器失焦后会挂起脚本,呵呵,这是多老的ios版本的表现了,实际上现在的ios已经没有这么做,有些版本会跟安卓的表现一样,而有些则是直接跳转根本不会去打开,还有打开的时候那个恶心的系统d窗是什么鬼。好吧,实际上至此你会发现,ios9.0以上的有些打不开直接跳,有些打得开还会有允许d窗,而微信则是无论如何都打不开,实际上微信会在他的浏览器里拦截掉所有未经其允许的scheme包括app store。

转自:《怎么在网页中打开你的app》@AlfredMou -- segmentfault

通过Html网页调用本地安卓app

一、通过html页面打开Android本地的app

1、首先在编写一个简单的html页面

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<a href="m://my.com/">打开app</a><br/>

</body>

</html>

2、在Android本地app的配置

在AndroidManifest的清单文件里的intent-filte中加入如下元素:

<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="my.com"

android:scheme="m" />

</intent-filter>

示例截图如下:

然后使用“手机浏览器”或者“webview”的方式打开这个本地的html网页,点击“打开APP”即可成功开启本地的指定的app

二、如何通过这个方法获取网页带过来的数据

只能打开就没什么意思了,最重要的是,我们要传递数据,那么怎么去传递数据呢?

我们可以使用上述的方法,把一些数据传给本地app,那么首先我们更改一下网页,代码修改后:

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<a href="m://my.com/?arg0=0&arg1=1">打开app</a><br/>

</body>

</html>

(1).假如你是通过浏览器打开这个网页的,那么获取数据的方式为:

Uri uri = getIntent().getData() String test1= uri.getQueryParameter("arg0") String test2= uri.getQueryParameter("arg1")

(2)如果使用webview访问该网页,获取数据的 *** 作为:

webView.setWebViewClient(new WebViewClient(){

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

Uri uri=Uri.parse(url)

if(uri.getScheme().equals("m")&&uri.getHost().equals("my.com")){

String arg0=uri.getQueryParameter("arg0")

String arg1=uri.getQueryParameter("arg1")

}else{

view.loadUrl(url)

}

return true

}

})

html中其实是无法判断应用是否安装,除非在webview中通过js bridge,这里通过一种方式达到此目的。

1、编辑AndroidManifest.xml:

主要是增加第二个<intent-filter>,myapp用来标识schema,最好能保证手机系统唯一,那样就可以打开应用,而不是d出一个选择框。

android:pathPrefix标识url的path,可以附带自己的数据通过string传递到activity,比如完整url为 myapp://xxx/openwith?data=mydata

[html] view plaincopy

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<activity

android:name="com.abc.MainActivity"

android:configChanges="orientation|keyboardHidden|navigation|screenSize"

android:screenOrientation="landscape"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />

<category android:name="android.intent.category.DEFAULT"/>

<data android:scheme="myapp" android:pathPrefix="/xxx/openwith" />

</intent-filter>

t/activity>

然后通过activity获得data数据:

1

2

3

4

5

6

[java] view plaincopy

public void onCreate(Bundle savedInstanceState) {

Uri uridata = this.getIntent().getData()

String mydata = uridata.getQueryParameter("data")

...

}

2、编写html页面:

整个页面也许是某个app的详细介绍,这里只写出关键的js代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[javascript] view plaincopy

function openApp() {

if (/android/i.test(navigator.userAgent)) {

var isrefresh = getUrlParam('refresh')// 获得refresh参数

if(isrefresh == 1) {

return

}

window.location.href = 'myapp://xxx/openwith?data=mydata'

window.setTimeout(function () {

window.location.href += '&refresh=1' // 附加一个特殊参数,用来标识这次刷新不要再调用myapp:// 了

}, 500)

}

}

上面代码可以达到这样一个目的,先请求 myapp:// ,如果系统能处理,或者说已经安装了myapp表示的应用,那么就可以打开,另外,如果不能打开,直接刷新一下当前页面,等于是重置location。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5915307.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-08
下一篇 2023-03-08

发表评论

登录后才能评论

评论列表(0条)

保存