DataController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. //
  2. // DataController.m
  3. // CParam
  4. //
  5. // Created by breeze on 2017/6/20.
  6. // Copyright © 2017年 Breeze. All rights reserved.
  7. //
  8. #import "DataController.h"
  9. #import "IdentityData.h"
  10. #include <sys/socket.h>
  11. #include <sys/sysctl.h>
  12. #include <net/if.h>
  13. #include <net/if_dl.h>
  14. #import "SSKeychain.h"
  15. #import <UIKit/UIKit.h>
  16. #import <sys/utsname.h>
  17. #import <CoreTelephony/CTCarrier.h>
  18. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  19. #define kAppGIDFile @"appGID.dat"
  20. #define kServerIDFile @"appSID.dat"
  21. #define kDeviceUIDFile @"deviceUID.dat"
  22. #define kAppUIDFile @"appUID.dat"
  23. // 全局数据控制器
  24. static DataController *globalDataController = nil;
  25. @implementation DataController
  26. {
  27. IdentityData *identityData; // 身份数据
  28. }
  29. // 获取数据管理的控制器
  30. + (DataController *)getInstance
  31. {
  32. @synchronized(self)
  33. {
  34. // 实例对象只分配一次
  35. if(globalDataController == nil)
  36. {
  37. globalDataController = [[super allocWithZone:NULL] init];
  38. }
  39. }
  40. return globalDataController;
  41. }
  42. + (id)allocWithZone:(NSZone *)zone
  43. {
  44. return [self getInstance];
  45. }
  46. - (id)copyWithZone:(NSZone *)zone
  47. {
  48. return self;
  49. }
  50. // 初始化
  51. - (void)initIdentityData
  52. {
  53. if(identityData == nil)
  54. {
  55. identityData = [[IdentityData alloc] init];
  56. }
  57. }
  58. + (NSString*)getAppGroupID
  59. {
  60. //获取Bundle identifier
  61. NSDictionary *dic = [[NSBundle mainBundle] infoDictionary];
  62. NSString *appName = [dic objectForKey:@"CFBundleIdentifier"];
  63. // 查找第一个点
  64. NSRange range = [appName rangeOfString:@"."];
  65. // 取第一个点及后面的内容
  66. NSString* subString = [appName substringWithRange:NSMakeRange(range.location, [appName length]-range.location)];
  67. // 连接字符串
  68. NSString* appGroupName = [@"group" stringByAppendingString:subString];
  69. return appGroupName;
  70. }
  71. #pragma mark - 获取和设置程序用户ID
  72. // ===================================================================
  73. // 获取和设置程序用户ID
  74. // ===================================================================
  75. // 获取程序用户ID
  76. - (NSString *)appGID
  77. {
  78. [self initIdentityData];
  79. // 如果没有加载
  80. if([identityData appGID] == nil)
  81. {
  82. // 获取document文件夹位置
  83. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  84. NSString *documentDirectory = [paths objectAtIndex:0];
  85. // 加载GID文件
  86. NSString *appGIDPath = [documentDirectory stringByAppendingPathComponent: kAppGIDFile];
  87. // 该文件存在
  88. if([[NSFileManager defaultManager] fileExistsAtPath:appGIDPath])
  89. {
  90. NSString *appIDFromFile = [[NSString alloc] initWithContentsOfFile:appGIDPath
  91. encoding:NSUTF8StringEncoding
  92. error:nil];
  93. [self setAppGID:appIDFromFile];
  94. }
  95. // 该文件不存在,设置成默认值(空字符串)
  96. else
  97. {
  98. [self setAppGID:@""];
  99. }
  100. }
  101. return [identityData appGID];
  102. }
  103. // 设置程序用户ID
  104. - (void)setAppGID:(NSString *)appGIDNew
  105. {
  106. [self initIdentityData];
  107. [identityData setAppGID:appGIDNew];
  108. }
  109. // 获取程序用户UID
  110. - (NSString *)appUID
  111. {
  112. [self initIdentityData];
  113. // 如果没有加载
  114. if([identityData appUID] == nil)
  115. {
  116. // 获取document文件夹位置
  117. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  118. NSString *documentDirectory = [paths objectAtIndex:0];
  119. // 加载UID文件
  120. NSString *appUIDPath = [documentDirectory stringByAppendingPathComponent:kAppUIDFile];
  121. //获取sandbox的UID
  122. NSString *appIDFromFile = [[NSString alloc] initWithContentsOfFile:appUIDPath
  123. encoding:NSUTF8StringEncoding
  124. error:nil];
  125. //获取keychain的UID
  126. NSString *appIDFromKeychain = [SSKeychain passwordForService:@"com.brilliantAreo.userClient"account:@"iid"];
  127. NSString *uuidStr = nil;
  128. BOOL shouldSaveSandBox = NO;
  129. BOOL shouldSaveKeychain = NO;
  130. //同时存在
  131. if ((appIDFromFile.length > 0) && (appIDFromKeychain.length > 0))
  132. {
  133. uuidStr = appIDFromFile;
  134. if (![appIDFromFile isEqualToString:appIDFromKeychain])
  135. {
  136. shouldSaveKeychain = YES;
  137. }
  138. }
  139. //沙盒不存在,Keychain存在
  140. else if ((appIDFromFile.length == 0) && (appIDFromKeychain.length > 0))
  141. {
  142. uuidStr = appIDFromKeychain;
  143. shouldSaveSandBox = YES;
  144. }
  145. //沙盒存在,Keychain不存在
  146. else if ((appIDFromFile.length > 0) && (appIDFromKeychain.length == 0))
  147. {
  148. uuidStr = appIDFromFile;
  149. shouldSaveKeychain = YES;
  150. }
  151. //同时不存在
  152. else
  153. {
  154. if([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)])
  155. {
  156. uuidStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  157. shouldSaveKeychain = YES;
  158. shouldSaveSandBox = YES;
  159. }
  160. }
  161. [self setAppUID:uuidStr];//设置APPUID
  162. if (shouldSaveSandBox)
  163. {
  164. [self saveAppUID];//保存沙盒
  165. }
  166. if (shouldSaveKeychain)
  167. {
  168. [SSKeychain setPassword:uuidStr
  169. forService:@"com.brilliantAreo.userClient" account:@"iid"];//保存Keychain
  170. }
  171. }
  172. return [identityData appUID];
  173. }
  174. // 设置程序用户UID
  175. - (void)setAppUID:(NSString *)appUIDNew
  176. {
  177. [self initIdentityData];
  178. [identityData setAppUID:appUIDNew];
  179. }
  180. #pragma mark - 获取服务端分配的ServerID
  181. // ===================================================================
  182. // 获取和设置程序用户ID
  183. // ===================================================================
  184. // 获取程序ServerID
  185. - (NSString *)serverID
  186. {
  187. [self initIdentityData];
  188. // 如果没有加载
  189. if([identityData serverID] == nil)
  190. {
  191. // 获取document文件夹位置
  192. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  193. NSString *documentDirectory = [paths objectAtIndex:0];
  194. // 加载ServerID文件
  195. NSString *serverIDPath = [documentDirectory stringByAppendingPathComponent:kServerIDFile];
  196. // 该文件存在
  197. if([[NSFileManager defaultManager] fileExistsAtPath:serverIDPath])
  198. {
  199. NSString *serverIDFromFile = [[NSString alloc] initWithContentsOfFile:serverIDPath
  200. encoding:NSUTF8StringEncoding
  201. error:nil];
  202. [self setServerID:serverIDFromFile];
  203. }
  204. // 该文件不存在,设置成默认值(空字符串)
  205. else
  206. {
  207. [self setServerID:@""];
  208. }
  209. }
  210. return [identityData serverID];
  211. }
  212. // 设置程序用户ID
  213. - (void)setServerID:(NSString *)serverIDNew
  214. {
  215. [self initIdentityData];
  216. [identityData setServerID:serverIDNew];
  217. }
  218. #pragma mark - 获取和设置deviceUID
  219. // 获取UID
  220. - (NSString *)deviceUID
  221. {
  222. [self initIdentityData];
  223. // 如果没有加载
  224. if([identityData deviceUID] == nil)
  225. {
  226. // 获取document文件夹位置
  227. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  228. NSString *documentDirectory = [paths objectAtIndex:0];
  229. // 加载UID文件
  230. NSString *deviceUIDPath = [documentDirectory stringByAppendingPathComponent:kDeviceUIDFile];
  231. // 该文件存在
  232. if([[NSFileManager defaultManager] fileExistsAtPath:deviceUIDPath])
  233. {
  234. NSString *deviceUIDFromFile = [[NSString alloc] initWithContentsOfFile:deviceUIDPath
  235. encoding:NSUTF8StringEncoding
  236. error:nil];
  237. [self setDeviceUID:deviceUIDFromFile];
  238. }
  239. // 该文件不存在,则通过方式获取该唯一标识
  240. else
  241. {
  242. // 产生唯一标识
  243. CFUUIDRef puuid = CFUUIDCreate(nil);
  244. CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
  245. NSString * deviceID = (__bridge_transfer NSString *)CFStringCreateCopy(NULL, uuidString);
  246. CFRelease(puuid);
  247. CFRelease(uuidString);
  248. [self setDeviceUID:deviceID];
  249. // 保存到文件中
  250. [self saveDeviceUID];
  251. }
  252. }
  253. return [identityData deviceUID];
  254. }
  255. // 设置UID
  256. - (void)setDeviceUID:(NSString *)deviceUIDNew
  257. {
  258. [self initIdentityData];
  259. [identityData setDeviceUID:deviceUIDNew];
  260. }
  261. // 获取Mac地址
  262. // Return the local MAC addy
  263. // Courtesy of FreeBSD hackers email list
  264. // Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
  265. + (NSString *)macAddress
  266. {
  267. int mib[6];
  268. size_t len;
  269. char *buf;
  270. unsigned char *ptr;
  271. struct if_msghdr *ifm;
  272. struct sockaddr_dl *sdl;
  273. mib[0] = CTL_NET;
  274. mib[1] = AF_ROUTE;
  275. mib[2] = 0;
  276. mib[3] = AF_LINK;
  277. mib[4] = NET_RT_IFLIST;
  278. if ((mib[5] = if_nametoindex("en0")) == 0)
  279. {
  280. return @"";
  281. }
  282. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
  283. {
  284. return @"";
  285. }
  286. if ((buf = malloc(len)) == NULL)
  287. {
  288. return @"";
  289. }
  290. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)
  291. {
  292. free(buf);
  293. return @"";
  294. }
  295. ifm = (struct if_msghdr *)buf;
  296. sdl = (struct sockaddr_dl *)(ifm + 1);
  297. ptr = (unsigned char *)LLADDR(sdl);
  298. NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
  299. *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
  300. free(buf);
  301. return outstring;
  302. }
  303. // 获取platform
  304. + (NSString *)platform
  305. {
  306. size_t size;
  307. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  308. char *machine = malloc(size);
  309. sysctlbyname("hw.machine", machine, &size, NULL, 0);
  310. NSString *platform = [NSString stringWithCString:machine
  311. encoding:NSUTF8StringEncoding];
  312. free(machine);
  313. return platform;
  314. }
  315. - (NSString *)vendorUID
  316. {
  317. // UID
  318. #if TARGET_IPHONE_SIMULATOR
  319. NSString *deviceID = @"0000000000000000000000000000000000000000";
  320. #elif TARGET_OS_IPHONE
  321. NSString *deviceID = [DataController macAddress];
  322. if ([deviceID isEqualToString:@"02:00:00:00:00:00"] || (deviceID == nil))
  323. {
  324. return [self appUID];
  325. }
  326. #endif
  327. return deviceID;
  328. }
  329. #pragma mark - 保存
  330. // ===================================================================
  331. // 程序用户ID存储函数
  332. // ===================================================================
  333. // 保存AppID
  334. - (void)saveAppGID
  335. {
  336. if((identityData != nil) && ([identityData appGID] != nil))
  337. {
  338. // 获取document文件夹位置
  339. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  340. NSString *documentDirectory = [paths objectAtIndex:0];
  341. // 将GID数据写入文件
  342. NSString *appGIDPath = [documentDirectory stringByAppendingPathComponent:kAppGIDFile];
  343. [[identityData appGID] writeToFile:appGIDPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  344. if ([[NSFileManager defaultManager] respondsToSelector:@selector(containerURLForSecurityApplicationGroupIdentifier:)])
  345. {
  346. NSString *groupUrl = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[DataController getAppGroupID]] relativePath];
  347. [[identityData appGID] writeToFile:[groupUrl stringByAppendingPathComponent:kAppGIDFile] atomically:YES encoding:NSUTF8StringEncoding error:nil];
  348. }
  349. }
  350. }
  351. // 保存AppUID
  352. - (void)saveAppUID
  353. {
  354. if((identityData != nil) && ([identityData appUID] != nil))
  355. {
  356. // 获取document文件夹位置
  357. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  358. NSString *documentDirectory = [paths objectAtIndex:0];
  359. // 将UID数据写入文件
  360. NSString *appUIDPath = [documentDirectory stringByAppendingPathComponent:kAppUIDFile];
  361. [[identityData appUID] writeToFile:appUIDPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  362. if ([[NSFileManager defaultManager] respondsToSelector:@selector(containerURLForSecurityApplicationGroupIdentifier:)])
  363. {
  364. NSString *groupUrl = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[DataController getAppGroupID]] relativePath];
  365. [[identityData appUID] writeToFile:[groupUrl stringByAppendingPathComponent:kAppUIDFile] atomically:YES encoding:NSUTF8StringEncoding error:nil];
  366. }
  367. }
  368. }
  369. // 保存ServerID
  370. - (void)saveServerID
  371. {
  372. if((identityData != nil) && ([identityData serverID] != nil))
  373. {
  374. // 获取document文件夹位置
  375. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  376. NSString *documentDirectory = [paths objectAtIndex:0];
  377. // 将ServerID数据写入文件
  378. NSString *serverIDPath = [documentDirectory stringByAppendingPathComponent:kServerIDFile];
  379. [[identityData serverID] writeToFile:serverIDPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  380. if ([[NSFileManager defaultManager] respondsToSelector:@selector(containerURLForSecurityApplicationGroupIdentifier:)])
  381. {
  382. NSString *groupUrl = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[DataController getAppGroupID]] relativePath];
  383. [[identityData serverID] writeToFile:[groupUrl stringByAppendingPathComponent:kServerIDFile] atomically:YES encoding:NSUTF8StringEncoding error:nil];
  384. }
  385. }
  386. }
  387. // 保存UID
  388. - (void)saveDeviceUID
  389. {
  390. if((identityData != nil) && ([identityData deviceUID] != nil))
  391. {
  392. // 获取document文件夹位置
  393. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  394. NSString *documentDirectory = [paths objectAtIndex:0];
  395. // 将DeviceUID数据写入文件
  396. NSString *deviceUIDPath = [documentDirectory stringByAppendingPathComponent:kDeviceUIDFile];
  397. [[identityData deviceUID] writeToFile:deviceUIDPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  398. if ([[NSFileManager defaultManager] respondsToSelector:@selector(containerURLForSecurityApplicationGroupIdentifier:)])
  399. {
  400. NSString *groupUrl = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[DataController getAppGroupID]] relativePath];
  401. [[identityData deviceUID] writeToFile:[groupUrl stringByAppendingPathComponent:kDeviceUIDFile] atomically:YES encoding:NSUTF8StringEncoding error:nil];
  402. }
  403. }
  404. }
  405. // 系统型号
  406. + (NSString *)osVersion {
  407. return [[UIDevice currentDevice] systemVersion];
  408. }
  409. // 获取App的版本号,包含buildID, 如:1.0.0-3 其中1.0.0为appVersion,3位build ID
  410. + (NSString *)appVersion {
  411. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  412. NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
  413. if(appVersion.length > 0) {
  414. return [NSString stringWithFormat: @"%@-%@", appVersion, [self appBuildID]];
  415. }
  416. return @"";
  417. }
  418. // 获取build号
  419. + (NSString *)appBuildID {
  420. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  421. NSString *buildId = [infoDic objectForKey:@"CFBundleVersion"];
  422. if(buildId.length > 0) {
  423. return buildId;
  424. }
  425. return @"";
  426. }
  427. + (NSString *)deviceModelName
  428. {
  429. struct utsname systemInfo;
  430. uname(&systemInfo);
  431. NSString* code = [NSString stringWithCString:systemInfo.machine
  432. encoding:NSUTF8StringEncoding];
  433. static NSDictionary* deviceNamesByCode = nil;
  434. if (!deviceNamesByCode) {
  435. deviceNamesByCode = @{@"i386" : @"Simulator",
  436. @"x86_64" : @"Simulator",
  437. @"iPod1,1" : @"iPod Touch", // (Original)
  438. @"iPod2,1" : @"iPod Touch", // (Second Generation)
  439. @"iPod3,1" : @"iPod Touch", // (Third Generation)
  440. @"iPod4,1" : @"iPod Touch", // (Fourth Generation)
  441. @"iPod7,1" : @"iPod Touch", // (6th Generation)
  442. @"iPhone1,1" : @"iPhone", // (Original)
  443. @"iPhone1,2" : @"iPhone", // (3G)
  444. @"iPhone2,1" : @"iPhone", // (3GS)
  445. @"iPad1,1" : @"iPad", // (Original)
  446. @"iPad2,1" : @"iPad 2", //
  447. @"iPad3,1" : @"iPad", // (3rd Generation)
  448. @"iPhone3,1" : @"iPhone 4", // (GSM)
  449. @"iPhone3,3" : @"iPhone 4", // (CDMA/Verizon/Sprint)
  450. @"iPhone4,1" : @"iPhone 4S", //
  451. @"iPhone5,1" : @"iPhone 5", // (model A1428, AT&T/Canada)
  452. @"iPhone5,2" : @"iPhone 5", // (model A1429, everything else)
  453. @"iPad3,4" : @"iPad", // (4th Generation)
  454. @"iPad2,5" : @"iPad Mini", // (Original)
  455. @"iPhone5,3" : @"iPhone 5c", // (model A1456, A1532 | GSM)
  456. @"iPhone5,4" : @"iPhone 5c", // (model A1507, A1516, A1526 (China), A1529 | Global)
  457. @"iPhone6,1" : @"iPhone 5s", // (model A1433, A1533 | GSM)
  458. @"iPhone6,2" : @"iPhone 5s", // (model A1457, A1518, A1528 (China), A1530 | Global)
  459. @"iPhone7,1" : @"iPhone 6 Plus", //
  460. @"iPhone7,2" : @"iPhone 6", //
  461. @"iPhone8,1" : @"iPhone 6S", //
  462. @"iPhone8,2" : @"iPhone 6S Plus", //
  463. @"iPhone8,4" : @"iPhone SE", //
  464. @"iPhone9,1" : @"iPhone 7", //
  465. @"iPhone9,3" : @"iPhone 7", //
  466. @"iPhone9,2" : @"iPhone 7 Plus", //
  467. @"iPhone9,4" : @"iPhone 7 Plus", //
  468. @"iPhone10,1": @"iPhone 8", // CDMA
  469. @"iPhone10,4": @"iPhone 8", // GSM
  470. @"iPhone10,2": @"iPhone 8 Plus", // CDMA
  471. @"iPhone10,5": @"iPhone 8 Plus", // GSM
  472. @"iPhone10,3": @"iPhone X", // CDMA
  473. @"iPhone10,6": @"iPhone X", // GSM
  474. @"iPad4,1" : @"iPad Air", // 5th Generation iPad (iPad Air) - Wifi
  475. @"iPad4,2" : @"iPad Air", // 5th Generation iPad (iPad Air) - Cellular
  476. @"iPad4,4" : @"iPad Mini", // (2nd Generation iPad Mini - Wifi)
  477. @"iPad4,5" : @"iPad Mini", // (2nd Generation iPad Mini - Cellular)
  478. @"iPad4,7" : @"iPad Mini", // (3rd Generation iPad Mini - Wifi (model A1599))
  479. @"iPad6,7" : @"iPad Pro (12.9\")", // iPad Pro 12.9 inches - (model A1584)
  480. @"iPad6,8" : @"iPad Pro (12.9\")", // iPad Pro 12.9 inches - (model A1652)
  481. @"iPad6,3" : @"iPad Pro (9.7\")", // iPad Pro 9.7 inches - (model A1673)
  482. @"iPad6,4" : @"iPad Pro (9.7\")" // iPad Pro 9.7 inches - (models A1674 and A1675)
  483. };
  484. }
  485. NSString* deviceName = [deviceNamesByCode objectForKey:code];
  486. if (!deviceName) {
  487. // Not found on database. At least guess main device type from string contents:
  488. if ([code rangeOfString:@"iPod"].location != NSNotFound) {
  489. deviceName = @"iPod Touch";
  490. }
  491. else if([code rangeOfString:@"iPad"].location != NSNotFound) {
  492. deviceName = @"iPad";
  493. }
  494. else if([code rangeOfString:@"iPhone"].location != NSNotFound){
  495. deviceName = @"iPhone";
  496. }
  497. else {
  498. deviceName = @"Unknown";
  499. }
  500. }
  501. return deviceName;
  502. }
  503. + (NSString *)bundleID {
  504. return [[NSBundle mainBundle] bundleIdentifier];
  505. }
  506. // 用户所在的运营商
  507. + (id)getCarrierCode
  508. {
  509. // 判断是否能够取得运营商
  510. Class telephoneNetWorkClass = (NSClassFromString(@"CTTelephonyNetworkInfo"));
  511. if (telephoneNetWorkClass != nil)
  512. {
  513. CTTelephonyNetworkInfo *telephonyNetworkInfo = [[CTTelephonyNetworkInfo alloc] init];
  514. // 获得运营商的信息
  515. Class carrierClass = (NSClassFromString(@"CTCarrier"));
  516. if (carrierClass != nil)
  517. {
  518. CTCarrier *carrier = telephonyNetworkInfo.subscriberCellularProvider;
  519. // 移动运营商的mcc 和 mnc
  520. NSString * mobileCountryCode = [carrier mobileCountryCode];
  521. NSString * mobileNetworkCode = [carrier mobileNetworkCode];
  522. // 统计能够取到信息的运营商
  523. if ((mobileCountryCode != nil) && (mobileNetworkCode != nil))
  524. {
  525. NSString *mobileCode = [[NSString alloc] initWithFormat:@"%@%@", mobileCountryCode, mobileNetworkCode];
  526. return mobileCode;
  527. }
  528. }
  529. }
  530. return [NSNull null];
  531. }
  532. @end