stringByEvaluatingJavaScriptFromString does nothin

2019-09-06 08:17发布

问题:

I'm using goNative.io to run my webapp in a wrapper/native iOS app. I'm adding bit of extra objective-c that reads the contents of HTML5 local storage and persists the data in the app when it's exited. Also, when the app opens back up I need to put that data back in local storage.

My issue is that stringByEvaluatingJavaScriptFromString:jsString does not seem to run any javascript in my app. I can try to alert or console.log something and absolutely nothing happens in UIWebView. No errors either.

Any ideas?

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");

    //Try to do anything in Javascript when app opens.. nothing happens!
    NSString *jsString = @"alert('Javascript wont run!!! Can't read local storage');";
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    //Persist local storage data
    NSLog(@"Persisting local storage since app will resign active");

    //Reading local storage item
    NSString *jsString = @"localStorage.getItem('mpo.subdomain');";
    NSString *someKeyValue = [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    NSLog(@"**** localStorage subdomain string: %@", someKeyValue);

    // Store your subdomain in ios persistent variable and use it later in the application
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:someKeyValue forKey:@"subdomain"];
    [userdefault synchronize];

    //use of User default
    NSLog(@"NSUserDefaults Subdomain %@",[userdefault valueForKey:@"subdomain"]);
}

回答1:

Your method stringByEvaluatingJavaScriptFromString will work only after the app run its web view delegate method "webViewDidFinishLoad". Until and unless your self.webview value would remain nil.

Try using below method.

  • (void)webViewDidFinishLoad:(UIWebView*)theWebView {

[theWebView stringByEvaluatingJavaScriptFromString:@"console.log('webViewDidFinishLoad');"];

}

And also make sure your "self.webview" is not nil.