@@ -45,6 +45,13 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
45
45
document.documentElement.appendChild(s);
46
46
"""
47
47
}
48
+ var cssrule2 : String {
49
+ return """
50
+ var s = document.createElement('style');
51
+ s.textContent = `body { font-size: ${thefontsize}px; }`;
52
+ document.documentElement.appendChild(s);
53
+ """
54
+ }
48
55
@IBOutlet weak var wv : WKWebView !
49
56
50
57
override init ( nibName nibNameOrNil: String ? , bundle nibBundleOrNil: Bundle ? ) {
@@ -136,15 +143,15 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
136
143
switch leak {
137
144
case true :
138
145
let config = self . wv. configuration
139
- config. userContentController. add ( self , name: " playbutton " )
146
+ config. userContentController. add ( self , contentWorld : . defaultClient , name: " playbutton " )
140
147
case false :
141
148
let config = self . wv. configuration
142
149
let handler = MyMessageHandler ( delegate: self )
143
- config. userContentController. add ( handler, name: " playbutton " )
150
+ config. userContentController. add ( handler, contentWorld : . defaultClient , name: " playbutton " )
144
151
}
145
152
146
- wv. configuration. applicationNameForUserAgent = " Version/1.0 MyShinyBrowser/1.0 " // not working, too late?
147
-
153
+ wv. configuration. applicationNameForUserAgent = " Version/13.1.2 Safari/605.1.15 " // not working, too late?
154
+ // so presumably if the web view comes from the storyboard, you must set this in the storyboard
148
155
149
156
wv. restorationIdentifier = " wv "
150
157
wv. scrollView. backgroundColor = . black // web view alone, ineffective
@@ -342,8 +349,9 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
342
349
if self . fontsize < 10 {
343
350
self . fontsize = 20
344
351
}
345
- let s = self . cssrule
346
- self . wv. evaluateJavaScript ( s)
352
+ // let s = self.cssrule
353
+ // self.wv.evaluateJavaScript(s, in: nil, in: .defaultClient)
354
+ self . wv. callAsyncJavaScript ( self . cssrule2, arguments: [ " thefontsize " : self . fontsize] , in: nil , in: . defaultClient)
347
355
}
348
356
349
357
deinit {
@@ -375,44 +383,47 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
375
383
print ( navigation. effectiveContentMode. rawValue) // 2, but I'm still not seeing the desktop version
376
384
}
377
385
378
- func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Swift . Void ) {
386
+ func webView( _ webView: WKWebView ,
387
+ decidePolicyFor navigationAction: WKNavigationAction ,
388
+ preferences: WKWebpagePreferences , decisionHandler: @escaping ( WKNavigationActionPolicy , WKWebpagePreferences ) -> Void ) {
379
389
print ( " old here " )
390
+ preferences. preferredContentMode = . desktop
391
+ print ( " asking for desktop version " )
392
+
380
393
if navigationAction. navigationType == . linkActivated {
381
394
if let url = navigationAction. request. url {
382
395
if url. scheme == " file " { // we do not scroll to anchor; bug in iOS 11?
383
- decisionHandler ( . allow)
396
+ decisionHandler ( . allow, preferences )
384
397
return
385
398
}
386
399
print ( " user would like to navigate to \( url) " )
387
400
var whichNav : Int { return 1 }
388
401
switch whichNav {
389
402
case 0 :
390
403
// this is how you would open in Mobile Safari
391
- if #available( iOS 10 . 0 , * ) {
392
- UIApplication . shared. open ( url)
393
- } else {
394
- UIApplication . shared. openURL ( url)
395
- }
396
- decisionHandler ( . cancel)
404
+ UIApplication . shared. open ( url)
405
+ decisionHandler ( . cancel, preferences)
397
406
return
398
407
case 1 :
399
408
// this is how to use the new Safari view controller
400
409
let svc = SFSafariViewController ( url: url)
401
410
self . safariurl = url
402
411
svc. restorationIdentifier = " sf " // doesn't help
403
412
svc. restorationClass = type ( of: self )
404
- // svc.delegate = self
413
+ svc. delegate = self
405
414
self . present ( svc, animated: true )
406
- decisionHandler ( . cancel)
415
+ decisionHandler ( . cancel, preferences)
416
+ return
417
+ default :
418
+ decisionHandler ( . allow, preferences)
407
419
return
408
- default : break
409
420
}
410
421
}
411
422
}
412
423
// must always call _something_
413
- decisionHandler ( . allow)
424
+ decisionHandler ( . allow, preferences )
414
425
}
415
-
426
+
416
427
func webViewWebContentProcessDidTerminate( _ webView: WKWebView ) { // new in iOS 9
417
428
print ( " process did terminate " ) // but I do not know under what circumstances this will actually be called
418
429
}
@@ -425,7 +436,10 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKScriptM
425
436
func safariViewController( _ controller: SFSafariViewController , didCompleteInitialLoad didLoadSuccessfully: Bool ) {
426
437
print ( " loaded svc " )
427
438
}
428
-
439
+
440
+ func safariViewControllerWillOpenInBrowser( _ controller: SFSafariViewController ) { // new in iOS 14
441
+ print ( " will open in browser " )
442
+ }
429
443
430
444
func userContentController( _ userContentController: WKUserContentController ,
431
445
didReceive message: WKScriptMessage ) {
0 commit comments