iOS Question Access Local Storage WKWebview

AneeshJay

Member
Licensed User
The Javascript library that I load in my webview uses the local storage to do some it's functions. In b4a, I give webview access to the local storage using the following code,
B4X:
Dim jwb As JavaObject = myWebview
Dim jset As JavaObject = jwb.RunMethod("getSettings", Null)
Dim r As Reflector
r.Target = jset

' Enable local storage
r.RunMethod2("setDomStorageEnabled", True, "java.lang.boolean")
' Disable cross-origin limits on File protocol
r.RunMethod2("setAllowFileAccess", True, "java.lang.boolean")
r.RunMethod2("setAllowFileAccessFromFileURLs", True, "java.lang.boolean")
r.RunMethod2("setAllowUniversalAccessFromFileURLs", True, "java.lang.boolean")

I couldn't figure out a way to do this in B4i. After doing some research online and in the forum, I got the following inline Obj-C code to enable the storage using websiteDataStore property.
Objective-C:
#If OBJC
#import <Foundation/Foundation.h>

- (WKWebViewConfiguration *) getconfig: ( WKWebViewConfiguration *)config
{
       WKUserContentController* userController = [[WKUserContentController alloc]init];
       [userController addScriptMessageHandler:self name:@"callback"];
    config.userContentController = userController;
    config.preferences.javaScriptEnabled = YES;
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.suppressesIncrementalRendering = YES;
    config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
 
  return config;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message     {
if ([message.name isEqualToString:@"callback"]) {
        [self.bi raiseEvent:nil event:@"jscallback:"params:@[message.body]];
}
}
#End If

I'm not sure whether this is right as the webview looks like it still doesn't have access to the browser's local storage.
Any help on this is much appreciated.

Thanks
 

AneeshJay

Member
Licensed User
It solved a different problem. Basically I'm trying to integrate my b4x POS app to a payment gateway provider to take payments through card terminals. The POS and the card terminal communicate with each other through cloud server facilitated by a javascript based library(provided by the payment gateway company). I have an HTML document that loads the Javascript library and has javascript helper functions to access the methods of the library. And I load this html file on to my webview to send transaction requests.

Previously before setting the file access, the webview used to load the payment page UI but wouldn't initiate the transaction(page was blank except for branding UI elements). Once I added the file access permission using your help here, I could straightaway see the library communicating with it's payment gateway servers but always returning a "Terminal not paired" error. I pair the POS with the card terminal using a weblink that I load on another WebView. The pairing is successful, but for some reason the whenever I send a transaction it comes up with same terminal not paired error. According to the payment gateway's documentation, the JS library uses the browser's local storage to store pairing info, which is used when sending any requests to the terminal.

The same integration works seamlessly on the B4a app but comes up with this error in b4i. Thinking that different webviews might have different local storages I tried running the pairing sequence and the transaction request on the same webview(without reinitializing) but no luck. All of this pointed to the fact that the webview still doesn't have access to the local storage.
 
Upvote 0
Top