场景描述:
公司的APP主要通过WKWebView调用wap页面,里面有支付宝支付。点击立即支付,如果手机装有支付宝,应该打开支付宝客户端的。问题是,Android截获URL跳转是没有问题的,但是iOS却有问题。无法打开客户端还是停留支付宝wap页面。一直调试考虑如何解决这个问题,还是同事点子多,结合了Android和iOS重新拼接URL,然后open,就可以了,能想到这种法子,实在是佩服。
以下为参考代码:
//在发送请求之前,决定是否跳转 如果不实现这个代理方法,默认会屏蔽掉打电话等url
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *url = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
NSString* reUrl=[[webView URL] absoluteString];
if ([url containsString:kBaseUrl]) {
reUrl=url;
}
if ([url containsString:@"alipay://"]) {
// alipay://alipayclient/?{"dataString":"h5_route_token=\"5ca0bf28bad7cdb27e9069853cfb67d7\"&is_h5_route=\"true\"","requestType":"SafePay","fromAppUrlScheme":"alipays"}
NSString* dataStr=[url substringFromIndex:23];
NSLog(@"%@",dataStr);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[dataStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
//appId=20000125&orderSuffix=h5_route_token%3D%22ba8f51bce235fe3b02f945da619125e5%22%26is_h5_route%3D%22true%22#Intent
NSMutableString* mString=[[NSMutableString alloc] init];
[mString appendString:@"alipays://platformapi/startApp?appId=20000125&orderSuffix="];
// [mString appendString:@"alipay://alipayclient/?appId=20000125&orderSuffix="];
//url进行编码
[mString appendString:[self encodeString:dict[@"dataString"]]];
// alipays://platformapi/startApp?appId=20000125&orderSuffix=h5_route_token=\"5f941ada4bcb49832562e7289dce72ee\"&is_h5_route=\"true\""
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mString]];
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
}
-(NSString*)encodeString:(NSString*)unencodedString{
// CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
// CharactersToLeaveUnescaped = @"[].";
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}
虽然可以打开测试没有问题,但是还是觉得可能会有其他问题,如果哪位兄弟姐妹遇到过类似问题,希望指教。
后来又尝试通过iOS返回的URL,但是对参数进行编码,也可以,提出疑问,为啥iOS本身的URL或者用Android的URL都可以打开iOS端的支付宝:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *url = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
NSString* reUrl=[[webView URL] absoluteString];
if ([url containsString:kBaseUrl]) {
reUrl=url;
}
if ([url containsString:@"alipay://"]) {
NSInteger subIndex = 23;
NSString* dataStr=[url substringFromIndex:subIndex];
//编码
NSString *encodeString = [self encodeString:dataStr];
NSMutableString* mString=[[NSMutableString alloc] init];
[mString appendString:[url substringToIndex:subIndex]];
[mString appendString:encodeString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mString]];
decisionHandler(WKNavigationActionPolicyAllow);
return;
}