【问题标题】:stringByEvaluatingJavaScriptFromString does nothingstringByEvaluatingJavaScriptFromString 什么都不做
【发布时间】:2017-04-18 01:00:42
【问题描述】:

我正在使用 goNative.io 在包装器/原生 iOS 应用程序中运行我的 web 应用程序。我添加了一些额外的objective-c,它读取HTML5本地存储的内容并在应用程序退出时将数据保留在应用程序中。此外,当应用程序重新打开时,我需要将这些数据放回本地存储中。

我的问题是stringByEvaluatingJavaScriptFromString:jsString 似乎没有在我的应用程序中运行任何 javascript。我可以尝试提醒或 console.log 一些东西,但在 UIWebView 中绝对没有发生任何事情。也没有错误。

有什么想法吗?

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

【问题讨论】:

    标签: javascript ios objective-c uiwebview


    【解决方案1】:

    您的方法 stringByEvaluatingJavaScriptFromString 仅在应用程序运行其 Web 视图委托方法“webViewDidFinishLoad”后才能工作。直到且除非您的 self.webview 值保持为零。

    试试下面的方法。

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

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

    }

    还要确保您的“self.webview”不为零。

    【讨论】:

      猜你喜欢
      • 2019-03-08
      • 2016-12-24
      • 2011-08-03
      • 2014-05-11
      • 2014-09-10
      • 1970-01-01
      • 2023-03-29
      • 2013-09-28
      • 2012-05-09
      相关资源
      最近更新 更多