sha*_*aka 3 iphone objective-c ios
每当我的应用程序到达前台时,我想刷新UIWebView.我在ViewController.m中真正拥有的是一种检查互联网访问(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
Run Code Online (Sandbox Code Playgroud)
有关如何启用此功能的任何建议?它是在AppDelegate中还是在ViewController.m中创建另一个方法?
您应该UIApplicationWillEnterForegroundNotification
在自己ViewController
的viewDidLoad
方法中注册一个,每当应用程序从后台返回时,您可以在注册通知的方法中执行任何操作.ViewController
的viewWillAppear
或viewDidAppear
当应用程序回来,从后台到前台就不会被调用.
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doYourStuff)
name:UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)doYourStuff{
[webview reload];
}
Run Code Online (Sandbox Code Playgroud)
不要忘记取消注册您注册的通知.
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果您注册了viewController
for,UIApplicationDidBecomeActiveNotification
那么每次您的应用程序变为活动状态时都会调用您的方法,注册此通知是不合适的.
归档时间: |
|
查看次数: |
4948 次 |
最近记录: |