Home > Software engineering >  WKWebView browser size doesn't match mobile safari
WKWebView browser size doesn't match mobile safari

Time:08-16

I am new to swiftui, and I am trying to create a full screen WKWebView. I am testing on an iPad Pro 12.9. When I load the page https://browsersize.com/ I get the follwing:

  • iOS app using WKWebView (code below) reports: 1024 x 768
  • Mobile Safari reports: 1366 x 917

When I check https://www.ios-resolution.com/ it appears that the device width for an iPad Pro should be 1366 rather than 1024. Why is the WKWebView giving a different width, and is there a way to change this to match mobile safari?

//  ContentView.swift

import SwiftUI
import WebKit

struct ContentView: View {
    private let urlString: String = "https://browsersize.com/"
    var body: some View {
        WebView(url: URL(string: urlString)!).ignoresSafeArea()
    }
}

struct WebView: UIViewRepresentable {
    var url: URL
    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }
    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This is related, but it didn't work in my case: How to set iOS WkWebview zoom scale

CodePudding user response:

I'm not sure which iPad Pro you're using to test, but when running your code through a iPad Pro (12.9-inch) simulator, I'm getting 1366x1024

You can also verify that size in xcode like this:

struct ContentView: View {
    private let urlString: String = "https://browsersize.com/"

    var body: some View {
        GeometryReader { geo in
            Button {
                print(geo.size) // (1366.0, 1024.0)
            } label: {
                WebView(
                    url: URL(string: urlString)!
                )
            }
        }
        .ignoresSafeArea()
    }
}

CodePudding user response:

The problem was solved when I changed "Launch Screen (Generation)" from Yes to No in the "info.plist Values" of the "Build Settings". The dimensions display correctly now.

  • Related