swift UIでwebブラウザを制作しています。ブラウザの閲覧履歴をcore dataを使用して保存したいのですが、wkwebview上で保存しようとしたところ”Foundation._GenericObjCError.nilError”が発生しクラッシュしてしまいます。解決方法を教えて頂けないでしょうか?
ContentView.swift
swift
1import SwiftUI 2import WebKit 3import CoreData 4 5struct ContentView: View { 6 @Environment(\.managedObjectContext) private var viewContext 7 8 @FetchRequest( 9 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], 10 animation: .default) 11 private var items: FetchedResults<Item> 12 13 var body: some View { 14 NavigationView { 15 VStack{ 16 ZStack{ 17 VStack{ 18 Text("閲覧数") 19 20 Text(items.count.description) 21 } 22 NavigationLink(destination: WbView()) { 23 Text("web閲覧") 24 ) 25 } 26 } 27 NavigationLink(destination: HistoryView()) { 28 Text("履歴") 29 } 30 } 31 } 32 } 33} 34struct WbView: View { 35 @Environment(\.managedObjectContext) private var viewContext 36 var body: some View { 37 //WebView() 38 WebView(urlString: "https://www.google.com/") 39 .environment(\.managedObjectContext, viewContext) 40 } 41}
WebView.swift
swift
1import SwiftUI 2import WebKit 3import CoreData 4 5struct WebView: UIViewRepresentable { 6 var webView = WKWebView() 7 var urlString: String 8 9 class Coordinator: NSObject, WKUIDelegate, WKNavigationDelegate { 10 var parent: WebView 11 12 init(_ parent: WebView) { 13 self.parent = parent 14 } 15 16 func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) { 17 if let url = navigationAction.request.url?.absoluteString { 18 if (url.hasPrefix("https://")) { 19 decisionHandler(WKNavigationActionPolicy.allow) 20 } else { 21 guard let appStoreLink = URL(string: url) else { 22 return 23 } 24 UIApplication.shared.open(appStoreLink, options: [:], completionHandler: { (succes) in 25 }) 26 decisionHandler(WKNavigationActionPolicy.cancel) 27 } 28 } 29 } 30 // 表示しているページ情報 31 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 32 // 表示しているページのURL 33 let urlText = webView.url?.absoluteString ?? "" 34 // 表示しているページのタイトル 35 let titleText = webView.title ?? "" 36 37 let newItem = Item(context: viewContext) 38 newItem.timestamp = Date() 39 newItem.title = titleText 40 newItem.url = urlText 41 do { 42 try viewContext.save() 43 } catch { 44 // Replace this implementation with code to handle the error appropriately. 45 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 46 let nsError = error as NSError 47 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 48 } 49 } 50 } 51 52 func makeCoordinator() -> Coordinator { 53 Coordinator(self) 54 } 55 56 func makeUIView(context: Context) -> WKWebView { 57 let webView = WKWebView() 58 return webView 59 } 60 61 func updateUIView(_ webView: WKWebView, context: Context) { 62 webView.uiDelegate = context.coordinator 63 webView.navigationDelegate = context.coordinator 64 guard let url = URL(string: urlString) else { 65 return 66 } 67 let request = URLRequest(url: url) 68 webView.load(request) 69 } 70}
あなたの回答
tips
プレビュー