iOS:可达性

编程入门 行业动态 更新时间:2024-10-24 17:21:39
本文介绍了iOS:可达性 - startNotifier在返回应用程序后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的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:

  • 添加 tonymillion / Reachability 库到您的项目。

    为你的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"; 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:可达性

    本文发布于:2023-11-27 19:45:05,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1639355.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:可达性   iOS

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!