1, in the Uiwebviewdelegate method to intercept the request, set requests header, nonsense not to say to see the code:
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype {
NSString *urlstring = [[Request URL] absolutestring];
NSLog (@ "==>%@", urlstring);
Nsmutableurlrequest *mutablerequest = [request mutablecopy];
Nsdictionary *requestheaders = Request.allhttpheaderfields;
//To determine if the request header is included, if not judged, causes the webview to load with a dead loop
if (requestheaders[@ "token"] && requestheaders[@ "ClientType"]) {
return YES;
} else {
[Mutablerequest setvalue:@ "token" forhttpheaderfield:@ "token"];
[Mutablerequest setvalue:@ "ios" forhttpheaderfield:@ "ClientType"];
request = [mutablerequest copy];
[WebView Loadrequest:request];
return NO;
}
return YES;
}
After my own test, this has a certain problem, when the webview execution of the GoBack method, resulting in two interfaces load each other, resulting in a dead loop loading can not be returned, the solution please refer to the following more awesome Method 2
2, write a class inheritance Nsurlprotocol, in the + (BOOL) Caninitwithrequest: (Nsurlrequest *) Request method to intercept requests to add the requested header, This allows all WebView operation requests to be transferred here, adding a request header to the requests, without affecting the WebView GoBack method returning
#import <Foundation/Foundation.h>
@interface Kjurlprotocol:nsurlprotocol
@end
#import "KJURLProtocol.h"
@implementation Kjurlprotocol
+ (void) Load {
[Nsurlprotocol registerclass:self];
}
+ (BOOL) Caninitwithrequest: (Nsurlrequest *) Request {
if ([Request Iskindofclass:[nsmutableurlrequest class]]) {
[(ID) Request setvalue:@ "ios" forhttpheaderfield:@ "ClientType"];
[(ID) Request Setvalue:[kjappdata instance].token forhttpheaderfield:@ "token"];
}
return NO;
}
@end
/********************************************************************************/
+ (BOOL) Caninitwithrequest: (nsurlrequest *) request//In this method to decide whether to intercept the requests, return Yes to intercept
+ (Nsurlrequest *) Canonicalrequestforrequest: (nsurlrequest *) request//Modify requests in this method and return
-(void) startloading//execution of specific request requests
-(void) stoploading //Interrupt specific request requests
/********************************************************************************/
Attached: iOS8 Apple introduced a Load page view more hanging frame WebKit, wherein the Wkwebview basically contains all the uiwebview of things, it is said that the performance of the optimization is not a little bit, and added a lot of mainstream things, For example: to achieve the page slip between the return, estimate the page loading progress, if your application has been the minimum support iOS8, you still in because of what, quickly try it.
IOS UIWebView two ways to add a request header