3

Hy everyone.

I'm making an mobile app that use a WKWebView.

When I load some site in iPhone it returns the mobile website but when I try to load some site in iPad it request the desktop site and I want to make a button that switches between desktop and mobile site.

I try this on iPad but not success:

self.webView.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72"

like you can see I try to force the webView in iPad to use the iPhone userAgent but it keep requesting some desktop sites.

If you guys need more of my code I'll be happy to provide.

Thank you!

Mr. James
  • 456
  • 1
  • 5
  • 12

2 Answers2

1

you can try to subclass your webview like this:

class CustomWebView: WKWebView {
    override func loadRequest(request: NSURLRequest) -> WKNavigation? {
        guard let mutableRequest = request.mutableCopy() as? NSMutableURLRequest else {
            return super.loadRequest(request)
        }
        mutableRequest.setValue("Mozilla/5.0 (iPhone; CPU iPhone OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72", forHTTPHeaderField: "User-Agent")
        return super.loadRequest(mutableRequest)
    }
}

as proposed in this answer https://stackoverflow.com/a/37474812/2579725

that changes only the first user agent. what I did in the UIWebView to override it for every request was this:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSMutableURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    if ([request isKindOfClass:[NSMutableURLRequest class]])
    { 
         [request setValue:@"Mozilla/5.0 (iPhone; CPU iPhone OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Mobile/14B72" forHTTPHeaderField:@"User-Agent"];
    }
    return YES;
}

My research has given me the result that this way to do it is not possible in WKWebView.

Community
  • 1
  • 1
ben
  • 900
  • 9
  • 17
1

preferredContentMode is perfect for you ,

under iOS 13.0 or newer,

lazy var web: WKWebView = {
        let configuration = WKWebViewConfiguration()
        if #available(iOS 13.0, *) {
            configuration.defaultWebpagePreferences.preferredContentMode = .mobile
        }
        let vv = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
        vv.navigationDelegate = self
        return vv
    }()

You can check the result here:

 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        web.evaluateJavaScript("window.navigator.userAgent;") { res, err in
            if let word = res as? String{
                print(word)
            }
        }
        

  • the raw agent is

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko)


after set this

.preferredContentMode = .mobile

  • the new agent is

Mozilla/5.0 (iPad; CPU OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148}

DNG
  • 599
  • 4
  • 14