问题描述
我的Reachability完全按照中的建议工作。
I have Reachability working exactly as suggested as in this thread.
我正在使用 。但是我没有使用块而是通知,因此该过程非常类似于Apple的Reachability代码。
I am using the open source Reachability. However I am not using blocks but notifications, hence the process is pretty similar to the Apple's Reachability code.
我第一次启动应用程序时,我运行它并且它可以工作大。
The first time I start the app, I run this and it works great.
Reachability *reachability = [reach hostReachability];
[reachability startNotifier];
reachabilityChanged:事件正在解雇:
The reachabilityChanged: event is firing:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachability_Changed:) name:kReachabilityChangedNotification object:nil];
然而,一旦我按下主页按钮并返回应用程序, startNotifier
在内部返回NO而不是YES。
However once I press the home button and come back to the app, the startNotifier
returns internally a NO instead of a YES.
// Set it as our reachability queue, which will retain the queue
if(!SCNetworkReachabilitySetDispatchQueue(self.reachabilityRef, self.reachabilitySerialQueue))
{
#ifdef DEBUG
NSLog(@"SCNetworkReachabilitySetDispatchQueue() failed: %s", SCErrorString(SCError()));
#endif
...
return NO;
因此上述事件再也不会被解雇。
and hence the event above is never fired again.
除非我错误地使用了这个并且 startNotifier
只能在实例化可达性时在 init
中调用一次再也不会?
Unless I am using this wrongly and startNotifier
should only be called once in init
when reachability is instantiated and never again?
self.hostReachability = [Reachability reachabilityWithHostname:_HOST];
推荐答案
您只需要拨打 [self.hostReachability startNotifier]
一次在init / load上。以下是基本需求的概要,使用通知而不是链接线程上的块方法:
You should only need to call [self.hostReachability startNotifier]
once on init/load. Here's a rundown of your basic needs, using notifications rather than the block method on the linked thread:
-
添加库到您的项目。
为你的Reachability对象创建属性以确保它被保留,例如。
Create property for your Reachability object to make sure it's retained, eg.
@interface ViewController () {
NSString *_HOST;
}
@property Reachability *hostReachability;
@end
注册变更通知,并启动通知程序,例如。
Register for change notifications, and start the notifier, eg.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
_HOST = @"www.google.com";
self.hostReachability = [Reachability reachabilityWithHostname:_HOST];
[self.hostReachability startNotifier];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
最后,创建一个 reachabilityChanged:
处理对可达性更改的响应的方法,例如。
Finally, create a reachabilityChanged:
method to handle your response to Reachability changes, eg.
- (void)reachabilityChanged:(NSNotification*)notification
{
Reachability *notifier = [notification object];
NSLog(@"%@", [notifier currentReachabilityString]);
}
注意:如果你按Home键并卸载应用程序,Reachability中的更改应在返回应用程序后立即触发通知。
Note: If you press the Home button and unload the app, changes in Reachability should fire a notification immediately upon returning to the app.
这篇关于iOS:可达性 - startNotifier在返回应用程序后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!