ios - 可达性不起作用
<p><p>我目前正在编写一个需要互联网连接(TCP 到服务器)的游戏。我正在尝试使用 Apple 的 Reachability 类,但它不起作用。我是 NotificationCenter 和 Reachability 的新手,所以如果这是一个愚蠢的问题,请原谅。</p>
<p><strong>LoginViewController.h 中与可达性相关的 Prop </strong></p>
<pre><code>@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;
@property BOOL internetActive;
@property BOOL hostActive;
</code></pre>
<p><strong>在 LoginViewController.m 中</strong></p>
<pre><code>//Reachability
[ addObserver: self selector: @selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];
NSString* hostIP = @"<my server IP is here, removed for obvious reasons>";
hostReachability = ;
;
NetworkStatus hostStatus = ;
if(hostStatus == NotReachable) {NSLog(@"no");}
else if (hostStatus == ReachableViaWiFi) {NSLog(@"wifi"); }
else if (hostStatus == ReachableViaWWAN) {NSLog(@"cell"); }
hostReachability = ;
;
NetworkStatus internetStatus = ;
if(internetStatus == NotReachable) {NSLog(@"no");}
else if (internetStatus == ReachableViaWiFi) {NSLog(@"wifi"); }
else if (internetStatus == ReachableViaWWAN) {NSLog(@"cell"); }
</code></pre>
<p><strong>handleNetworkChange:过程</strong></p>
<pre><code>- (void)handleNetworkChange:(NSNotification*)notification{
NetworkStatus internetStatus = ;
switch (internetStatus) {
case NotReachable:
{
NSLog(@"No internet connection");
self.internetActive = NO;
UIAlertView *alert = [ initWithTitle:@"No internet connection"
message:@"Could not connect to the internet. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
;
}
break;
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.IN AGENDA");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.IN AGENDA");
self.internetActive = YES;
break;
}
default:
break;
}
NetworkStatus hostStatus = ;
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.IN AGENDA");
self.hostActive = NO;
NSLog(@"No internet connection");
UIAlertView *alert = [ initWithTitle:@"No connection to host"
message:@"Could not connect to the host server. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.IN AGENDA");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.IN AGENDA");
self.hostActive = YES;
break;
}
}
</code></pre>
<p>运行应用程序时,无论我是否实际连接到互联网,以及服务器软件是否正在运行,我总是在终端中收到“no no”。</p>
<p>提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这里的主要问题是您的代码中的拼写错误; <strong>LoginViewController.m</strong> 中的 <code>hostReachability = ;</code> 应该是 <code>internetReachability = ;</code>。目前的方式是,您永远不会实例化 <code>internetReachability</code>,然后我猜 <code></code> 将始终返回 <code>nil</code>。</p>
<p>以下是您可以使用的基本示例:</p>
<pre><code>@interface LoginViewController ()
// ...
@property (strong, nonatomic) Reachability *hostReachability;
@property (strong, nonatomic) Reachability *internetReachability;
@property (strong, nonatomic) Reachability *wifiReachability;
@end
@implementation LoginViewController
// ...
- (void)setupReachabilityMonitoring
{
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
_hostReachability = ;
;
_internetReachability = ;
;
_wifiReachability = ;
;
;
}
- (void)reachabilityChanged:(NSNotification *)notification
{
if () {
;
}
}
- (void)logReachabilityStatus
{
NSString *hostStatus = _hostReachability.currentReachabilityStatus ? @"up" : @"down";
NSString *internetStatus = _internetReachability.currentReachabilityStatus ? @"up" : @"down";
NSString *wifiStatus = _wifiReachability.currentReachabilityStatus ? @"up" : @"down";
NSLog(@"Internet is %@, wifi is %@, host is %@", internetStatus, wifiStatus, hostStatus);
}
@end
</code></pre></p>
<p style="font-size: 20px;">关于ios - 可达性不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22810545/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22810545/
</a>
</p>
页:
[1]