[英]How to use Reachability in xcode for assessment Internet Connection
我是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];
然后,我們將設置在可達性中創建的NetworkStatus變量。
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
最后,我們將在開關塊中使用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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.