“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

It is well known that apple tax is really ruthless, any APP will take 30%, although due to the epidemic now for individual business situation of bad companies to reduce the cut, but the feeling is still a week to peel, since so ruthless, then how can bypass this cut? Method is still a bit, but not recommended, there will be a risk of closure. This is just a technical discussion

implementation

How do we implement WKWebView in the APP to evoke the third-party payment platform? Here is divided into wechat Alipay two major platforms.

First, we need to intercept the URL to jump in WKWebView’s proxy method for special processing

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { BOOL isIntercepted = [[AlipaySDK defaultService] PayInterceptorWithUrl: urlStr fromScheme: @ "cailemao" callback: ^ (NSDictionary * result) {/ / processing results NSLog (@ "% @", result); If ([result[@"isProcessUrlPay"] boolValue]) {// returnUrl represents the success page URL that the third-party App needs to jump to NSString* urlStr = result[@"returnUrl"]; [wself loadWithUrlStr:urlStr]; } }]; if (isIntercepted) { decisionHandler(WKNavigationActionPolicyCancel); return; } if ([self someBao:urlScheme urlStr:urlStr]) { decisionHandler(WKNavigationActionPolicyCancel); return; } decisionHandler(WKNavigationActionPolicyAllow); }Copy the code

Processing of wechat payment

/ / WeChat pay nsstrings * wxPre = @ "https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"; if ([request.URL.absoluteString hasPrefix:wxPre] && ! [request.URL.absoluteString containsString:[NSString stringWithFormat:@"%@://",urlSchemes]]) { NSArray * newUrl = [request.URL.absoluteString componentsSeparatedByString:@"redirect_url="]; NSString * newStr = [NSString stringWithFormat:@"%@redirect_url=%@://%@",newUrl[0],urlSchemes,newUrl[1]]; NSMutableURLRequest * newRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:newStr]]; newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields; [webView loadRequest:newRequest]; decisionHandler(WKNavigationActionPolicyCancel); return; } if ([request.url.scheme containsString:@"weixin"]) {if (@available(iOS 10.0, *)) { [UIApplication.sharedApplication openURL:request.URL options:@{} completionHandler:nil]; } else { // Fallback on earlier versions [[UIApplication sharedApplication] openURL:request.URL]; }}Copy the code

Alipay processing

- (BOOL)someBao:(NSString *)urlScheme urlStr:(NSString *)urlStr { // if ([urlStr hasPrefix:@"alipay://alipayclient/"]) {  // NSLog(@"123"); // } [[AlipaySDK defaultService] payInterceptorWithUrl:urlStr fromScheme:@"xxxx" callback:^(NSDictionary *resultDic) { NSLog(@"123"); }]; if ([urlStr hasPrefix:@"alipays://"] || [urlStr hasPrefix:@"alipay://"]) { NSURL* alipayURL = [self changeURLSchemeStr:urlStr]; [[UIApplication sharedApplication] openURL:alipayURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:nil]; return true; } return false; } - (NSURL*)changeURLSchemeStr:(NSString*)urlStr{ NSString* tmpUrlStr = urlStr; if([urlStr containsString:@"fromAppUrlScheme"]) { tmpUrlStr = [tmpUrlStr stringByRemovingPercentEncoding]; NSDictionary* tmpDic = [self dictionaryWithUrlString:tmpUrlStr]; NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithDictionary:tmpDic]; tmp[@"fromAppUrlScheme"] = @"xxxx"; NSString* tmpValue = [tmpDic valueForKey:@"fromAppUrlScheme"]; tmpUrlStr = [[tmpUrlStr stringByReplacingOccurrencesOfString:tmpValue withString:@"xxxx"] mutableCopy]; tmpUrlStr = [[tmpUrlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]  mutableCopy]; } NSURL * newURl = [NSURL URLWithString:tmpUrlStr]; return newURl; } - (NSDictionary*)dictionaryWithUrlString:(NSString*)urlStr{ if(urlStr && urlStr.length && [urlStr rangeOfString:@"?"] .length==1) { NSArray *array = [urlStr componentsSeparatedByString:@"?"] ; if(array && array.count==2) { NSString*paramsStr = array[1]; if(paramsStr.length) { NSString* paramterStr = [paramsStr stringByRemovingPercentEncoding]; NSData *jsonData = [paramterStr dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]; return responseDic; } } } return nil; }Copy the code

conclusion

If you use H5 to pay, it is just for convenient operation, rather than to bypass the apple tax operation, although it can achieve this purpose, but if it is detected, it will be closed, so we still do not do so.

If you have any questions or new ideas, please let me know in the comments section. Thank you