h5外部瀏覽器直接調起app


1. 安卓端:

 其中,scheme必須是小寫的,同時要求H5必須是“<a href="appback://">啟動應用程序</a> ”

2. h5端完整示例:

 1   <a href="javascript:testApp('appback://')" class="dl-btn" id="download">打開APP</a> 
 2     <script>
 3     function testApp(url) { 
 4           var timeout, t = 1000, hasApp = true; 
 5           setTimeout(function () { 
 6             if (!hasApp) { 
 7                   //未安裝app
 8                   if(browser.versions.ios){
 9                     window.location.href = '${url_ios}';  //ios下載地址
10                 }else{
11                     window.location.href = '${url_android}';    //安卓下載地址
12                    }
13             }
14             document.body.removeChild(ifr); 
15           }, 2000) 
16           
17           var t1 = Date.now(); 
18           var ifr = document.createElement("iframe"); 
19           ifr.setAttribute('src', url); 
20           ifr.setAttribute('style', 'display:none'); 
21           document.body.appendChild(ifr); 
22           timeout = setTimeout(function () { 
23              var t2 = Date.now(); 
24              if (!t1 || t2 - t1 < t + 100) { 
25                hasApp = false; 
26              } 
27           }, t); 
28         } 
29         //判斷訪問終端
30         var browser={
31             versions:function(){
32                 var u = navigator.userAgent, app = navigator.appVersion;
33                 return {
34                     trident: u.indexOf('Trident') > -1, //IE內核
35                     presto: u.indexOf('Presto') > -1, //opera內核
36                     webKit: u.indexOf('AppleWebKit') > -1, //蘋果、谷歌內核
37                     gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐內核
38                     mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否為移動終端
39                     ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios終端
40                     android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android終端或者uc瀏覽器
41                     iPhone: u.indexOf('iPhone') > -1 , //是否為iPhone或者QQHD瀏覽器
42                     iPad: u.indexOf('iPad') > -1, //是否iPad
43                     webApp: u.indexOf('Safari') == -1, //是否web應該程序,沒有頭部與底部
44                     weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
45                     qq: u.match(/\sQQ/i) == " qq" //是否QQ
46                 };
47             }(),
48             language:(navigator.browserLanguage || navigator.language).toLowerCase()
49         }
50     </script>

3. ios端——通過URL協議實現從Safari等瀏覽器中跳轉打開你的app

第一步:在info.plist中加入這些內容

其中URL identifier 可以隨便取,URL Schemes 就是實現跳轉URL協議的名稱(可以多個)

第二步:在視圖控制器中加入這樣的代碼用於顯示跳轉過來的地址:

+(void)alert:(NSString*)information{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"程序通過URL協議打開,該URL為:“%@”",information] delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

第三步:在AppDelegate.m中加入這些代碼

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    if(!url){
        return NO;
    }
    
    NSString *urlString=[url absoluteString];
    [ViewController alert:urlString];
    return YES;
}

測試方法:在瀏覽器中輸入“appABC://”之后就會打開這個程序,打開后程序中會顯示跳轉過來的鏈接地址。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM