UIWebView 載入重定向url、新增請求頭資訊
阿新 • • 發佈:2019-01-26
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ // 新增請求頭資訊 NSMutableURLRequest *mutableRequest = [request mutableCopy]; [mutableRequest setValue:accessToken forHTTPHeaderField:@"Authorization"]; request = [mutableRequest copy]; if (navigationType != UIWebViewNavigationTypeOther) { self.loadedURL = request.URL.absoluteString; } if ([request.URL.absoluteString isEqualToString:self.loadedURL]) { [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError || ([response respondsToSelector:@selector(statusCode)] && [((NSHTTPURLResponse *)response) statusCode] != 200 && [((NSHTTPURLResponse *)response) statusCode] != 302)) { //Show error message LLog(@"%@ %@",response,connectionError); }else {// 重定向的URL [webView loadRequest:[NSURLRequest requestWithURL:kURLString(response.URL.absoluteString)]]; LLog(@"%@ %@",response,connectionError); } }]; return NO; } return YES; }