0

I am using WebKit to store the email in the safari browser local storage. However, I am getting an error of

thread 3: Fatal error: Unexpectedly found nil while unwrapping an Optional value

One way to store value in localstorage is using UserDefaults but this way the value is not stored in safari local storage thus i used the below way

import SafariServices
import WebKit

class SafariExtensionViewController: SFSafariExtensionViewController {
    @IBOutlet weak var webView: WKWebView!
    static let shared = SafariExtensionViewController()
    override func viewDidLoad() {
        self.preferredContentSize = NSSize(width: 300, height: 250)
        message.stringValue = ""
        emailMessage.stringValue = ""
        passwordMessage.stringValue = ""
    }
    @IBAction func userLogin(_ sender: Any) {
     let providedEmailAddress = email.stringValue
      self.webView.evaluateJavaScript("localStorage.setItem(\"email\", \"value\")") { (result, error) in

         self.webView.reload()                           
     }
    }
}

my question is, how do i store the value in safari browser localstorage from swift so i can access that from javascript as well.

1 Answers1

1

You didn't connect webView outlet (that is marked as Implicitly Unwrapped Optional) from storyboard so you got this error.

You can use CoreData, UserDefaults, FileManager, even simple SQLite to store what you want.

EDIT:

A Safari app extension can read and modify your webpage content. Also it allows you to access data in your browser.

Complete the following steps to make Safari App Extension:

  1. Build a Safari App Extension
  2. Inject a script into a webpage
  3. Pass message between your Safari app extension and injected script
ezaji
  • 304
  • 2
  • 9
  • I am using xib file. I used UserDefaults but it does not store in the safari browser localstorage. I just want to store email in the browser localstorage so that I can use javascript to get the email in client side. – Tushant Khatiwada Sep 05 '18 at 04:26
  • Can you show me a sample code for storing email in safari browser localstorage not in app, please? – Tushant Khatiwada Sep 05 '18 at 04:28
  • You can't access resources outside your app because all iOS apps is protected by App Sandbox technology (https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AboutAppSandbox/AboutAppSandbox.html). So there is no way to store some data in safari browser localstorage from iOS app. – ezaji Sep 05 '18 at 04:30
  • I am not developing IOS app. I am totally new to this field. I have created a project from macos and then select the safari extension to create the browser extension. – Tushant Khatiwada Sep 05 '18 at 04:32
  • What I can suggest you is to use your backend to transfer data from iOS app and web app. – ezaji Sep 05 '18 at 04:32
  • Ok, sorry for misunderstanding. I think it might be useful for your issue: https://developer.apple.com/documentation/safariservices/safari_app_extensions/passing_messages_between_safari_app_extensions_and_injected_scripts – ezaji Sep 05 '18 at 04:37
  • Start from [Building a Safari App Extension](https://developer.apple.com/documentation/safariservices/safari_app_extensions/building_a_safari_app_extension). Then try to [pass message between your extension and web app](https://developer.apple.com/documentation/safariservices/safari_app_extensions/passing_messages_between_safari_app_extensions_and_injected_scripts) – ezaji Sep 05 '18 at 04:41
  • This is what exactly what i was doing before looking your valuable comment and yes it worked. Because i have mentioned the terms "javascript" so can you please update your answer with the "message passing" comment so i can accept it as solution. Thanks a lot for your coordination. – Tushant Khatiwada Sep 05 '18 at 04:48
  • one question though, can i use safari.extension.dispatchMessage() anywhere? though safariservices is imported, it still says Use of unresolved identifier 'safari' – Tushant Khatiwada Sep 05 '18 at 04:52
  • you can use safari object only from script injected to your webpage by Safari app extension. – ezaji Sep 05 '18 at 06:13
  • can you help me at this question, please? https://stackoverflow.com/questions/52159268/load-xib-file-from-another-xib-programmatically – Tushant Khatiwada Sep 05 '18 at 06:30