iOS中html打開APP傳參

一、在項目info.plist中添加URL Types以供html調用javascript

 

2.html代碼html

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>支付頁面</title>

</head>

<script type="text/javascript">

    var url = location.search; //獲取url中"?"符後的字串
    var typestr="";
    //截取參數 url=xxxx?type=1
    if (url.indexOf("?") != -1) {
        typestr = url.split("type=")[1];
    }

    //支付寶
    if(typestr==1){
         //支付寶調起應用
        window.load=AliPay();
    }
    else if(typestr==2){
        //微信調起應用
        window.load=Weixin();
    }

    //支付寶調起應用,嚮應用傳參數,0表示不作任何操做,1表示爲支付寶調起的app,2表示爲微信調起的app
    function AliPay(){
        if(confirm("是否打開支付寶?")){//點擊了肯定
            document.location = "XXXXFromHtml://type:1";
        }
        else{//點擊了取消後返回應用
            document.location = "XXXXFromHtml://type:0";
        }
    }

    //微信調起應用
    function Weixinclick(){
        if(confirm("是否打開微信?")){//點擊了肯定
            document.location = "XXXXFromHtml://type:2";
        }
        else{//點擊了取消
            document.location = "XXXXFromHtml://type:0";
        }
    }

</script>
<body>
</body>
</html>

 

3.客戶端代碼java

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

//打開外部連接回調
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    //網頁調起app外面連接url type
    NSString *string =url.absoluteString;
    if ([string hasPrefix:@"xxxxfromhtml://"]) {
       //截取html傳遞的參數類型
        NSString *type=[[string pathComponents] lastObject];
        NSString *typeStr = [[type componentsSeparatedByString:@":"]lastObject];
        NSInteger apptype = [typeStr integerValue];
//        DebugLog(@"%@",array);
         BaseNavigationController *nav=(BaseNavigationController*)self.window.rootViewController;
         //當前離開應用的控制器
         if ([[nav.childViewControllers lastObject] isKindOfClass:[XXXXPayViewController class]]) {
            //從網頁調起app,不會調用充值界面方法,需手動調用
            XXXXPayViewController *scoreVC=[nav.childViewControllers lastObject];
            //在內部調用支付的應用跳轉
            [scoreVC fromWebHtmlWithType:apptype];
        }
        return YES;
    }
}
相關文章
相關標籤/搜索