由於項目需求,需要像伺服器上傳客戶端的機型信息,方便反饋查詢錯誤。 1.UUID uuid並不唯一,但也是目前用的最多的唯一標識符,我們可以將其存入鑰匙串中,以保證用戶在卸載或升級系統時仍保證其唯一性。 2.設備型號 3.設備系統版本號 4.運營商信息 關...
由於項目需求,需要像伺服器上傳客戶端的機型信息,方便反饋查詢錯誤。
1.UUID
uuid並不唯一,但也是目前用的最多的唯一標識符,我們可以將其存入鑰匙串中,以保證用戶在卸載或升級系統時仍保證其唯一性。
"CHKeychain.h" 可以在keychain中存取數據,使用類似於NSUserDefault
#pragma mark--獲取設備UUID後存入keyChain中
NSString * GetUUID () {
if ([CHKeychain load:UUIDKEY]) {
NSString *result = [CHKeychain load:UUIDKEY];
return result;
}else {
CFUUIDRef puuid = CFUUIDCreate( nil );
CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
CFRelease(puuid);
CFRelease(uuidString);
NSMutableString * tmpResult = result.mutableCopy;
NSRange range = [tmpResult rangeOfString:@"-"];
while (range.location != NSNotFound) {
[tmpResult deleteCharactersInRange:range];
range = [tmpResult rangeOfString:@"-"];
}
[CHKeychain save:UUIDKEY data:tmpResult.copy];
return tmpResult.copy;
}
return nil;
}
2.設備型號
+ (NSString *)platform {
struct utsname systemInfo;
uname(&systemInfo);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
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:@"iPhone3,3"]) 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:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1G";
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3";
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3";
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3";
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4";
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4";
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4";
if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air";
if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air";
if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air";
if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini 2G";
if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini 2G";
if ([platform isEqualToString:@"iPad4,6"]) return @"iPad Mini 2G";
if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"iPhone Simulator";
return platform;
}
3.設備系統版本號
[[UIDevice currentDevice] systemVersion]]
4.運營商信息
關於獲取運營商信息,需通過CoreTelephony Framework中的CTTelephonyNetworkInfo和CTCarrier類型。這些都在iOS 4.0後就有了。
import必要的header:
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
CTCarrier類型代表著具體的運營商信息。調用CTTelephonyNetworkInfo的subscriberCellularProvider方法來獲取當前運營商信息,或者調用subscriberCellularProviderDidUpdateNotifier方法來覺察運營商變化。
獲取了CTCarrier類型,就可以執行從他的屬性中獲取運營商信息了。
目前他有如下屬性:allowsVOIP,carrierName,isoCountryCode,mobileCountryCode ,mobileNetworkCode。參考官方文檔。
其中isoCountryCode使用ISO 3166-1標準,參考:3166
mobileCountryCode(MCC)和mobileNetworkCode(MNC)可以參考:3166-1
中國的MCC是460。中國的MNC也在列表中,如下表:
MNC | 運營商 |
---|---|
00 | 中國移動 |
01 | 中國聯通 |
02 | 中國移動 |
03 | 中國電信 |
05 | 中國電信 |
06 | 中國聯通 |
07 | 中國移動 |
20 | 中國鐵通 |
獲取運營商實例代碼
-(void)getcarrierName{
CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
NSString *currentCountry=[carrier carrierName];
NSLog(@"[carrier isoCountryCode]==%@,[carrier allowsVOIP]=%d,[carrier mobileCountryCode=%@,[carrier mobileCountryCode]=%@",[carrier isoCountryCode],[carrier allowsVOIP],[carrier mobileCountryCode],[carrier mobileNetworkCode]);
}
附:
CHKeychain.h文件
//
// CHKeychain.h
// G100
//
// Created by Tilink on 15/4/3.
// Copyright (c) 2015年 Tilink. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CHKeychain : NSObject
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)deleteData:(NSString *)service;
@end
> CHKeychain.m文件
//
// CHKeychain.m
// G100
//
// Created by Tilink on 15/4/3.
// Copyright (c) 2015年 Tilink. All rights reserved.
//
#import "CHKeychain.h"
@implementation CHKeychain
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
service, (id)kSecAttrService,
service, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString *)service data:(id)data {
//Get search dictionary
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Delete old item before add new item
SecItemDelete((CFDictionaryRef)keychainQuery);
//Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Configure the search setting
//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
+ (void)deleteData:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
@end
有多少人和我一樣,坐在不足10平米的空間里,看著書里九萬五千公裡的絢麗。又或是和我一樣,擁有一顆比九萬五千公裡還遼闊的心,卻坐在不足一平米的椅子上。