支付宝和微信h5支付方式调起来

2019-03-03  本文已影响0人  i爱吃土豆的猫

1.支付宝H5支付

步骤1:后台返回 urlStr, 用webview加载(loadHtmlWithUrl)
步骤2: plist 文件配置白名单(alipays:// 、alipay://)
步骤3: 核心代码
- (void)loadHtmlWithUrl:(NSString *)urlStr{   

 NSString *htmlText = [NSString stringWithFormat:@"<html><body>%@</body></html>",urlStr];

[self.webView loadHTMLString:htmlText baseURL:nil];
}

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

   NSString* reqUrl = request.URL.absoluteString;

   **if** ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) {

    *// NOTE: 跳转支付宝App*

    **BOOL** bSucc = [[UIApplication sharedApplication]openURL:request.URL];        

    *// NOTE: 如果跳转失败,则跳转itune下载支付宝App*

    if(!bSucc) {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                       message:@"未检测到支付宝客户端,请安装后重试。"

                                                      delegate:**self**

                                             cancelButtonTitle:@"立即安装"

                                             otherButtonTitles:**nil**];
        [alert show];
    }

    return NO;
}

return  YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

// NOTE: 跳转itune下载支付宝App*

NSString* urlStr = @"[https://itunes.apple.com/cn/app/zhi-fu-bao-qian-bao-yu-e-bao/id333206289?mt=8](https://itunes.apple.com/cn/app/zhi-fu-bao-qian-bao-yu-e-bao/id333206289?mt=8)";

NSURL *downloadUrl = [NSURL URLWithString:urlStr];

[[UIApplication sharedApplication]openURL:downloadUrl];
  }

2.微信H5支付

步骤1:后台返回 urlStr, 用webview拦截
步骤2: plist 文件配置白名单
步骤3: 核心代码
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

            **if** ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"wechat://"]]  || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"wexin://"]]) {

                  NSLog(@"跳转到WX");

                   **return** **YES**;

           }**else**{

                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                   message:@"未检测到微信客户端,请安装后重试。"

                                                  delegate:**self**

                                         cancelButtonTitle:@"立即安装"

                                         otherButtonTitles:**nil**];

                      [alert show];

  }    

           **return** **YES**;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

*// NOTE: 跳转itune下载微信App*

NSString* urlStr = @"[https://itunes.apple.com/cn/app//id836500024?mt=12](https://itunes.apple.com/cn/app//id836500024?mt=12)";

NSURL *downloadUrl = [NSURL URLWithString:urlStr];

[[UIApplication sharedApplication]openURL:downloadUrl];
 }
上一篇下一篇

猜你喜欢

热点阅读