How to Request a Desktop Version of a Webpage Using Uiwebview in Swift 3.0

How can I request a desktop version of a webpage using UIWebView in Swift 3.0?

A WKWebView should solve your problem - you can define it in your code. Do this:

var webView : WKWebView!
override func loadView() {
super.loadView()

let config = WKWebViewConfiguration()
webView = WKWebView(frame: self.view.frame, configuration: config)
self.webView!.uiDelegate = self
webView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
//load URL here
let url = NSURL(string: "https://stackoverflow.com/")!
webView.load(URLRequest(url: url as URL))
self.view.addSubview(webView)


}

Request desktop site WKWebview not working in Swift 4

Here's my tested code:

class ViewController: UIViewController {

var webview: WKWebView?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

createWebView()
if let url = URL(string: "https://quora.com") {
load(url: url)
}
}

private func createWebView() {
let config = WKWebViewConfiguration()
let webview = WKWebView(frame: self.view.frame, configuration: config)
webview.uiDelegate = self
webview.navigationDelegate = self
self.webview = webview
self.view.addSubview(webview)
}

private func load(url: URL) {
var request = URLRequest(url: url)
let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
request.addValue(userAgent, forHTTPHeaderField: "User-Agent")
webview?.load(request)
}
}

extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("\(#function)")
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("\(#function): \(error)")
}
}

extension ViewController: WKUIDelegate {

}

And the resulting page:
iPhone 7Plus screenshot of WKWebview with Quora.com site

Cheers!

UIWebView load desktop version of website not Mobile version

It deppends on the web, but you can try to change the user agent to make the server think it's a desktop browser

To change the "UserAgent" default value run this code when your app starts:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

How to request desktop site in Swift programmatically (without UIWebView)?

Depends on server logic, but in most cases it's enough to set up userAgent Header in you request:

let url = URL(string:"https://google.com")!
var request = URLRequest(url: url)
let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.12 (KHTML, like Gecko) Version/11.1 Safari/605.1.12"
request.addValue(userAgent, forHTTPHeaderField: "User-Agent")

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// Process reponse
}
task.resume()

Force SFSafariViewController to load the desktop version of a website

As of iOS 9, sadly no. Your best bet might be to create your own web view and use a different user agent as @vizllx suggests in the comments.

UIWebView Desktop Website Won't Work

After checking user agent, https://web.whatsapp.com checks that client supports indexeddb, probably by some JavaScript. It's easy to notice - if you will trace all redirects your app performed, then you will find redirect to https://web.whatsapp.com/browsers.html?missing=indexeddb. In my case, I've used simple NSURLProtocol subclass to log all redirects. Sadly, NSURLProtocol no longer works with WKWebView.

UIWebView doesn't implement indexeddb at all. As far as I know, there is no easy way to implement indexeddb support myself.

You can easily check indexeddb support by accessing html5test.com in UIWebView. You will see something like:

html5test.com result for UIWebView

But WKWebView does support indexeddb:

html5test.com result for WKWebView

I've checked web.whatsapp.com loading in iOS WKWebView with custom user agent, taken from my desktop Safari using http://whatsmyuseragent.com, and it shows QR code as you expected. Moreover, migration to WKWebView will add some bonuses like improved performance and stability.

I've used following code for my test:

#import <WebKit/WebKit.h>

@interface ViewController () <WKNavigationDelegate>
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
self.webView.customUserAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17";
[self.view addSubview:self.webView];

self.webView.navigationDelegate = self;

NSURL *url = [NSURL URLWithString:@"http://web.whatsapp.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler {
decisionHandler(WKNavigationActionPolicyAllow);
}

@end

Although it's Objective-C, it should be pretty straightforward to convert into swift.

UPD:

Reason why you have blank view with WKWebView is transport security. Either use https://web.whatsapp.com as URL (note HTTPS instead of HTTP), or add NSAllowsArbitraryLoads: YES in your application info.plist

Force UIWebView to load mobile version of website, like Safari does

I forgot to set the constraints for the UIWebView, which caused the content to be rendered wrongly. I don't know how that's relevant, but constraining the UIWebView seems to not only render the content correctly but also display the mobile site.



Related Topics



Leave a reply



Submit