我正试图在我的应用程序中实现Reachability。我把它放在applicationDidFinishLaunching里了。如果我的连接非常糟糕,并且我启动了该应用程序,那么Reachability就会花费很长时间,并且经常会使用The application failed to launch in time错误消息使应用程序崩溃。
因此,我尝试将对Reachability的调用放在后台线程中。但是,当我这样做时,我将不再收到Reachability通知。
我该在哪里打电话给Reachability?
编辑:
我根据下面的建议添加了TonyMillions的Reachability代码,但在非常糟糕的网络条件下,我仍然会得到相同的application failed to load in time错误。要复制我看到的内容,请转到Settings.app -> Developer ->网络链接维护器,打开它并将其设置为100%的损失。
你的应用程序还在这种情况下加载吗?
发布于 2012-11-16 05:49:55
TonyMillion帮我在GitHub上解决了这个问题。
如果您正在使用他的Reachability替换,您所要做的就是像这样包装startNotifier调用:
dispatch_async(dispatch_get_global_queue(0,0), ^{
[self.internetReach startNotifier];
});该应用程序不会在糟糕的网络条件下启动,而且您仍然会在主线程上得到通知。
发布于 2012-11-07 15:14:16
使用托尼数百万可达项目上的吉突。
https://github.com/tonymillion/Reachability
然后,在我的应用程序代表中,我刚刚输入了应用程序,完成了启动
self.reachability = [Reachability reachabilityForInternetConnection];
[self.reachability startNotifier];然后当状态改变时,这将被调用
#pragma mark - Reachability
- (void)reachabilityChanged:(NSNotification *)note {
NetworkStatus ns = [(Reachability *)[note object] currentReachabilityStatus];
if (ns == NotReachable) {
if (![self.networkAlert isVisible]) {
if ([self networkAlert] == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\nNo Internet Connection" message:@"You require an internet connection to communicate with the server." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[self setNetworkAlert:alert];
}
[self.networkAlert show];
}
} else {
if ([self networkAlert] != nil) {
[self.networkAlert dismissWithClickedButtonIndex:0 animated:YES];
}
}
}发布于 2012-11-06 06:37:20
因此,我将对Reachability的调用放在后台线程中。但是,当我这样做时,我将不再收到Reachability通知。
当在后台线程中调用Reachability时,应该在后台run循环中调度Reachability,并确保后台线程run循环正在运行。
要在后台线程中执行:
这可能有效,但建议在主线程中调度通知程序。
https://stackoverflow.com/questions/13245285
复制相似问题