最近有业务需求 要加载一个第三方的支付页面 流程是这样的 流程是我在加载该页面的时候将后台传给我的一个加密串传递给这个地址 因为以前没做过类似的东西 所以第一次比较费劲 不过好歹搞了出来 本人菜鸟 有不对的地方请大神指正 下面贴代码
首先定义3个全局变量
NSMutableURLRequest* _originRequest;
NSURLConnection* _urlConnection;
BOOL_authenticated;//此参数代表是否通过身份验证通过后为1才能正常加载https
初始化webview 如果要传参数 不要再这里传 先加载一遍https地址
if(self.mainWebView==nil) {
self.mainWebView= [[UIWebViewalloc]initWithFrame:CGRectMake(0,64,SCREEN_WIDTH,SCREEN_HEIGHT-64)];
self.mainWebView.scalesPageToFit=YES;
self.mainWebView.delegate=self;
_originRequest= [[NSMutableURLRequestalloc]initWithURL:[NSURLURLWithString:@"要加载的https地址"]];
[self.mainWebViewloadRequest:_originRequest];
[self.viewaddSubview:self.mainWebView];
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"Did start loading: %@ auth:%d", [[requestURL]absoluteString],_authenticated);
if(!_authenticated) {
_authenticated=NO;
_urlConnection= [[NSURLConnectionalloc]initWithRequest:_originRequestdelegate:self];
[_urlConnectionstart];
returnNO;
}
returnYES;
}
-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error
{
// 102 == WebKitErrorFrameLoadInterruptedByPolicyChange
NSLog(@"***********error:%@,errorcode=%ld,errormessage:%@",error.domain,(long)error.code,error.description);
if(!([error.domainisEqualToString:@"WebKitErrorDomain"] && error.code==102)) {
//当请求出错了会做什么事情
}
}
#pragma mark-NURLConnectiondelegate
-(void)connection:(NSURLConnection*)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge
{
NSLog(@"WebController Got auth challange via NSURLConnection");
if([challengepreviousFailureCount]==0)
{
_authenticated=YES;
NSURLCredential*credential=[NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.senderuseCredential:credentialforAuthenticationChallenge:challenge];
}else{
[[challengesender]cancelAuthenticationChallenge:challenge];
}
}
**重点是这个方法 第一次加载会通过一个身份验证 当身份验证通过后会来到这个方法 然后重新加载https地址 然后在这里将要传递给该地址的参数写上 就ok了
-(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;
// NSString *body = [NSString stringWithFormat:@"msg=%@",self.baseStr];
[_originRequestsetHTTPMethod:@"POST"];
[_originRequestsetHTTPBody:[self.baseStrdataUsingEncoding:NSUTF8StringEncoding]];
[self.mainWebViewloadRequest:_originRequest];
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnectioncancel];
}
- (void)URLSession:(NSURLSession*)session didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge completionHandler:(void(^)(NSURLSessionAuthChallengeDispositiondisposition,NSURLCredential*__nullablecredential))completionHandler{
NSLog(@"didReceiveChallenge");
// if([challenge.protectionSpace.host isEqualToString:@"api.lz517.me"] /*check if this is host you trust: */ ){
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust]);
// }
}
// 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.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];
}
网友评论