IOS判断设备是否联网

1.导入SystemConfiguration.framework,并#import<SystemConfiguration/SCNetworkReachability.h>

+ (BOOL)connectedToNetwork
{
       //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态

       struct sockaddr_storage zeroAddress;//IP地址

       bzero(&zeroAddress, sizeof(zeroAddress));//将地址转换为0.0.0.0
       zeroAddress.ss_len = sizeof(zeroAddress);//地址长度
       zeroAddress.ss_family = AF_INET;//地址类型为UDP, TCP, etc.

       // Recover reachability flags
       SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
       SCNetworkReachabilityFlags flags;

       //获得连接的标志
       BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
       CFRelease(defaultRouteReachability);

       //如果不能获取连接标志,则不能连接网络,直接返回
       if (!didRetrieveFlags)
          {
            return NO;
        }
       //根据获得的连接标志进行判断

       BOOL isReachable = flags & kSCNetworkFlagsReachable;
       BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
       return (isReachable&&!needsConnection) ? YES : NO;

    /*
     1、sockaddr_storage
     This structure stores socket address information. Because this structure is large enough to store IPv4 or IPv6 address information, its use promotes protocol-family and protocol-version independence, and simplifies cross-platform development. Use this structure in place of the sockaddr structure.

     2、
     extern void bzero(void *s, int n);
     用法:#include <string.h>
     功能:置字节字符串s的前n个字节为零且包括‘\0’。
     说明:bzero无返回值,并且使用string.h头文件,string.h曾经是posix标准的一部分,但是在POSIX.1-2001标准里面,
     这些函数被标记为了遗留函数而不推荐使用。在POSIX.1-2008标准里已经没有这些函数了。推荐使用memset替代bzero。

     bzero( &tt, sizeof( tt ) );      //等价于memset(&tt,0,sizeof(tt));
     bzero( s, 20 );                  //等价于memset(s,0,20);

     */
}

2.  使用第三方类库请求网络,

#import "Reachability.h"

-(BOOL)isConnectionAvailable{

    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            //NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //NSLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            //NSLog(@"3G");
            break;
    }

    return isExistenceNetwork;
}
时间: 2024-10-18 00:00:41

IOS判断设备是否联网的相关文章

iOS 判断设备型号的三种方式

方法一: //***宏定义 #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO) 方法二: 还有做适配,代码中不要写320和480什么的初始化坐标,要用屏幕高度和宽度初始化#define Screen_height 

iOS 判断设备是否越狱了

1 #import "PrisonBreakCheck.h" 2 3 @implementation PrisonBreakCheck 4 /** 5 * 判断iPhone是否越狱了 6 */ 7 +(BOOL)checkPrisonBreak 8 { 9 //获取当前环境参数 10 char *env = getenv("DYLD_INSERT_LIBRARIES"); 11 NSLog(@"%s", env); 12 //如果env 不是空

iOS 判断设备为iPhone还是iPad

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.viewController = [[[_ViewController alloc] initWithNibName:@"_ViewController_iPhone" bundle:nil] autorelease]; } else { self.viewController = [[[_ViewControll

IOS判断设备是否已越狱

转自:http://www.cnblogs.com/supercheng/archive/2012/12/05/2804166.html - (BOOL)isJailbroken { BOOL jailbroken = NO; NSString *cydiaPath = @"/Applications/Cydia.app"; NSString *aptPath = @"/private/var/lib/apt/"; if ([[NSFileManagerdefaul

iOS 判断设备是否越狱

@interface UIDevice (Helper) - (BOOL)isJailbroken; @end @implementation UIDevice (Helper) - (BOOL)isJailbroken { BOOL jailbroken = NO; NSString *cydiaPath = @"/Applications/Cydia.app"; NSString *aptPath = @"/private/var/lib/apt/"; if (

ios判断设备是iphone还是ipad

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application

js判断设备是iOS还是安卓

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <title></title> <style&g

【ios开发】 判断设备屏幕尺寸、分辨率

IOS 设备现有的分辨率如下:iPhone/iPod Touch普通屏                          320像素 x 480像素       iPhone 1.3G.3GS,iPod Touch 1.2.33:2 Retina 屏           640像素 x 960像素        iPhone 4.4S,iPod Touch 416:9 Retina 屏               640像素 x 1136像素      iPhone 5,iPod Touch 5

判断设备

思考一个问题,如何让一个二维码在用不同的设备扫描时进入不一样的地址呢(听起来像不像隔空取物)?前端js可以实现吗?答案是ok的,目前找到一种办法,分享一下. 核心就是利用userAgent来判断设备,那什么是userAgent?简单的说,User Agent就是用来识别浏览器名称.版本.引擎以及操作系统等信息的内容.于是乎,User Agent的判断就成为识别浏览器的关键,不仅仅如此,移动互联网开发势头迅猛,那么通过User Agent判断桌面端设备或移动设备就变的很为重要. 首先写一个空页面里