📜 ⬆️ ⬇️

Tips & Tricks: Change User Agent on iPhone

As you know, the User Agent is required to “identify” the user, and more specifically, to determine which client or through which device the user comes to your site. Depending on this, you can "substitute" the necessary content (data, markup). This is relevant lately due to the large growth of mobile devices.

Continuing the theme of the iPhone Dev Tips & Tricks will talk about how to change the User Agent on the "iPhone".


')
In order to get some content that is sensitive to the User Agent (in my particular case, I want to get an XML file), you need to run the following code:

NSURL *url = [[NSURL alloc] initWithString:@"http://domain.com/SomeXml.xml"];
NSHTTPURLResponse *response;
NSError *error;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:@"Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)" forHTTPHeaderField:@"User-Agent"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


As you can see, to solve the problem, you must use setValue: forHTTPHeaderField for your request (NSMutableURLRequest).

In my specific task, I used NSData, since I needed to use the resulting XML for the NSXMLParser:

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
NSLog(@"XML is downloaded.");

Source: https://habr.com/ru/post/65376/


All Articles