IOS UIWebView two ways to add a request header

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.