首先要在文件中设置这个参数:
(联网)
遵循这个代理
加上这俩个参数
pragma mark - UIWebView delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:
(NSURLRequest *)request navigationType:
(UIWebViewNavigationType)navigationType
{
NSString *requestString = [[request URL] absoluteString];
DebugLog(@"requestString=====%@",requestString);
if (!_authenticated) {
_authenticated = kNeverAuthenticate;
_urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
[_urlConnection start];
return YES;
} else if (!([requestString rangeOfString:ACTIVITY_FINISHED_KEYWORDS].location
== NSNotFound)) {
NSArray *components = [requestString componentsSeparatedByString:@"#?"];
for (NSString *key in components)
{
if ([key rangeOfString:ACTIVITY_FINISHED_KEYWORDS].location !=
NSNotFound)
{
NSString *actMatId = [[key componentsSeparatedByString:@"="] lastObject];
[[NSNotificationCenter defaultCenter]
postNotificationName:ActivityMatFinishedNotification object:actMatId];
break;
}
}
return NO;
}
return YES;
}
在这里拦截加载的URL的协议,如果是https则去获取信任。
#pragma mark NURLConnection 代理方法
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(@"WebController 已经得到授权正在请求 NSURLConnection");
if ([challenge previousFailureCount] == 0){
_authenticated = kTryAuthenticate;
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 已经收到响应并通过了 NSURLConnection请求");
_authenticated = kTryAuthenticate;
[self.webView loadRequest:_request];
[_urlConnection cancel];
}
- (BOOL)connection:(NSURLConnection *)connection
canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
return [protectionSpace.authenticationMethod
isEqualToString:NSURLAuthenticationMethodServerTrust];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
[_progressLayer startLoad];
[SVProgressHUD showImage:[UIImage imageNamed:@"Nodata_McLearning"]
status:@"数据正在加载"];
DebugLog(@"开始加载");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
DebugLog(@"加载完成");
[[NSUserDefaults standardUserDefaults] setInteger:0
forKey:@"WebKitCacheModelPreferenceKey"];
[_progressLayer finishedLoad];
[SVProgressHUD dismissWithDelay:0.5];
if ([self.webView canGoBack]) {
self.title = [webView
stringByEvaluatingJavaScriptFromString:@"document.title"];
} else { if (!self.titleStr) {
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
else
{
self.title = _titleStr;
}
}
NSString *requestString = [[self.webView.request URL] absoluteString];
if([requestString rangeOfString:@"receiveMsgView.do"].location !=NSNotFound){
self.isMsg=YES;
}
else{
self.isMsg=NO;
}
self.context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//放大html里面的图片
[self loadPhoto:self.context];
//返回到上一个页面
[self goToHomePage:self.context];
//新版的对接方法
[self Pay:self.context];
//积分商城的对接方法
[self doPay:self.context];
[self doOrder:self.context];
//社会化分享
[self social:self.context];
//资料下载
[self downMaterial:self.context];
//课程播放
[self courseVideoPlay:self.context];
//课程下载
[self downloadCourse:self.context];
}
具体链接