当iOS应用进入后台时,请阅读本地存储



Objective-C对我来说是非常新的:

当我的iPhone应用移至背景时,下面的方法触发。在那一刻,我想在UIWebView的本地存储中获取这些项目并存储在应用中。

我会收到错误"No known class for selector 'stringByEvaluatingJavaScriptFromString:jsString'"。如何将此依赖性注入此功能,同时仍保持UIApplication

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    //Persist local storage data
    NSLog(@"Persisting local storage since app entering background");
    //Reading local storage item
    NSString *jsString = @"localStorage.getItem('mpo.subdomain');";
    NSString *someKeyValue = [UIWebView stringByEvaluatingJavaScriptFromString:jsString];
    // Store your subdomain in iPhone persistence variable and use it later in the application
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:someKeyValue forKey:@"subdomain"];
    [userdefault synchronize];
    //use of User default
    NSLog(@"User Subdomain %@",[userdefault valueForKey:@"subdomain"]);
}

代码[UIWebView stringByEvaluatingJavaScriptFromString:]试图在UIWebView类中调用称为stringByEvaluatingJavaScriptFromString的类方法。stringByEvaluatingJavaScriptFromString方法是一种实例方法。您需要将该消息发送到UIWebView的实例,而不是类。

如果您有属性mywebview,则使用代码:

[self.myWebView stringByEvaluatingJavaScriptFromString:]

stringByEvaluatingJavaScriptFromString消息发送到 self.myWebView属性指向的UIWebView的实例。那就是你想要的。

相关内容

  • 没有找到相关文章

最新更新