Hello, I have an app that only contains a WKWebView and loads an URL at startup and that is almost all. It has been working fine for years but for about a month (I guess) some clients reported only whitescreen comes up when the app is opened.
I have no idea what has changed and since it does not happen any of my device I cannot investigate what is wrong.
I've been reported that it shows white screen on iPhone X (iOS 16.5.2) and also on iPhone 14 Pro Max as well. But it is running completely fine most of the devices, including my iPhone 11.
The whole code is like this:
//
// ViewController.swift
//
import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
var request: URLRequest!
@IBOutlet var webViewContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NSLog("View loading...")
// 1
let contentController = WKUserContentController();
contentController.add(
self,
name: "route"
)
// 2
let config = WKWebViewConfiguration()
config.userContentController = contentController
let url = URL(string: "https://www.someurl.com/m/?app=ios")!
let request = URLRequest(url: url)
webViewContainer = UIView()
// 3
webView = WKWebView(frame: webViewContainer.bounds, configuration: config)
webView.allowsBackForwardNavigationGestures = false
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
webView.leadingAnchor.constraint(equalTo: webViewContainer.leadingAnchor, constant: 0).isActive = true
webView.trailingAnchor.constraint(equalTo: webViewContainer.trailingAnchor, constant: 0).isActive = true
webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor, constant: 0).isActive = true
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor, constant: 0).isActive = true
/*
DispatchQueue.main.async {
self.webView.load(request)
}
*/
webView.load(request)
view = webViewContainer
NSLog("View did loaded.")
}
@available(iOS 10.0, *)
func startNavigation(dict: NSDictionary) {
let lat = dict["lat"] as? Double ?? 41.0000
let lng = dict["lng"] as? Double ?? 28.000
openMaps(latitude: lat, longitude: lng, title: "Hakan Altın")
}
@available(iOS 10.0, *)
func openMaps(latitude: Double, longitude: Double, title: String?) {
let application = UIApplication.shared
let coordinate = "\(latitude),\(longitude)"
let encodedTitle = title?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
let handlers = [
("Apple Maps","http://maps.apple.com/?q=\(encodedTitle)&ll=\(coordinate)"),
("Google Maps", "comgooglemaps://?q=\(coordinate)"),
("Yandex Maps","yandexmaps://build_route_on_map/?lat_to=\(latitude)&lon_to=\(longitude)"),
("Waze", "waze://?ll=\(coordinate)"),
("Citymapper", "citymapper://directions?endcoord=\(coordinate)&endname=\(encodedTitle)")
]
.compactMap { (name, address) in URL(string: address).map { (name, $0) } }
.filter { (_, url) in application.canOpenURL(url) }
NSLog("Navigation apps count:\(handlers.count)")
guard handlers.count > 1 else {
if let (_, url) = handlers.first {
application.open(url, options: [:])
}
return
}
let alert = UIAlertController(title: "Please choose navigation application", message: nil, preferredStyle: .actionSheet)
handlers.forEach { (name, url) in
alert.addAction(UIAlertAction(title: name, style: .default) { _ in
application.open(url, options: [:])
})
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
extension ViewController:WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
NSLog("Command received from browser")
if message.name == "route", let dict = message.body as? NSDictionary {
if #available(iOS 10.0, *) {
startNavigation(dict: dict)
} else {
// Fallback on earlier versions
}
} else {
NSLog("Unknown browser command?!");
}
}
}
//
// AppDelegate.swift
//
import UIKit
@available(iOS 13.0, *)
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
Note: I've also replaced the URL content with only a red-background TEST page in order to eliminate if any javascript error cause the issue but yet clients still get all white blank screen.
Any ideas?