I am trying to develop safari extension using swift safari services. The project is created from mac os and then safari extension. I am javascript developer but developing safari extension needed to use swift but I do not have any idea on how to save data in safari local storage. I looked at the following documentation but could not benefit from it
https://developer.apple.com/documentation/safariservices/safari_app_extensions
here is the code
import SafariServices
class SafariExtensionViewController: SFSafariExtensionViewController {
@IBOutlet weak var passwordMessage: NSTextField!
@IBOutlet weak var emailMessage: NSTextField!
@IBOutlet weak var message: NSTextField!
@IBOutlet weak var email: NSTextField!
@IBOutlet weak var password: NSSecureTextField!
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
let providedPassword = password.stringValue
let isEmailAddressValid = isValidEmailAddress(emailAddressString: providedEmailAddress)
self.message.stringValue = ""
emailMessage.stringValue = ""
passwordMessage.stringValue = ""
if isEmailAddressValid && providedPassword.count > 0 {
let parameters = ["email": providedEmailAddress, "password": password.stringValue]
let URI = URL(string: "https://api.production.navihq.com/auth/sign_in")
let session = URLSession.shared
var request = URLRequest(url: URI!)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
if let httpResponse = response as? HTTPURLResponse {
if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
self.message.stringValue = "Login Successful"
// i want to store the email in safari local storage here
} else {
self.message.stringValue = "Invalid Credentials"
}
}
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
} else {
emailMessage.textColor = NSColor.red
emailMessage.stringValue = "Invalid Email"
}
}
}
Not: I am not using IOS. I am using safariservices.
Think you should use Messaging capability between Safari app extension and injected script. For example, send message from injected script to app ext. when DOMContentLoaded
triggered and send the answer to it with needed data (currently email) to set into local storage of the browser by injected JS.
Hope it helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.