user2386226
user2386226

Reputation:

How do I reload a webview once home button is pressed?

I want to reload my url once I press the home button. I tried the code mentioned in this thread: How to reload a UIWebView on HomeButton press But none of those refreshes/roloads my app once I return to the app from my main screen.Here's the code for it so far, any ideas what I am missing?

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //load url into webview
    NSString *strURL = @"http://myserver.com/firstpage";
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    self.webview.delegate = self;
    [self.webview loadRequest:urlRequest];


}

- (id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(reloadWebView:)
                                                     name:UIApplicationWillEnterForegroundNotification
                                                   object:nil];
        // Do some more stuff
    }
    return self;
}
- (void)reloadWebView:(NSNotification *)notification
{
    [webview reload];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.webview reload];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//No Internet Connection error code
-(void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"You have no internet connection!" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
    [alert show];

}
//Close app from AlertView
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    return;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 1

Views: 954

Answers (2)

leizh007
leizh007

Reputation: 19

reload the url in

- (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.
}

or in

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 40008

Put this code in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebView:) name:UIApplicationWillEnterForegroundNotification object:nil];

and this code in viewDidUnload

[[NSNotificationCenter defaultCenter] removeObserver:self];

Upvotes: 1

Related Questions