Post

Replies

Boosts

Views

Activity

Reply to Can a class instance in a View struct interact with the view's State variable?
Here's code for my completed use case: swift import SwiftUI import WebKit import WebView /* Using Swift Package Dependency: https://github.com/kylehickinson/SwiftUI-WebView */ struct ContentView: View { @StateObject private var webViewStore: WebViewStore @State private var hello = "SwiftUI world" class MyClass: NSObject, WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { if message.name == "hello" { print(message.body) /* this works */ /* but how do I change ContentView's hello State from here? */ } } } init() { let userContentController = WKUserContentController() let configuration = WKWebViewConfiguration() let userScript = WKUserScript( source: """ function elementReady(selector) { return new Promise((resolve, reject) = { let el = document.querySelector(selector) if (el) { resolve(el) } new MutationObserver((mutationRecords, observer) = { Array.from(document.querySelectorAll(selector)).forEach((element) = { resolve(element) observer.disconnect() }) }).observe(document.documentElement, { childList: true, subtree: true, }) }) } elementReady("h1").then((titleElement) = { window.webkit.messageHandlers.hello.postMessage(titleElement.textContent) }) """, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .defaultClient ) let myClass = MyClass() userContentController.add(myClass, contentWorld: .defaultClient, name: "hello") userContentController.addUserScript(userScript) configuration.userContentController = userContentController let webView = WKWebView(frame: .zero, configuration: configuration) _webViewStore = StateObject(wrappedValue: WebViewStore(webView: webView)) } var body: some View { Text("Hello \(hello)") WebView(webView: webViewStore.webView) .onAppear { webViewStore.webView.loadHTMLString("h1JS world/h1", baseURL: nil) /* I'll be loading any kind of webpage here */ } Button("load new world") { /* load new page with h1 tag */ webViewStore.webView.loadHTMLString("h1new world/h1", baseURL: nil) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Topic: Safari & Web SubTopic: General Tags:
Apr ’21
Reply to Do all iPad pros have faceid
No, the first two generations of iPad Pro’s didn’t. https://everymac.com/systems/apple/ipad/specs/apple-ipad-pro-9-7-inch-1st-gen-wi-fi-only-specs.html https://everymac.com/systems/apple/ipad/specs/apple-ipad-pro-10-5-inch-wi-fi-only-specs.html The 11’ iPad Pro was the first to support FaceID: https://everymac.com/systems/apple/ipad/specs/apple-ipad-pro-11-inch-a1980-wi-fi-only-specs.html
Apr ’22
Reply to Can a class instance in a View struct interact with the view's State variable?
Here's code for my completed use case: swift import SwiftUI import WebKit import WebView /* Using Swift Package Dependency: https://github.com/kylehickinson/SwiftUI-WebView */ struct ContentView: View { @StateObject private var webViewStore: WebViewStore @State private var hello = "SwiftUI world" class MyClass: NSObject, WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { if message.name == "hello" { print(message.body) /* this works */ /* but how do I change ContentView's hello State from here? */ } } } init() { let userContentController = WKUserContentController() let configuration = WKWebViewConfiguration() let userScript = WKUserScript( source: """ function elementReady(selector) { return new Promise((resolve, reject) = { let el = document.querySelector(selector) if (el) { resolve(el) } new MutationObserver((mutationRecords, observer) = { Array.from(document.querySelectorAll(selector)).forEach((element) = { resolve(element) observer.disconnect() }) }).observe(document.documentElement, { childList: true, subtree: true, }) }) } elementReady("h1").then((titleElement) = { window.webkit.messageHandlers.hello.postMessage(titleElement.textContent) }) """, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .defaultClient ) let myClass = MyClass() userContentController.add(myClass, contentWorld: .defaultClient, name: "hello") userContentController.addUserScript(userScript) configuration.userContentController = userContentController let webView = WKWebView(frame: .zero, configuration: configuration) _webViewStore = StateObject(wrappedValue: WebViewStore(webView: webView)) } var body: some View { Text("Hello \(hello)") WebView(webView: webViewStore.webView) .onAppear { webViewStore.webView.loadHTMLString("h1JS world/h1", baseURL: nil) /* I'll be loading any kind of webpage here */ } Button("load new world") { /* load new page with h1 tag */ webViewStore.webView.loadHTMLString("h1new world/h1", baseURL: nil) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Topic: Safari & Web SubTopic: General Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to Xcode 13.2 and autocompletion
I still have the same problem in the Developer website's Xcode 13.2 (13C90)
Replies
Boosts
Views
Activity
Dec ’21
Reply to Xcode 13.2 and autocompletion
Other autocompletions are also missing, String's init(contentsOf:) here for example: I've tried using DevCleaner.app and clearing out everything. Removed Xcode completely and reinstalled... still same problem.
Replies
Boosts
Views
Activity
Dec ’21
Reply to Do all iPad pros have faceid
No, the first two generations of iPad Pro’s didn’t. https://everymac.com/systems/apple/ipad/specs/apple-ipad-pro-9-7-inch-1st-gen-wi-fi-only-specs.html https://everymac.com/systems/apple/ipad/specs/apple-ipad-pro-10-5-inch-wi-fi-only-specs.html The 11’ iPad Pro was the first to support FaceID: https://everymac.com/systems/apple/ipad/specs/apple-ipad-pro-11-inch-a1980-wi-fi-only-specs.html
Replies
Boosts
Views
Activity
Apr ’22
Reply to Cloudkit dashboard won't load
Same problem here. Feedback sent: FB20993269
Replies
Boosts
Views
Activity
Nov ’25