@@ -2,7 +2,7 @@ import WebKit
2
2
3
3
private let logger = USLogger ( #fileID)
4
4
5
- class ViewController : UIViewController , WKNavigationDelegate , WKScriptMessageHandler , UIDocumentPickerDelegate {
5
+ class ViewController : UIViewController , WKNavigationDelegate , WKScriptMessageHandlerWithReply , UIDocumentPickerDelegate {
6
6
7
7
@IBOutlet var webView : WKWebView !
8
8
@@ -13,7 +13,7 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan
13
13
// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler
14
14
configuration. setURLSchemeHandler ( USchemeHandler ( ) , forURLScheme: AppWebViewUrlScheme)
15
15
// https://developer.apple.com/documentation/webkit/wkusercontentcontroller
16
- configuration. userContentController. add ( self , name: " controller " )
16
+ configuration. userContentController. addScriptMessageHandler ( self , contentWorld : . page , name: " controller " )
17
17
// https://developer.apple.com/documentation/webkit/wkwebview
18
18
self . webView = WKWebView ( frame: . zero, configuration: configuration)
19
19
// https://developer.apple.com/documentation/webkit/wknavigationdelegate
@@ -40,12 +40,11 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan
40
40
webView. load ( URLRequest ( url: URL ( string: " \( AppWebViewUrlScheme) :/// " ) !) )
41
41
}
42
42
43
- func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
44
- let appVersion = Bundle . main. infoDictionary ? [ " CFBundleShortVersionString " ] as? String ?? " ?? "
45
- let buildNumber = Bundle . main. infoDictionary ? [ " CFBundleVersion " ] as? String ?? " ?? "
46
- webView. evaluateJavaScript ( " APP.printVersion('v \( appVersion) ', '( \( buildNumber) )') " )
47
- webView. evaluateJavaScript ( " APP.printDirectory(' \( getCurrentScriptsDirectoryString ( ) ) ') " )
48
- }
43
+ // https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455629-webview
44
+ // DOMContentLoaded
45
+ // func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
46
+ // webView.evaluateJavaScript("")
47
+ // }
49
48
50
49
func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Void ) {
51
50
if navigationAction. navigationType == . linkActivated {
@@ -79,32 +78,48 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan
79
78
decisionHandler ( . allow)
80
79
}
81
80
82
- func userContentController( _ userContentController: WKUserContentController , didReceive message: WKScriptMessage ) {
81
+ func userContentController( _ userContentController: WKUserContentController , didReceive message: WKScriptMessage ) async -> (
82
+ Any ? ,
83
+ String ?
84
+ ) {
83
85
guard let name = message. body as? String else {
84
86
logger? . error ( " \( #function, privacy: . public) - Userscripts iOS received a message without a name " )
85
- return
87
+ return ( nil , " bad message body " )
86
88
}
87
- if name == " CHANGE_DIRECTORY " {
89
+ switch name {
90
+ case " INIT " :
91
+ let appVersion = Bundle . main. infoDictionary ? [ " CFBundleShortVersionString " ] as? String ?? " 0.0.0 "
92
+ let buildNumber = Bundle . main. infoDictionary ? [ " CFBundleVersion " ] as? String ?? " 0 "
93
+ return ( [
94
+ " build " : buildNumber,
95
+ " version " : appVersion,
96
+ " directory " : getCurrentScriptsDirectoryString ( ) ,
97
+ ] , nil )
98
+ case " CHANGE_DIRECTORY " :
88
99
// https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories
89
100
logger? . info ( " \( #function, privacy: . public) - Userscripts iOS has requested to set the readLocation " )
90
101
let documentPicker = UIDocumentPickerViewController ( forOpeningContentTypes: [ . folder] )
91
102
documentPicker. delegate = self
92
103
documentPicker. directoryURL = getDocumentsDirectory ( )
93
104
present ( documentPicker, animated: true , completion: nil )
94
- }
95
- if name == " OPEN_DIRECTORY " {
105
+ break
106
+ case " OPEN_DIRECTORY " :
96
107
guard var components = URLComponents ( url: Preferences . scriptsDirectoryUrl, resolvingAgainstBaseURL: true ) else {
97
- return
108
+ return ( nil , " ScriptsDirectoryUrl malformed " )
98
109
}
99
110
components. scheme = " shareddocuments "
100
111
if let url = components. url, UIApplication . shared. canOpenURL ( url) {
101
- UIApplication . shared. open ( url)
112
+ await UIApplication . shared. open ( url)
102
113
}
114
+ break
115
+ default :
116
+ return ( nil , " Unexpected message body " )
103
117
}
118
+ return ( nil , nil )
104
119
}
105
120
106
121
func documentPicker( _ controller: UIDocumentPickerViewController , didPickDocumentAt url: URL ) {
107
122
Preferences . scriptsDirectoryUrl = url
108
- webView. evaluateJavaScript ( " APP.printDirectory ('\( getCurrentScriptsDirectoryString ( ) ) ') " )
123
+ webView. evaluateJavaScript ( " webapp.updateDirectory ('\( getCurrentScriptsDirectoryString ( ) ) ') " )
109
124
}
110
125
}
0 commit comments