1

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.

4

1 回答 1

0

您可以使用下面的代码通过手动指定要使用的用户代理来确保设备正在发送用户代理。

修改后的报价来自: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];
}

注意:我列出的示例用户代理可能已经过时,您可能需要找到更新的用户代理。

于 2012-05-17T15:01:25.043 回答