[英]How to check internet connectivity in ios?
如何檢查應用程序是否已連接到互聯網? 當前,我在我的appdelegate.m
文件中使用此代碼
dispatch_queue_t connectivityThread = dispatch_queue_create("com.gm.kart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
if([GMMConnectivity hasConnectivity])
NSLog(@"%@", @"connected");
else
NSLog(@"Not connected");
usleep(10000000);
}
});
當我單擊登錄按鈕時,我想使用NSnotificationcenter
檢查互聯網是否已連接?
請幫我
下載后的例子如下。
http://developer.apple.com/iphone/library/samplecode/Reachability/index.html
您可以像下面的步驟一樣在項目中使用它:-
included Apple's Reachability.h & .m from their Reachability example.
add the SystemConfiguration framework.
將波紋管方法放入您的appdelegare.m文件中:-
- (BOOL) connectedToNetwork{
Reachability* reachability = [Reachability reachabilityWithHostName:@"google.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable)
{
isInternet =NO;
}
else if (remoteHostStatus == ReachableViaWWAN)
{
isInternet = TRUE;
}
else if (remoteHostStatus == ReachableViaWiFi)
{ isInternet = TRUE;
}
return isInternet;
}
isInternet是您的.h類的BOOL
根據您的代碼:-
dispatch_queue_t connectivityThread = dispatch_queue_create("com.GMM.assamkart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
isInternet =[self connectedToNetwork];
if (isInternet)
{
NSLog(@"connected");
}
else
{
NSLog(@"Not connected");
}
// usleep(10000000);
}
});
-(BOOL) connectedToInternet
{
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
return ( URLString != NULL ) ? YES : NO;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.