Currently, I am utilizing goNative.io
to execute my web application within a wrapper/native iOS app. In order to enhance the functionality, I have incorporated some additional objective-c code that is responsible for extracting and storing data from HTML5 local storage when the app is closed, and then reinserting that data back into the local storage upon reopening.
The main issue I am facing is that the method
stringByEvaluatingJavaScriptFromString:jsString
does not appear to be executing any JavaScript code within my app. Even simple commands such as alert or console.log do not produce any outcomes in UIWebView
, without any error messages appearing either.
Any suggestions on how to resolve this?
appDelegate.h
#import <UIKit/UIKit.h>
#import <OneSignal/OneSignal.h>
#import "LEANWebViewController.h"
#import "LEANCastController.h"
#import "Reachability.h"
@interface LEANAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property LEANCastController *castController;
@property NSURLRequest *currentRequest;
@property Reachability *internetReachability;
@property (strong, nonatomic) OneSignal *oneSignal;
@property UIWebView *webView;
- (void)configureApplication;
@end
appDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//Persist local storage data
NSLog(@"Reading local storage since app is becoming active");
//Attempting to run Javascript upon app opening.. but no result!
NSString *jsString = @"alert('Javascript wont run!!! Can't read local storage');";
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Triggered when the application is transitioning from active to inactive state, like during incoming calls or when the user closes the app.
// This method can be used to pause ongoing tasks, stop timers, and limit OpenGL ES frame rates. Games typically use it to halt game activity.
//Persist local storage data
NSLog(@"Persisting local storage since app will resign active");
//Extracting an item from local storage
NSString *jsString = @"localStorage.getItem('mpo.subdomain');";
NSString *someKeyValue = [self.webView stringByEvaluatingJavaScriptFromString:jsString];
NSLog(@"**** localStorage subdomain string: %@", someKeyValue);
// Storing the obtained subdomain in a persistent iOS variable for future use in the app
NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
[userdefault setObject:someKeyValue forKey:@"subdomain"];
[userdefault synchronize];
//Utilizing User defaults
NSLog(@"NSUserDefaults Subdomain %@",[userdefault valueForKey:@"subdomain"]);
}