關於iOS網頁UIWebview的http請求替換成https請求,涉及JS交互


網上應該有很多相關的解決辦法,這種是調用官方的API, 所以從http直接轉https會容易點,而且要修改的地方不多,只需實現以下幾個代理方法就可以了,把返回的NSData類型數據 encoding 一下,然后 loadHTMLString 就行了,不多說,直接上代碼先:

@interface ViewController ()

<UIWebViewDelegate,NSURLConnectionDelegate,NSURLConnectionDataDelegate>

{

    UIWebView *webView;

    NSURLConnection *connenction;

    NSMutableData *mutableData;

}

@end

 

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    webView.delegate = self;

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];

    [webView loadRequest:request];

    [self.view addSubview:webView];

    

    mutableData = [NSMutableData data];

}

 // 相關代理方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

    if ([request.URL.scheme isEqualToString:@"https"]) {

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-Wdeprecated-declarations"

        connenction = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        [connenction start];

#pragma clang diagnostic pop

        return NO;

    }

    return YES;

}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    // 判斷是否是信任服務器證書

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        // 創建一個憑據對象

        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

        //告訴服務器客戶端信任證書

        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];

    } else {

        if ([challenge previousFailureCount] == 0) {

                [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];

        } else {

            [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];

        }

    }

}

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"response %@",response);

}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [mutableData appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *html = [[NSString alloc] initWithData:[mutableData copy] encoding:NSUTF8StringEncoding];

    [webView loadHTMLString:html baseURL:[[NSURL alloc] init]];

    [connenction cancel];

    [mutableData setData:[NSData data]];

}

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    //清空數據

    [mutableData setData:[NSData data]];

}

 

以上方法親測可行,而且如果你在用http請求時通過 bridgeWebview 實現了JS交互,也是可以的,不會有任何影響,只需在你之前的 UIWebviewController 中實現上面的幾個代理方法 就可以了,這樣既兼容了 http 請求 也可以兼容 https 請求。本人也只是稍微研究了下,具體有什么不足的地方還請大神們指點出來。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM