Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:可达性-startNotifier在返回应用程序后失败_Ios_Ios7_Reachability - Fatal编程技术网

iOS:可达性-startNotifier在返回应用程序后失败

iOS:可达性-startNotifier在返回应用程序后失败,ios,ios7,reachability,Ios,Ios7,Reachability,我的可达性工作完全如本文所建议的那样 我在用电脑。然而,我使用的不是块而是通知,因此这个过程与苹果的可达性代码非常相似 第一次启动应用程序时,我运行了这个程序,效果非常好 Reachability *reachability = [reach hostReachability]; [reachability startNotifier]; reachabilityChanged:事件正在触发: [[NSNotificationCenter defaultCenter] addObserver:

我的可达性工作完全如本文所建议的那样

我在用电脑。然而,我使用的不是块而是通知,因此这个过程与苹果的可达性代码非常相似

第一次启动应用程序时,我运行了这个程序,效果非常好

Reachability *reachability = [reach hostReachability];
[reachability startNotifier];
reachabilityChanged:事件正在触发:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachability_Changed:) name:kReachabilityChangedNotification object:nil];
但是,一旦我按下home(主页)按钮并返回应用程序,
startNotifier
会在内部返回“否”而不是“是”

    // 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;
因此,上述事件不会再次触发

除非我错误地使用了它,并且当可访问性被实例化时,
startNotifier
应该只在
init
中调用一次,而不再调用

self.hostReachability = [Reachability reachabilityWithHostname:_HOST];

您只需在init/load上调用一次
[self.hostReachability startNotifier]
。以下是使用通知而不是链接线程上的block方法的基本需求概述:

  • 将库添加到项目中

  • 为您的可达性对象创建属性以确保它被保留,例如

    @interface ViewController () {
      NSString *_HOST;
    }
    @property Reachability *hostReachability;
    @end
    
  • 注册更改通知,并启动通知程序,例如

    - (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];
    }
    
  • 最后,创建一个
    可达性更改:
    方法来处理您对可达性更改的响应,例如

    - (void)reachabilityChanged:(NSNotification*)notification
    {
      Reachability *notifier = [notification object];
      NSLog(@"%@", [notifier currentReachabilityString]);
    }
    
  • 注意:如果您按下Home(主页)按钮并卸载应用程序,可访问性的更改应在返回应用程序后立即发出通知