iPhone User-Agent - iphone

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.

Related

UIWebView not loading mobile site

I know this was covered before, but none of the methods mentioned worked for me,
I need UIWebView to load a mobile website, but instead it returns desktop version
I tried
NSMutableURLRequest *stadiumURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com/"]];
[stadiumURLRequest setValue:#"Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3" forHTTPHeaderField:#"User-Agent"];
[stadiumWebView loadRequest:stadiumURLRequest];
and in the AppDelegate:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:#"mobileApp" forKey:NSHTTPCookieName];
[cookieProperties setObject:#"1" forKey:NSHTTPCookieValue];
[cookieProperties setObject:#"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:#"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:#"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:#"0" forKey:NSHTTPCookieVersion];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
Thanks
you dont have to add the headers, they are sent by default, also, see if it loads in safari and its not just the websites problem

How to initial a NSURLRequest with user agent

Usually the http user-agent is like:
Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341
If I
NSMutableURLRequest *newUserAgentRequest = (NSMutableURLRequest*)[NSURLRequest requestWithURL:self.url];
NSString *userAgent = [newUserAgentRequest valueForHTTPHeaderField:#"User-Agent"];
the userAgent is nil or empty means
NSMutableURLRequest *newUserAgentRequest = (NSMutableURLRequest*)[NSURLRequest requestWithURL:self.url];
no user-agent includes, how to initial a request with user agent?
I have only done it like the following. This is for iOS devices:
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:[yourURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSString *userAgent = [NSString stringWithFormat:#"%# %#",[UIDevice currentDevice].systemName,[UIDevice currentDevice].systemVersion];
[urlRequest setValue:userAgent forHTTPHeaderField:#"User-Agent"];
Hope it helps
Setting the User_Agent (with an underscore) field works for some, but not all websites, and isn't usually overridden by the NSURL... classes. The other alternative, besides messing with the dictionary (which I believe is not allowed, but I'll post an example anyhow), is method swizzling.
+ (void)initialize {
// Set user agent (the only problem is that we can't modify the User-Agent later in the program)
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#"Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
//only under MRC do we release [dictionnary release];
}
This works fine for me:
NSString *userAgent = #"My user agent";
NSURL *url = [NSURL URLWithString:#"http://127.0.0.1:9000"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:userAgent forHTTPHeaderField:#"User-Agent"];
The User-Agent is set later if you do not provide one yourself, so it is nil when you try to read it.
Why not subclass NSMutableURLRequest, and in the constructor of your subclass, simply set the "User-Agent" HTTPHeaderField. This will have a less pain point of you always remembering to set the header whenever creating an instance.

How to get/set user agent on iOS 4.x?

I have a webview app tool, which essentially consists of the webview and two buttons on a toolbar. One button to view the source of the page, and another button to view/change the current User Agent.
I have both functions working on iOS 5 (view source, and change User Agent), but I cant seem to grab the User Agent in iOS 4.x.
I'm using the following now:
userAgentViewController.UAText = [self.webView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
This works in iOS5, but in iOS 4.x, it doesnt return anything. Is there a way to achieve the same functionality in iOS 4.x?
Thank you!
to get the useragent, check the HTTP header in the NSURLRequest of your response.
You can retreive this one in the webViewDidFinishLoad delegate method.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%#", [[webView request] valueForHTTPHeaderField: #"User-Agent"]);
}
to set it, you have to custom an NSMutableURLRequest and give it to your webView
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:#"yourwebsite.com"]];
[request setHTTPMethod:#"POST"];
[request setValue:USERAGENT_STRING forHTTPHeaderField: #"User-Agent"];
[webView loadRequest:request];
[request release];
and that's it !
Try this in the AppDelegate.m
+ (void)initialize
{
// Set user agent (the only problem is that we can’t modify the User-Agent later in the program)
// iOS 5.1
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#”Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3”, #”UserAgent”, nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
}

How do I get the default user-agent string in an NSURLConnection?

I would like to append text to the default user-agent header in an NSURLConnection. I know how to change the user-agent of the NSURLConnection, but I don't see how to get the default user-agent. I tried the following:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSString *userAgent = [request valueForHTTPHeaderField:#"User-Agent"];
userAgent = [userAgent stringByAppendingString:extraUserAgentInfo];
[request addValue:userAgent forHTTPHeaderField:#"User-Agent"];
This does not work because userAgent is coming back nil from the valueForHTTPHeaderField: call.
Part of the default User-Agent is your AppName and Version:
My user agent is:
User-Agent foo-bar/1.0 CFNetwork/609.1.4 Darwin/12.4.0
foo-bar is the value of [[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleNameKey]
and
1.0 is the value of [[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleVersionKey]
There's a blog entry Changing the headers for UIWebKit HTTP requests from iCab. He suggest using a process call "Method Swizzling” to get what you want. But there is a rumor that Method swizzling cause for 4.0 rejection

UIWebView shows error when webpage is loaded, Safari shows perfectly

The problem is that the website doesn't fully load in a UIWebView, but it loads normally in Safari.
This is the error I get when loading the website in a UIWebView:
Warning:
cos_before_render(/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob)
[function.cos-before-render]: failed
to open stream: No such file or
directory in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 731
Warning: cos_before_render()
[function.include]: Failed opening
'/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob'
for inclusion
(include_path='.:/home/user/ctown/cti_bin/phplot:/usr/local/lib/php')
in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 731
Fatal error: Call to undefined
function: json_encode() in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 737
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
[super viewDidLoad];
}
I tried the question linked below, however I can't modify the backend of the website!
UIWebView Xhmtl parse error but safari don't
The error is not within your code! The UIWebView, especially from within the simulator uses a user agent something like this:
Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C134
When you call the url provided it results in the error you are seeing. It's a server side error.
You can alter your user agent by using this code, put it somewhere in the startup phase of your app so it's only set once:
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#"Safari/528.16", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[dictionnary release];
With this I was able to open your page. It looks like the site returns different markup for the iphone, so the result with the code above will show the website, which is a bit large for the small display.
UIWebView and Safari does not have the same user agent.
This answer won't fix your issue (see Nick's answer), but you should put [super viewDidLoad]; before you do anything else. So:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = #"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
}