我是 iphone 新手,我想知道如何在 xcode 中使用可达性。我继续阅读可达性示例并阅读它,但了解它。我创建了一个应用程序并将 Reachability.m 和 Reachability.h 放入其中,但我不知道如何使用它。
请指导我。我想在运行应用程序时随时检查我的网络连接并运行此代码:
if (isConnection)
{
NSLog(@"Connection Success")
}
else
NSLog(@"Connection has been lost")
我是 iphone 新手,我想知道如何在 xcode 中使用可达性。我继续阅读可达性示例并阅读它,但了解它。我创建了一个应用程序并将 Reachability.m 和 Reachability.h 放入其中,但我不知道如何使用它。
请指导我。我想在运行应用程序时随时检查我的网络连接并运行此代码:
if (isConnection)
{
NSLog(@"Connection Success")
}
else
NSLog(@"Connection has been lost")
你可以这样做:
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
现在internetStatus
通过检查它的值来检查 var。这些值定义为:
typedef enum
{
// Apple NetworkStatus Compatible Names.
NotReachable = 0,
ReachableViaWiFi = 2,
ReachableViaWWAN = 1
} NetworkStatus;
所以,在你的情况下:
if (internetStatus == NotReachable)
{
NSLog(@"Bazinga!");
}
else
{
NSLog(@"Houston we have ignition");
}
下载可达性类并遵循此代码
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
然后我们将设置在 Reachability 中创建的 NetworkStatus 变量。
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
最后,我们将在 switch 块中使用 netStatus。
switch (netStatus)
{
case ReachableViaWWAN:
{
break;
}
case ReachableViaWiFi:
{
break;
}
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
}
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case ReachableViaWWAN:
{
break;
}
case ReachableViaWiFi:
{
break;
}
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
}
}