iOS-html调起APP并传参

前一段时间要做一个html页面调起APP应用的需求,特记下研究成果
1、在项目info.plist中添加URL Types以供html调用

2、在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、在Appdelegate中- (BOOL)application:(UIApplication )application openURL:(NSURL )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{}方法里监听html的调起

#pragma mark - CallbackMethod
//打开外部链接回调
- (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;
    }
}

你可能感兴趣的:(html,支付,APP,传参,调起app)