How URL addresses in iOS apps redirect-B

Source: Internet
Author: User

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

    1. -(Nsurlrequest *) connection: (Nsurlconnection *) connection willsendrequest: (nsurlrequest *) Request Redirectresponse :(Nsurlresponse *) response

URLs that can be redirected later

Look at the code

  1. #import "ViewController.h"
  2. @interface Viewcontroller ()
  3. @end
  4. @implementation Viewcontroller
  5. -(void) Viewdidload {
  6. [Super Viewdidload];
  7. Nsurl *url = [Nsurl urlwithstring:@ "http://www.google.com"];
  8. Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
  9. Nsurlconnection *connection = [[Nsurlconnection alloc] initwithrequest:request delegate:self];
  10. if (!connection) {
  11. NSLog (@ "FAIL");
  12. }
  13. }
  14. -(Nsurlrequest *) connection: (Nsurlconnection *) connection willsendrequest: (nsurlrequest *) Request Redirectresponse :(Nsurlresponse *) Response {
  15. NSLog (@ "================================================");
  16. NSLog (@ "would send request\n%@", [request URL]);
  17. NSLog (@ "redirect response\n%@", [response URL]);
  18. return request;
  19. }
  20. @end


The above omitted some irrelevant code

Explain the code.

One connection in the Viewdidload method is Google's homepage.

Look at this again.


    1. -(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

  1. ====will Send request====
  2. http://www.google.com/
  3. ====redirect response====
  4. (NULL)
  5. ====will Send request====
  6. 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
  7. ====redirect response====
  8. http://www.google.com/
  9. ====will Send request====
  10. http://www.google.com.hk/
  11. ====redirect response====
  12. 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


    1. ====will Send request====
    2. http://www.google.com/
    3. ====redirect response====
    4. (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


    1. ====will send request====   
    2. 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;
    3. ====redirect RESPONSE====&NBSP;&NBSP;
    4. 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" >
    1. ====will send request====   
    2. http://www.google.com.hk/  
    3. ====redirect RESPONSE====&NBSP;&NBSP;
    4. 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

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.