iOS Network Chapter 1 intercept local URL request (NSURLPROTOCOL)

Source: Internet
Author: User

This article reprinted to http://blog.csdn.net/u014011807/article/details/39894247

Nsurlprotocol is a very important part of iOS and we often use it in the following places:

(1) Network request proxy Forwarding (FQ network acceleration, etc.)

(2) Offline cache policy

In short, we need to use this stuff whenever we need to intercept local URL requests.

The Nsurlconnection and WebView page loads we use frequently in iOS are intercepted by Nsurlprotocol, so the core of this section is how to use this section:

Below I will describe the use of nsurlprotocol according to the procedure used:

Final Run Result:

(1) First we need to create a class that inherits Nsurlprotocol

and implement proxy inheritance:

@interfaceMyURLProtocol () <NSURLConnectionDelegate>

@end

(2) Register this Agreement

In general, this agreement is registered in the Appdelegate file:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

{

[Nsurlprotocol Registerclass:[myurlprotocolclass]];

Override point for customization after application launch.

Returnyes;

}

You can use this stuff down here.

(3) The following is ready for a WebView page load code, this is too simple, not detailed, to give a reference code:

-(void) SendRequest {

NSString *text =self.textfield.text;

if (![ textisequaltostring:@ ""]) {

Nsurl *url = [Nsurlurlwithstring:text];

Nsurlrequest *request = [Nsurlrequestrequestwithurl:url];

[Self.webview Loadrequest:request];

}

}

(4) The normal use of this nsurlprotocol only need to simply use the following 9 methods, these nine methods are the most basic and most important method of this protocol can be achieved, of course, it has a lot of advanced features, here is not introduced.

The 9 most important methods are as follows:

<1>

@method: Create an Nsurlprotocol instance, and after Nsurlprotocol is registered, all nsurlconnection will check to see if the HTTP request is held by this method.

@parma:

@return: YES: Hold the HTTP request no: Do not hold the HTTP request

+ (BOOL) Caninitwithrequest: (Nsurlrequest *) request

#pragma mark--nsurlprotocol hold Relevantmethod 4 ways

<2-5>

@method: Nsurlprotocol Abstract classes must be implemented. Usually there is a minimum standard: the input/output request satisfies the most basic protocol specification. So the simple procedure here can be returned directly. In general, we are not going to change this request. If you want to change, like adding a title to this request, combine it into a new HTTP request.

@parma: Local HttpRequest requests: request

@return: Direct forwarding

+ (nsurlrequest*) Canonicalrequestforrequest: (Nsurlrequest *) request

@method: Nsurlprotocol Cache system settings: If there are two URL requests and they are equal, then the same cache space can be used here

@parma: Local HttpRequest requests: request

@return:

+ (BOOL) requestiscacheequivalent: (Nsurlrequest *) a torequest: (nsurlrequest*) b

@method: Get data on a website to establish connect connection

@parma:

@return:

-(void) startloading

@method: Called when the current connection connection is canceled. Pay particular attention to this stoploading method, when the local nsurlrequest initialization, there is a time-out period, under the low-speed network, it is possible that the page has not been loaded, the Stoploading method is called.

@parma:

@return:

-(void) stoploading

<6-9> Receive data

#pragma mark--nsurlprotocol Delegate 4 ways

-(void) connection: (nsurlconnection*) connection didreceiveresponse: (Nsurlresponse *) response

-(void) connection: (nsurlconnection*) connection didreceivedata: (nsdata*) data

-(void) connectiondidfinishloading: (nsurlconnection*) connection

-(void) connection: (nsurlconnection*) connection didfailwitherror: (nserror*) error

Note the method in this case:

Caninitwithrequest creates an instance, yes, and then continues to call Startloading. This will continue to call Caninitwithrequest. Into a dead loop, so here's our usual practice of setting up a [Nsurlprotocolsetproperty: @YESforKey: @ "Myurlprotocolhandledkey" inrequest:newrequest];

This prevents the program from going into a dead loop.

(5) A reference code is given below:

@implementation Myurlprotocol

/**

@method: Create an Nsurlprotocol instance, and after Nsurlprotocol is registered, all nsurlconnection will check to see if the HTTP request is held by this method.

@parma:

@return: YES: Hold the HTTP request no: Do not hold the HTTP request

*/

+ (BOOL) Caninitwithrequest: (Nsurlrequest *) Request {

Staticnsuinteger requestcount = 0;

NSLog (@ "Request #%u:url =%@", requestcount++, request);

if ([nsurlprotocolpropertyforkey:@ "Myurlprotocolhandledkey" inrequest:request]) {

Returnno;

}

Returnyes;

}

#pragma mark--nsurlprotocol hold relevant Method

/**

@method: Nsurlprotocol Abstract classes must be implemented. Usually there is a minimum standard: the input/output request satisfies the most basic protocol specification. So the simple procedure here can be returned directly. In general, we are not going to change this request. If you want to change, like adding a title to this request, combine it into a new HTTP request.

@parma: Local HttpRequest requests: request

@return: Direct forwarding

*/

+ (Nsurlrequest *) Canonicalrequestforrequest: (nsurlrequest*) Request {

return request;

}

/**

@method: Nsurlprotocol Cache system settings: If there are two URL requests and they are equal, then the same cache space can be used here

@parma: Local HttpRequest requests: request

@return:

*/

+ (BOOL) requestiscacheequivalent: (Nsurlrequest *) a torequest: (Nsurlrequest *) b {

return [SUPERREQUESTISCACHEEQUIVALENT:ATOREQUEST:B];

}

-(void) startloading {

Nsmutableurlrequest *newrequest = [Self.requestmutablecopy];

[Nsurlprotocolsetproperty: @YESforKey: @ "Myurlprotocolhandledkey" inrequest:newrequest];

Self.connection = [NSURLConnectionconnectionWithRequest:newRequestdelegate:self];

}

/**

@method: Called when the current connection connection is canceled

@parma:

@return:

*/

-(void) stoploading {

[Self.connectioncancel];

Self.connection =nil;

}

#pragma Mark--nsurlprotocol Delegate

-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response {

[Self.clientURLProtocol:self Didreceiveresponse:response cachestoragepolicy:nsurlcachestoragenotallowed];

}

-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data {

[Self.clientURLProtocol:selfdidLoadData:data];

}

-(void) connectiondidfinishloading: (nsurlconnection *) connection {

[Self.clientURLProtocolDidFinishLoading:self];

}

-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error {

[Self.clientURLProtocol:selfdidFailWithError:error];

}

@end

iOS Network Chapter 1 intercept local URL request (NSURLPROTOCOL)

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.