Rick
Rick

Reputation: 1163

reload a UIwebview from main view automatically (view based application)

I have a view based application. Now on the opening view I have some buttons and a picture and a small web view.

The web view has its own .h/.m file the calls a JSON request to fill it. That works great.

My problem is that when the app is closed and reopened the webview is not updating. How to I get that to work?

welcomeMessage.m (connected to 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"];
}

Upvotes: 0

Views: 487

Answers (1)

Krumelur
Krumelur

Reputation: 33048

Check out Apple's documentation on an iOS's lifecycle. You will find all callbacks you need in there:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Especially – applicationWillEnterForeground: will be interesting for you.

Upvotes: 1

Related Questions