项目里需要使用银联支付,直接拿出WKWebView。
```
//设置webview配置
WKWebViewConfiguration*config = [[WKWebViewConfigurationalloc]init];
config.preferences.minimumFontSize=18;
//初始化webview
_webView= [[WKWebViewalloc]initWithFrame:CGRectMake(0,0,IPHONE_W,IPHONE_H-60)configuration:config];
//设置代理
_webView.UIDelegate=self;
_webView.navigationDelegate=self;
//加载网页
NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:urlstring]];
[self.webViewloadRequest:request];
```
做个加载进度条吧
```
//监听webview的加载进度
[_webViewaddObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNewcontext:nil];
#pragma mark计算wkWebView进度条
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if(object ==self.webView&& [keyPathisEqualToString:@"estimatedProgress"])
{
CGFloatnewprogress = [[changeobjectForKey:NSKeyValueChangeNewKey]doubleValue];
if(newprogress ==1)
{
self.progressView.hidden=YES;
[self.progressViewsetProgress:0animated:NO];
}
else
{
self.progressView.hidden=NO;
[self.progressViewsetProgress:newprogressanimated:YES];
}
}
}
```
项目中需要使用到JS与OC的交互,这里使用JS 调用iOS
```
WKUserContentController*userCC = config.userContentController;
//JS调用OC添加处理脚本
[userCCaddScriptMessageHandler:selfname:@"test"];
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message {
NSLog(@"%@",message.body);
if([message.nameisEqualToString:@"test"]) {
if(self.successBlock) {//成功后回调
self.successBlock();
}
[self.navigationControllerpopViewControllerAnimated:YES];
}
}
```
当然,你也可以使用代理在网页加载过程中进行操作
```
#pragma mark - WKNavigationDelegate
//页面开始加载时调用
- (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation
{
NSLog(@"%@",navigation);
}
//当内容开始返回时调用
- (void)webView:(WKWebView*)webView didCommitNavigation:(WKNavigation*)navigation
{
NSLog(@"%@",navigation);
}
//页面加载完成之后调用
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation
{
self.title= webView.title;
NSLog(@"%@",navigation);
}
//页面加载失败时调用
- (void)webView:(WKWebView*)webView didFailProvisionalNavigation:(WKNavigation*)navigation
{
NSLog(@"%@",navigation);
}
```
一切准备就绪,开始打开银联支付页......一片空白?黑人问号脸?试了一个百度(HTTPS),可以加载,这个咋加载不出来。其实是因为百度这个是付费的SSL证书,但是银联支付使用的是自签名,那就需要我们来验证这个签名了
```
//验证证书
-(void)webView:(WKWebView*)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge completionHandler:(void(^)(NSURLSessionAuthChallengeDisposition,NSURLCredential*_Nullable))completionHandler
{
if(challenge.previousFailureCount==0) {
NSURLCredential*cre = [[NSURLCredentialalloc]initWithTrust:challenge.protectionSpace.serverTrust];
[challenge.senderuseCredential:creforAuthenticationChallenge:challenge];
completionHandler(NSURLSessionAuthChallengeUseCredential,cre);
}else{
[challenge.sendercancelAuthenticationChallenge:challenge];
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,nil);
}
}
```
或者使用
```
#pragma mark === connectDelegate
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"验证签名证书");
if ([challenge previousFailureCount] == 0)
{
_authenticated = YES;
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"WebController received response via NSURLConnection");
// remake a webview call now that authentication has passed ok.
_authenticated = YES;
[self.showView loadRequest:_request];
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnection cancel];
}
// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
```
点击成功加载,支付成功后JS成功回调。