我有一个基于视图的应用程序。现在在开场视图中,我有一些按钮,一张图片和一个小网页视图。
Web视图有自己的.h / .m文件,调用JSON请求来填充它。这很好用。
我的问题是,当应用关闭并重新打开时,webview不会更新。如何让它工作?
welcomeMessage.m(连接到webview)
- (void)awakeFromNib{
[NSThread sleepForTimeInterval:1];
NSUserDefaults *gMess =[NSUserDefaults standardUserDefaults];
NSString *myMess=[gMess stringForKey:@"welcomeMessage"];
NSLog(@"WEBVIEW CLASS %@",myMess);
if (myMess == NULL) {
NSString *html = [NSString stringWithFormat:@"<body style ='background-color:#FFFF33' align='center'><p>Welcome</p><p>Check out our Daily Winners</p></body>"];
[welcomeMessage loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.myapp.com/api/welcome/welcomemessage.php?iappid=37"]];
}
else{
NSString *html = [NSString stringWithFormat:@"<body style ='background-color:#FFFF33' align='center'> %@ </body>", myMess];
[welcomeMessage loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.myapp.com/api/welcome/welcomemessage.php?iappid=37"]];
}
}
Mainviewcontroller json
- (void)viewDidLoad
{
// Create new SBJSON parser object
SBJsonParser *object = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.kickintheapp.com/api/welcome/welcomemessage.php?iappid=37"]];
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSError *jsonParseError;
NSDictionary *status = [object objectWithString:json_string error:&jsonParseError];
if (!status) {
// there's been a parse error; look at jsonParseError
// for example:
NSLog(@"JSON parse error: %@", jsonParseError);
}
NSString *messValue = [status objectForKey:@"message"];
NSUserDefaults *gMess = [NSUserDefaults standardUserDefaults];
[gMess setObject:messValue forKey:@"welcomeMessage"];
}
答案 0 :(得分:1)
查看Apple关于iOS生命周期的文档。你会在那里找到你需要的所有回调:
特别是– applicationWillEnterForeground:
会让你感兴趣。