DoraemonKit -App info

作者: 谌文 | 来源:发表于2020-06-18 11:55 被阅读0次
    • 获取设备名称
    [UIDevice currentDevice].name
    
    • 获取当前系统版本号
    [UIDevice currentDevice].systemVersion
    
    • 获取手机型号
    + (NSString *)iphoneType{
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
        
        //iPhone
        if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
        if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
        if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
        if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
        if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
        if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
        if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
        if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
        if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
        if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
        if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
        if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
        if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
        if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
        if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6S";
        if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6S Plus";
        if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
        if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
        if ([platform isEqualToString:@"iPhone9,3"]) return @"iPhone 7";
        if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
        if ([platform isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus";
        if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
        if ([platform isEqualToString:@"iPhone10.4"]) return @"iPhone 8";
        if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
        if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
        if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
        if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
        if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
        if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
        if ([platform isEqualToString:@"iPhone11,4"]) return @"iPhone XS Max";
        if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS Max";
        if ([platform isEqualToString:@"iPhone12,1"]) return @"iPhone 11";
        if ([platform isEqualToString:@"iPhone12,3"]) return @"iPhone 11 Pro";
        if ([platform isEqualToString:@"iPhone12,5"]) return @"iPhone 11 Pro Max";
        
        return platform;
    }
    
    • 获取手机屏幕大小
    [UIScreen mainScreen].bounds.size.width
    [UIScreen mainScreen].bounds.size.height
    
    • 获取手机ipv4地址
    + (NSString *)getIPAddress:(BOOL)preferIPv4
    {
        NSArray *searchArray = preferIPv4 ?
                                @[ /*IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6,*/ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
                                @[ /*IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4,*/ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
    
        NSDictionary *addresses = [[self class] getIPAddresses];
        __block NSString *address;
        [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
            {
                address = addresses[key];
                if(address) *stop = YES;
            } ];
        return address ? address : @"0.0.0.0";
    }
    
    • 获取手机ipv6地址
    + (NSString *)getIPAddress:(BOOL)preferIPv4
    {
        NSArray *searchArray = preferIPv4 ?
                                @[ /*IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6,*/ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
                                @[ /*IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4,*/ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
    
        NSDictionary *addresses = [[self class] getIPAddresses];
        __block NSString *address;
        [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
            {
                address = addresses[key];
                if(address) *stop = YES;
            } ];
        return address ? address : @"0.0.0.0";
    }
    
    //获取所有相关IP信息
    + (NSDictionary *)getIPAddresses
    {
        NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
    
        // retrieve the current interfaces - returns 0 on success
        struct ifaddrs *interfaces;
        if(!getifaddrs(&interfaces)) {
            // Loop through linked list of interfaces
            struct ifaddrs *interface;
            for(interface=interfaces; interface; interface=interface->ifa_next) {
                if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
                    continue; // deeply nested code harder to read
                }
                const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
                char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
                if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
                    NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
                    NSString *type;
                    if(addr->sin_family == AF_INET) {
                        if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
                            type = IP_ADDR_IPv4;
                        }
                    } else {
                        const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
                        if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
                            type = IP_ADDR_IPv6;
                        }
                    }
                    if(type) {
                        NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
                        addresses[key] = [NSString stringWithUTF8String:addrBuf];
                    }
                }
            }
            // Free memory
            freeifaddrs(interfaces);
        }
        return [addresses count] ? addresses : nil;
    }
    
    • 设备是否模拟器
    #pragma mark 设备是否模拟器
    + (NSString *)deviceIdentifier {
        struct utsname systemInfo;
        uname(&systemInfo);
        return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    }
    
    + (BOOL)isSimulator {
        NSString *identifier = [self deviceIdentifier];
        return [identifier isEqualToString:@"i386"] || [identifier isEqualToString:@"x86_64"];
    }
    
    • 获取手机是否有地理位置权限
    + (NSString *)locationAuthority{
        NSString *authority = @"";    
        if ([CLLocationManager locationServicesEnabled]) {
            CLAuthorizationStatus state = [CLLocationManager authorizationStatus];
            if (state == kCLAuthorizationStatusNotDetermined) {
                authority = @"NotDetermined";
            }else if(state == kCLAuthorizationStatusRestricted){
                authority = @"Restricted";
            }else if(state == kCLAuthorizationStatusDenied){
                authority = @"Denied";
            }else if(state == kCLAuthorizationStatusAuthorizedAlways){
                authority = @"Always";
            }else if(state == kCLAuthorizationStatusAuthorizedWhenInUse){
                authority = @"WhenInUse";
            }
        }else{
            authority = @"NoEnabled";
        }
        return authority;
    }
    
    • 获取网络权限
        //获取网络权限
        if (@available(iOS 9.0, *)) {
            _cellularData = [[CTCellularData alloc]init];
            __weak typeof(self) weakSelf = self;
            _cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
                if (state == kCTCellularDataRestricted) {
                    weakSelf.authority = @"Restricted";
                }else if(state == kCTCellularDataNotRestricted){
                    weakSelf.authority = @"NotRestricted";
                }else{
                    weakSelf.authority = @"Unknown";
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableView reloadData];
                });
                
            };
        }
    
    • 获取push权限
    + (NSString *)pushAuthority{
        if ([[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIUserNotificationTypeNone) {
            return @"NO";
        }
        return @"YES";
    }
    
    • 获取拍照权限
    + (NSString *)cameraAuthority{
        NSString *authority = @"";
        NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
        switch (authStatus) {
            case AVAuthorizationStatusNotDetermined:
                authority = @"NotDetermined";
                break;
            case AVAuthorizationStatusRestricted:
                authority = @"Restricted";
                break;
            case AVAuthorizationStatusDenied:
                authority = @"Denied";
                break;
            case AVAuthorizationStatusAuthorized:
                authority = @"Authorized";
                break;
            default:
                break;
        }
        return authority;
    }
    
    • 获取麦克风权限
    + (NSString *)audioAuthority{
        NSString *authority = @"";
        NSString *mediaType = AVMediaTypeAudio;//读取媒体类型
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
        switch (authStatus) {
            case AVAuthorizationStatusNotDetermined:
                authority = @"NotDetermined";
                break;
            case AVAuthorizationStatusRestricted:
                authority = @"Restricted";
                break;
            case AVAuthorizationStatusDenied:
                authority = @"Denied";
                break;
            case AVAuthorizationStatusAuthorized:
                authority = @"Authorized";
                break;
            default:
                break;
        }
        return authority;
    }
    
    • 获取相册权限
    + (NSString *)photoAuthority{
        NSString *authority = @"";
        #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 //iOS 8.0以下使用AssetsLibrary.framework
        ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
        switch (status) {
            case ALAuthorizationStatusNotDetermined:    //用户还没有选择
            {
                authority = @"NotDetermined";
            }
                break;
            case ALAuthorizationStatusRestricted:       //家长控制
            {
                authority = @"Restricted";
            }
                break;
            case ALAuthorizationStatusDenied:           //用户拒绝
            {
                authority = @"Denied";
            }
                break;
            case ALAuthorizationStatusAuthorized:       //已授权
            {
                authority = @"Authorized";
            }
                break;
            default:
                break;
        }
        #else   //iOS 8.0以上使用Photos.framework
        PHAuthorizationStatus current = [PHPhotoLibrary authorizationStatus];
        switch (current) {
            case PHAuthorizationStatusNotDetermined:    //用户还没有选择(第一次)
            {
                authority = @"NotDetermined";
            }
                break;
            case PHAuthorizationStatusRestricted:       //家长控制
            {
                authority = @"Restricted";
            }
                break;
            case PHAuthorizationStatusDenied:           //用户拒绝
            {
                authority = @"Denied";
            }
                break;
            case PHAuthorizationStatusAuthorized:       //已授权
            {
                authority = @"Authorized";
            }
                break;
            default:
                break;
        }
        #endif
        return authority;
    }
    
    • 获取通讯录权限
    + (NSString *)addressAuthority{
        NSString *authority = @"";
        if (@available(iOS 9.0, *)) {//iOS9.0之后
            CNAuthorizationStatus authStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
            switch (authStatus) {
                case CNAuthorizationStatusAuthorized:
                    authority = @"Authorized";
                    break;
                case CNAuthorizationStatusDenied:
                {
                    authority = @"Denied";
                }
                    break;
                case CNAuthorizationStatusNotDetermined:
                {
                    authority = @"NotDetermined";
                }
                    break;
                case CNAuthorizationStatusRestricted:
                    authority = @"Restricted";
                    break;
            }
        }else{//iOS9.0之前
            ABAuthorizationStatus authorStatus = ABAddressBookGetAuthorizationStatus();
            switch (authorStatus) {
                case kABAuthorizationStatusAuthorized:
                    authority = @"Authorized";
                    break;
                case kABAuthorizationStatusDenied:
                {
                    authority = @"Denied";
                }
                    break;
                case kABAuthorizationStatusNotDetermined:
                {
                    authority = @"NotDetermined";
                }
                    break;
                case kABAuthorizationStatusRestricted:
                    authority = @"Restricted";
                    break;
                default:
                    break;
            }
        }
        return authority;
    }
    
    • 获取日历权限
    + (NSString *)calendarAuthority{
        NSString *authority = @"";
        EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
        switch (status) {
            case EKAuthorizationStatusNotDetermined:
                authority = @"NotDetermined";
                break;
            case EKAuthorizationStatusRestricted:
                authority = @"Restricted";
                break;
            case EKAuthorizationStatusDenied:
                authority = @"Denied";
                break;
            case EKAuthorizationStatusAuthorized:
                authority = @"Authorized";
                break;
            default:
                break;
        }
        return authority;
    }
    
    • 获取提醒事项权限
    + (NSString *)remindAuthority{
        NSString *authority = @"";
        EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
        switch (status) {
            case EKAuthorizationStatusNotDetermined:
                authority = @"NotDetermined";
                break;
            case EKAuthorizationStatusRestricted:
                authority = @"Restricted";
                break;
            case EKAuthorizationStatusDenied:
                authority = @"Denied";
                break;
            case EKAuthorizationStatusAuthorized:
                authority = @"Authorized";
                break;
            default:
                break;
        }
        return authority;
    }
    

    相关文章

      网友评论

        本文标题:DoraemonKit -App info

        本文链接:https://www.haomeiwen.com/subject/yftuxktx.html