简体   繁体   中英

iPhone User-Agent

I'm getching content in a universal iOS app using:

NSString stringWithContentsOfURL: [ NSURL URLWithString: link ]

... does that send iPhone/iPad user's IP and User-Agent to the server from which its fetching content from?

I need it to send at least user-agent so I can detect whether its an iPhone or iPad and optimize the content accordingly.

You can use the code below to make sure that the devices is sending an user agent by manually specifying which one to use.

Modified quote from: https://stackoverflow.com/a/1533032/716216

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
    NSString* userAgent = @"Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10";
    NSURL* url = [NSURL URLWithString:@"http://www.myWebSite.com/"];
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
                                    autorelease];
    [request setValue:userAgent forHTTPHeaderField:@"User-Agent"];

    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
}

NOTE: The sample user agent I've listed may be dated, and you MAY need to find a newer one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM