1. 程式人生 > >iOS APP內UIWebView呼叫支付寶應用

iOS APP內UIWebView呼叫支付寶應用

實現原理,判斷是否是有alipays://或者alipay://,如果有,就直接呼叫
[[UIApplication sharedApplication]openURL:request.URL];

OC程式碼

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString* reqUrl = request.URL.absoluteString;
    if
([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) { BOOL bSucc = [[UIApplication sharedApplication]openURL:request.URL]; //bSucc是否成功調起支付寶 if (!bSucc) { NSLog(@"呼叫失敗"); return NO; } } return YES; }

Swift程式碼

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType)
->
Bool { let reqUrl = request.url?.absoluteString if ((reqUrl?.hasPrefix("alipays://"))! || (reqUrl?.hasPrefix("alipay://"))!) { let bSucc:Bool = UIApplication.shared.openURL(request.url!) if !bSucc { print("呼叫失敗") return
false } } return true }