Iphone互联网连接(可达性)

编程入门 行业动态 更新时间:2024-10-16 00:25:29
本文介绍了Iphone互联网连接(可达性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看到任何有关可达性的帖子,但人们并没有真正给出问题的确切答案。 在我的应用程序中,我使用来自apple的可达性代码,在我的appDelegate中,我使用:

I saw any post about Reachability but people doesn't really give the exact answer to the problem. In my application I use the Reachability code from apple and in my appDelegate I use this:

-(BOOL)checkInternet { Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google"]; NetworkStatus internetStatus = [reachability currentReachabilityStatus]; BOOL internet; if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) { internet = NO; }else { internet = YES; } return internet; }

所以问题是即使我有互联网连接,这段代码告诉我我没有。 有谁知道该怎么做才能使这个工作?

So the problem is even if I have an internet connection, this code telling me that I don't have one. Does anyone know what to do to make this working?

谢谢,

推荐答案

您可能应该使用 + [Reachability reachabilityForInternetConnection] 而不是特定名称的可达性(当然除非您实际需要)。

You should probably be using +[Reachability reachabilityForInternetConnection] rather than reachability for a particular name (unless of course that's what you actually need).

毕竟,当你仍然有可用的互联网连接时,可能会出现各种原因导致特定服务器无法访问。

There could be all manner of reasons that a particular server might not be reachable while you still have a working internet connection, after all.

这就是我的工作:

BOOL hasInet; Reachability *connectionMonitor = [Reachability reachabilityForInternetConnection]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(inetAvailabilityChanged:) name: kReachabilityChangedNotification object: connectionMonitor]; hasInet = [connectionMonitor currentReachabilityStatus] != NotReachable;

然后

-(void)inetAvailabilityChanged:(NSNotification *)notice { Reachability *r = (Reachability *)[notice object]; hasInet = [r currentReachabilityStatus] != NotReachable; }

对我来说效果很好。

更多推荐

Iphone互联网连接(可达性)

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

发布评论

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

>www.elefans.com

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