Is there a way to set local storage in WKWebView
From @paulvs comment here is what I do.
Set navigation delegate to listen to finished callback.
webView.navigationDelegate = self
Then in the callback, check for value in localStorage and set if needed.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("localStorage.getItem(\"key\")") { (result, error) in
// check if result is what I want
// if it is what I want, do nothing
// if not set it
webView.evaluateJavaScript("localStorage.setItem(\"key\", \"value\")") { (result, error) in
webView.reload()
}
}
}
How do I get HTML5 localstorage data to the native code by cordova on ios?
yes it works on ios in javascript, to get the value :
window.localStorage.getItem("test");
more from cordova docs https://cordova.apache.org/docs/fr/latest/cordova/storage/localstorage/localstorage.html :
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
window.localStorage.setItem("key", "value");
var keyname = window.localStorage.key(i);
// keyname is now equal to "key"
var value = window.localStorage.getItem("key");
// value is now equal to "value"
window.localStorage.removeItem("key");
window.localStorage.setItem("key2", "value2");
window.localStorage.clear();
// localStorage is now empty
}
to read :
https://github.com/react-native-community/react-native-webview/issues/73
How to set the local storage before a UIWebView loading its initial request?
Read local storage data using WKWebView
You need to inject this script when the website has already loaded:
your WKWebView needs to assign the
navigationDelegate
webView.navigationDelegate = self
inject the script when the website was loaded completely
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//you might want to edit the script, with the escape characters
let script = "localStorage.getItem(\"token\")"
wkWebView.evaluateJavaScript(script) { (token, error) in
if let error = error {
print ("localStorage.getitem('token') failed due to \(error)")
assertionFailure()
}
print("token = \(token)")
}
}
How do I enable Local Storage in my WebKit-based application?
I submitted an app using this code to the Mac App Store, and Apple approved it:
Objective-C
WebPreferences* prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];
Swift 3
var prefs: WebPreferences? = webView.preferences
prefs?._setLocalStorageDatabasePath("~/Library/Application Support/MyApp")
prefs?.localStorageEnabled = true
Whether they will continue to approve that, I don't know, but they allowed it for my application as of 2011-01-29. Update: They also approved a version update to the same app, so it has gone through twice.
Loading HTML5 containing SVG in UIWebView whilst keeping localStorage available
I felt reinvigorated today, in large part thanks to @JimDusseau providing a novel attempt to solve the problem (I must confess I'd given up on getting this to work.) Unfortunately, Jim's approach works for the request, but it still had no effect on the response.
I then Googled for 2 hours trying to find other options, one of which was trying to associate a mime type with particular file extensions, etc.
All of this Googling proved fruitless, but it did trigger a vague memory of another file extension...
.xhtml
Could using this file extension cause the request to be handled with the appropriate mime type? My heart started pounding, I could feel the blood coursing through my veins, and, with great anticipation, I made the edit and rebuilt the project to find that [drumroll]...
YES! YES! YES! The iphone treats the response with the appropriate mime type AND keeps the document origin happy for localStorage.
Here's the working code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"xhtml"];
NSURL *baseURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[webView loadRequest:request];
Simple, yet elusive (until now.)
Related Topics
Increment Tab Bar Badge W/ Uialertaction Swift
Xcode 9: Swift Dependency Analysis Error
Subview Gesture Recognizer Not Being Called
Swift: Sort Array by Sort Descriptors
Swift 3 Filter Array of Dictionaries by String Value of Key in Dictionary
Square Video Using Avfoundation
How to Turn Off Core Data Write-Ahead Logging in Swift Using Options Dictionary
Swift - Update/Refresh Label That Displays Time
Swift: Nil Is Incompatible with Return Type String
Swift: Triggering Tableviewcell to Lead to a Link in a Uiwebview in Another Viewcontroller
Creating a Rtsp Client for Live Audio and Video Broadcasting in Objective C
Swift 2 - Separating an Array into a Dictionary with Keys from a to Z
Circular Button Becomes Rounded Rectangle After Size Increase
Animate a Change in Part of an Nsmutableattributedstring
Google Cloud Messaging Bridging Header Import Fails
Operation Went Isfinished=Yes Without Being Started by the Queue It Is In
Uitabbar Transition Issue Below iOS 11 Swift
Cannot Invoke 'Decode' with an Argument List of Type '(T, From: Data)'