Load desktop version of website in iphone - ios

It might seem duplicate to you, but as long as my problem is not solved I should ask here for help. I have tried previous given answers but nothing is working for me. So please help me out.
I want to force my UIWebView to load desktop versions of all websites(not the mobile version) and I am using this code, but still I am getting mobile versions of websites.
NSURL *urlValue = [NSURL URLWithString:#"any url here"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlValue];
NSString *newUserAgent = #"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7";
[request setValue:newUserAgent forHTTPHeaderField:#"User_Agent"];
[webView loadRequest:request];
There might be some problem with my User_Agent string. So your kind help is required.

Related

Set different User-Agents for multiple UIWebViews

My app has UIWebViews, some loading sites where I need the desktop version of the site, some loading sites where I need the mobile version of the site.
So I set the user-agent to a desktop user-agent or a mobile user-agent to achieve this.
For example if I want a UIWebView to load a site's Desktop version I will simply run this code right before:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
The problem is that this of course is setting a NSUserDefaults variable, meaning for any UIWebView in my app that loads after this it will use the desktop user agent. I can just flip flop the NSUserAgent as needed between desktop and mobile and load accordingly but sometimes my app requires TWO UIWebViews to load at the same time, one mobile, one desktop.
Is there any way I can set the user-agent of a UIWebView specific to said UIWebView?
I'm assuming this is going to involve swizzling NSUrlRequest methods or something? I know very little about that stuff
The easy way, if possible, is to just use WKWebView instead of UIWebView. WKWebView exposes the user agent string as a property on the class itself (.customUserAgent, iOS9+). As an added bonus, JavaScript code running in the view will be faster and will use less battery power because the JavaScript can be precompiled into native code.
That said, if you have to stick with UIWebView for some reason, you could create an NSURLProtocol that intercepts all your app's HTTP requests and changes the field depending on the hostname in the URL. There are some examples of an NSURLProtocol on Apple's website. Start with that and tweak as needed.

iOS User-Agent for Requesting a Desktop Site

I'm trying to request a desktop site using NSURLRequest. I found from How does Chrome's "Request Desktop Site" option work? that the solution seems to be to set the User-Agent header field to mimic a desktop User-Agent string. However, simply doing
NSURL *url = [NSURL URLWithString:#"http://www.facebook.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setValue:#"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.53.11 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10" forHTTPHeaderField:#"User-Agent"];
[_webView loadRequest:req];
[self.view addSubview:_webView];
doesn't seem to lead me anywhere. My question is is this a correct User-Agent string I can use? Or is perhaps the request overriding the headers sometime after this?
UPDATE:
I tried visiting http://www.useragentstring.com/ instead of facebook to see the User-Agent string at the time of the request. It seems that it's
Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H141
instead of the string I set. This leads me to think that the request is overriding the User-Agent sometime during the request.
The UIWebView you're using to send the request is adding its own user agent, overriding yours. To change the user agent for your web view, try this
Change User Agent in UIWebView (iPhone SDK)

(iOS) Will mucking about with my User-agent get my app rejected?

In my app, I use the following type of code to muck with the User-agent:
// Fake Firefox's user agent during web service's registration
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"Mozilla/5.0 (compatible; Windows; U; Windows NT 6.2; WOW64; en-US; rv:12.0) Gecko/20120403211507 Firefox/12.0", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
// Return to standard iOS user agent when web service registration is done
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
Part of my app allows the user to register for a web service using its "sign up for an account" page, and the only way I could find to prevent its website from automatically launching a page which says "DOWNLOAD OUR APP INSTEAD!" is to muck with the user-agent string thusly. I've tried setting the User-agent in a NSMutableURLRequest but I have NOT gotten this to work at all. (Yes I have tried setting both "User-Agent" and "User_Agent" in the NSMutableURLRequest.)
I have heard that this might get your app rejected from Apple. Can anyone confirm or deny, or offer any comment? Thanks!
There are multiple apps on the App Store that fake the user agent as well.
All workarounds for setting the language of NSLocalizedString set a custom value for an Apple key in the NSUserDefaults as well
So I don't think Apple will reject your app.
We use custom user agents in our apps as a way for our servers to know that our app is calling - and to pass extra data, for example, whether the app has a retina screen. As long as you don't rely on this for security it works well. Nothing has ever been rejected as a result.

Enable Cookies in UIWebView

How can I enable cookies in my iPhone Application that uses a UIWebView Window, so that my login system will work?
For sure start with
[NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy =
NSHTTPCookieAcceptPolicyAlways;
But, as mentioned by #JoelFan, the issue may be your User Agent string causing ASP.NET to attempt and fail at a cookieless login. Instead of a response that includes
Set-Cookie: .ASPXAUTH=really-long-hex-number
it returns a redirection to something like
Location: /(F(long-sorta-base64ish-looking-string))/
The default UIWebView user agent string is something like
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A501
but ASP.NET doesn't like this. Safari sends something like this:
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A501 Safari/9537.53
Do the following early on, maybe in your AppDelegate.m
// DON'T try to reuse a UIWebView for this.
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectZero];
// This webview has already decided to use the default user agent string.
// let's use javascript to get the existing user agent string
NSString *userAgent = [wv stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
// let's tack on some stuff to make ASP.NET happy
userAgent = [userAgent stringByAppendingString:#" Version/7.0 Safari/9537.53"];
[[NSUserDefaults standardUserDefaults] registerDefaults:#{#"UserAgent": userAgent}];
// New UIWebViews inited after here will use the user agent string you made.
If the site you are logging into is an ASP.NET site, the problem may be due to the UIWebView sending an unrecognized User Agent. See Change User Agent in UIWebView (iPhone SDK)

iOS: UIWebView full open source browser?

Does anyone know if there are any open source solutions out there that use UIWebview to build a full browser? There is something like this in Three20 when you pass a URL, but I am assuming there must be other alternatives out there.
I realize that UIWebView is a web browser, but hooking up refresh, back button, URL bar, etc will take extra time.
Suggestions?
SVWebViewController looks pretty much like what you're looking for.
I have started an open source project (MIT License) to make something as close as possible to the native MobileSafari application (on iPhone and iPad).
Here are the features so far :
Design close to Mobile Safari (iOS 4.x) native application (for both iPhone and iPad)
Bookmark support (support for folders in bookmarks not implemented yet)
Mail link support
Print web page support
Long tap handling (open or copy link) with customizable menu
Anyone wanting to contribute to this project is welcome to do it !
You can clone/fork the project here : https://github.com/sylverb/CIALBrowser
https://github.com/ghostery/banshee
EDIT the project is now maintained here: https://github.com/acatighera/banshee
It's an open source browser with tabs, bookmarks, search, etc.
UIWebView is a full browser ! To open a url in webView you do this -
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
You can even insert javascript into UIWebView. You could customize it to your liking.
//To customize the look & feel...
self.webView.scalesPageToFit = YES;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.webView.autoresizesSubviews = YES;
//To insert Javascript
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 0.5;"];
[self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
You could do lot more. Have fun...
UPDATE: To get a back button and all, webView provides those features, back, forward etc. all those browser features. You need to code up the buttons & UI & for code you could do this -
-(IBAction)goForward:(id)sender
{
[webView goForward];
}
-(IBAction)goBack:(id)sender
{
[webView goBack];
}
-(IBAction) gotoHome:(id)sender
{
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
You can also check out KINWebBrowser, a drop in web browser module for your apps.
https://github.com/dfmuir/KINWebBrowser
Features
iOS 7 & 8 support for iPhone and iPad
Customizable UI
Portrait and landscape orientation support
Use with existing UINavigationController or present modally
Load URL from NSURL or NSString
Delegate protocol for status callbacks
Action button to allow users to copy URL, share, or open in Safari & Google Chrome
Supports subclassing
Installation with CocoaPods

Resources