IOS 获取设备相关特性
一、 判断手机是否插入了SIM卡
解答:
A. 私有 API 检测
[CTSIMSupportGetSIMStatus()isEqualToString:kCTSIMSupportSIMStatusNotInserted]可以判断是否插入了 sim卡。
前提是把下面的代码随便复制到一个头文件里面,然后引入CoreTelephony.framework即可。
1 extern NSString* c*****tkCTSMSMessageReceivedNotification;
2
3 extern NSString* c*****tkCTSMSMessageReplaceReceivedNotification;
4
5 extern NSString* c*****tkCTSIMSupportSIMStatusNotInserted;
6
7 extern NSString* c*****t kCTSIMSupportSIMStatusReady;
8
9
10 id CTTelephonyCenterGetDefault(void);
11
12 void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);
13
14 voidCTTelephonyCenterRemoveObserver(id,id,NSString*,void*);
15
16 int CTSMSMessageGetUnreadCount(void);
17
18 int CTSMSMessageGetRecordIdentifier(void * msg);
19
20 NSString * CTSIMSupportGetSIMStatus();
21
22 NSString * CTSIMSupportCopyMobileSubscriberIdentity();
23
24 id CTSMSMessageCreate(void* unknow,NSString*number,NSString* text);
25
26 void * CTSMSMessageCreateReply(void* unknow,void *forwardTo,NSString* text);
27
28 void* CTSMSMessageSend(id server,id msg);
29
30 NSString *CTSMSMessageCopyAddress(void *, void *);
31
32 NSString *CTSMSMessageCopyText(void *, void *);
B. 开放 API 检测(有待改进,飞行模式下检测不到)
1 -(BOOL)simExist {
2
3 CTTelephonyNetworkInfo *info =[[CTTelephonyNetworkInfo alloc] init];
4
5 CTCarrier *carrier = [info subscriberCellularProvider];
6
7 //NSString *code = [carriermobileNetworkCode];
8
9 NSString *isoCountryCode =[carrier isoCountryCode];
10
11 if (isoCountryCode == nil ||isoCountryCode.length == 0) {
12
13 NSLog(@"no sim");
14
15 return NO;
16 } else {
17
18 NSLog(@"sim exist");
19
20 return YES;
21 }
22 }
二、 获取更换 SIM卡后的通知(只有更换前后的 SIM 运营商不同才能收到)
解答:
1 //初始化
2 CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
3
4 //当sim卡更换时弹出此窗口
5 networkInfo.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier) {
6
7 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Sim card changed" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
8
9 [alert show];
10 };
三、 获取 SIM卡信息
解答:
1 //初始化
2 CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
3
4 //获取sim卡信息
5 CTCarrier *carrier = networkInfo.subscriberCellularProvider;
6
7 //供应商名称(中国联通 中国移动)
8 carrier.carrierName;
9
10 //所在国家编号
11 carrier.mobileCountryCode;
12
13 //供应商网络编号
14 carrier.mobileNetworkCode;
15
16 // iso国家编号
17 carrier.isoCountryCode;
18
19 //是否允许voip
20 carrier.allowsVOIP;
四、 判断锁屏状态
解答:
A. 私有 API 检测
1 #import <SpringBoard/SBAwayController.h>
2
3 //一句话搞定
4 if ([[objc_getClass("SBAwayController")sharedAwayController] isLocked]) {
5
6 PrintLog(@"doublecheck Home ,now YES Lock");
7 } else {
8
9 PrintLog(@"double check Home ,now NOLock");
10 }
B. 程序在前台,这种比较简单。直接使用Darwin层的通知就可以了
1 #import <notify.h>
2
3 #define NotificationLockCFSTR("com.apple.springboard.lockcomplete")