Just a very simple example.
http://www.google.com Google's homepage
All know that now in the browser open google.com words will actually become http://www.google.com.hk
The URL is redirected
How do I redirect in the app?
Using the Nsurlconnectiondatadelegate delegate of the Nsurlconnetion class
This method in the Nsurlconnectiondatadelegate delegate
- -(Nsurlrequest *) connection: (Nsurlconnection *) connection willsendrequest: (nsurlrequest *) Request Redirectresponse :(Nsurlresponse *) response
URLs that can be redirected later
Look at the code
- #import "ViewController.h"
- @interface Viewcontroller ()
- @end
- @implementation Viewcontroller
- -(void) Viewdidload {
- [Super Viewdidload];
- Nsurl *url = [Nsurl urlwithstring:@ "http://www.google.com"];
- Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
- Nsurlconnection *connection = [[Nsurlconnection alloc] initwithrequest:request delegate:self];
- if (!connection) {
- NSLog (@ "FAIL");
- }
- }
- -(Nsurlrequest *) connection: (Nsurlconnection *) connection willsendrequest: (nsurlrequest *) Request Redirectresponse :(Nsurlresponse *) Response {
- NSLog (@ "================================================");
- NSLog (@ "would send request\n%@", [request URL]);
- NSLog (@ "redirect response\n%@", [response URL]);
- return request;
- }
- @end
The above omitted some irrelevant code
Explain the code.
One connection in the Viewdidload method is Google's homepage.
Look at this again.
- -(Nsurlrequest *) connection: (Nsurlconnection *) connection willsendrequest: (nsurlrequest *) Request Redirectresponse :(Nsurlresponse *) response
This method is called before the request will be sent out.
The return value is a nsurlrequest is the one that is really going to be sent
The second parameter, request, is the requested URL in this case after being redirected.
The third parameter, response, is a request that will trigger a redirect
Here's a look at the URL of the request and response.
and return directly to request
Run to see the results
- ====will Send request====
- http://www.google.com/
- ====redirect response====
- (NULL)
- ====will Send request====
- Http://www.google.com.hk/url? sa=p&hl=zh-cn&pref=hkredirect&pval=yes&q=/http/ www.google.com.hk/&ust=1347589441099727&usg=AFQJCNESBYCRLH_JQPBWRWSC0NPBJ2_LFG
- ====redirect response====
- http://www.google.com/
- ====will Send request====
- http://www.google.com.hk/
- ====redirect response====
- Http://www.google.com.hk/url? sa=p&hl=zh-cn&pref=hkredirect&pval=yes&q=/http/ www.google.com.hk/&ust=1347589441099727&usg=AFQJCNESBYCRLH_JQPBWRWSC0NPBJ2_LFG
You can see that two redirects have been made
First time
- ====will Send request====
- http://www.google.com/
- ====redirect response====
- (NULL)
REDIRECT response is null because it is the first call without redirection processing
And the request to be sent is www.google.com.
Second time
- ====will send request====
- http://www.google.com.hk/url? Sa=p&hl=zh-CN&< span class= "attribute" >pref=hkredirect&pval=yes&q=http:// Www.google.com.hk/&ust=1347589441099727&usg=AFQJCNESBYCRLH_JQPBWRWSC0NPBJ2_LFG&NBSP;&NBSP;
- ====redirect RESPONSE====&NBSP;&NBSP;
- http://www.google.com/
This time redirect response is not NULL is the first time the request that is to say this time the redirect is caused by the www.google.com this URL
And the result of redirection is http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://. Www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg
Third time
[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
- ====will send request====
- http://www.google.com.hk/
- ====redirect RESPONSE====&NBSP;&NBSP;
- http://www.google.com.hk/url?p&hl=zh-CN&< span class= "attribute" >pref=hkredirect&pval=yes&q=http:// Www.google.com.hk/&ust=1347589441099727&usg=AFQJCNESBYCRLH_JQPBWRWSC0NPBJ2_LFG&NBSP;&NBSP;
Of course this http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http:// Www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg
This URL is not the familiar URL.
Because you have to redirect again.
This time, it's the same as this one. The long URL was in the redirect response, triggering the redirect.
Results can be seen
That's the familiar www.google.com.hk.
When you get the desired URL you can call [connection Cancel]; method to cancel the connection
and return nil.
How URL addresses in iOS apps redirect-B