3

每当我的应用程序进入前台时,我都想刷新 UIWebView。我在 ViewController.m 中真正拥有的只是一个检查 Internet 访问 (hasInternet) 和 viewDidLoad 的方法。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webview;

-(BOOL)hasInternet{
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStats = [reach currentReachabilityStatus];

    if (internetStats == NotReachable) {
        UIAlertView *alertOne = [[UIAlertView alloc] initWithTitle:@"You're not connected to the internet." message:@"Please connect to the internet and restart the app." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alertOne show];
    }

    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self hasInternet];
    [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://warm-chamber-7399.herokuapp.com/"]] ];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

关于如何启用此功能的任何建议?它是进入 AppDelegate 还是我在 ViewController.m 中创建另一个方法?

4

2 回答 2

9

UIApplicationWillEnterForegroundNotification您应该在您ViewController的方法中注册 a ,viewDidLoad并且每当应用程序从后台返回时,您都可以在注册通知的方法中执行您想做的任何事情。ViewController当应用程序从后台返回到前台时,viewWillAppearviewDidAppear不会被调用。

-(void)viewDidLoad{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doYourStuff)

    name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)doYourStuff{

  [webview reload];
}

不要忘记取消注册您注册的通知。

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

请注意,如果您注册您viewControllerUIApplicationDidBecomeActiveNotification方法,那么每次您的应用程序激活时都会调用您的方法,注册此通知是不合适的。

于 2013-04-06T19:07:11.570 回答
0

注册 UIApplicationDidBecomeActiveNotification 或 UIApplicationWillEnterForegroundNotification。

于 2013-04-06T18:44:42.557 回答