ifish/Ifish/UMCommunity/UMCommunitySDK/UMCommunityUI/Common/WebVc/UMComWebViewController.m

106 lines
3.2 KiB
Objective-C

//
// UMComWebViewController.m
// UMCommunity
//
// Created by Gavin Ye on 8/19/15.
// Copyright (c) 2015 Umeng. All rights reserved.
//
#import "UMComWebViewController.h"
#import "UIViewController+UMComAddition.h"
@interface UMComWebViewController ()
@property (nonatomic) BOOL authenticated;
@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, strong) NSURLRequest *failedRequest;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSURLConnection * urlConnection;
@end
@implementation UMComWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (instancetype)initWithUrl:(NSString *)url
{
self = [super init];
if (self) {
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.delegate = self;
self.webView = webView;
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
[self.view addSubview:webView];
}
return self;
}
//兼容HTTPS
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
BOOL result = _authenticated;
if (!_authenticated) {
self.failedRequest = request;
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
return result;
}
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURL* baseURL = self.request.URL;
if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse {
_authenticated = YES;
[connection cancel];
[self.webView loadRequest:self.failedRequest];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end