码迷,mamicode.com
首页 > 移动开发 > 详细

ios 测试网络是否连接

时间:2016-06-12 00:10:33      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

转自:http://blog.csdn.net/lwq421336220/article/details/16982857

- (BOOL) connectedToNetwork

{

    //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态

   structsockaddr_in zeroAddress;

   bzero(&zeroAddress,sizeof(zeroAddress));

    zeroAddress.sin_len =sizeof(zeroAddress);

    zeroAddress.sin_family =AF_INET;

    

    

   SCNetworkReachabilityRef defaultRouteReachability =SCNetworkReachabilityCreateWithAddress(NULL, (structsockaddr *)&zeroAddress);

    SCNetworkReachabilityFlags flags;

    

   BOOL didRetrieveFlags =SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);

   CFRelease(defaultRouteReachability);

    

   if (!didRetrieveFlags)

    {

        printf("Error. Could not recover network reachability flagsn");

       returnNO;

    }

    

   BOOL isReachable = ((flags &kSCNetworkFlagsReachable) != 0);

   BOOL needsConnection = ((flags &kSCNetworkFlagsConnectionRequired) != 0);

   return (isReachable && !needsConnection) ?YES : NO;

}



使用的时候:

 

if ([selfconnectedToNetwork]) {

        

}else{

   UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"提示" message:@"网络连接失败,请查看网络连接。" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil,nil];

   [alertshow];

        

        

}

ios 测试网络是否连接

标签:

原文地址:http://www.cnblogs.com/feiyu-mdm/p/5576250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!