stringByEvaluatingJavaScriptFromString 什么都不做 [英] stringByEvaluatingJavaScriptFromString does nothing

查看:29
本文介绍了stringByEvaluatingJavaScriptFromString 什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的问题是 stringByEvaluatingJavaScriptFromString:jsString 似乎没有在我的应用程序中运行任何 javascript.我可以尝试警告或 console.log 一些东西,UIWebView 中绝对没有任何反应.也没有错误.

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.

有什么想法吗?

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

推荐答案

您的方法 stringByEvaluatingJavaScriptFromString 只有在应用运行其 Web 视图委托方法webViewDidFinishLoad"后才会起作用.直到并且除非您的 self.webview 值保持为零.

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.

尝试使用以下方法.

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

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

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

}

还要确保您的self.webview"不为零.

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

这篇关于stringByEvaluatingJavaScriptFromString 什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆