首先要在文件中設(shè)置這個(gè)參數(shù):
(聯(lián)網(wǎng))
遵循這個(gè)代理
加上這倆個(gè)參數(shù)
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的協(xié)議,如果是https則去獲取信任。
#pragma mark NURLConnection 代理方法
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(@"WebController 已經(jīng)得到授權(quán)正在請求 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 已經(jīng)收到響應(yīng)并通過了 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:@"數(shù)據(jù)正在加載"];
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];
//返回到上一個(gè)頁面
[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];
}
具體鏈接