判斷 iPhone 是否已插入 SIM 卡的方法
私有API
巨大的坑,网上都有可以,实际上是不可以的!
网上的代码和描述
判斷 iPhone 是否插入了 SIM 卡,可以參考蘋果官網的 systemconfigure framework 教程,將下面的代碼複製到頭文件
extern NSString* const kCTSMSMessageReceivedNotification;
extern NSString* const kCTSMSMessageReplaceReceivedNotification;
extern NSString* const kCTSIMSupportSIMStatusNotInserted;
extern NSString* const kCTSIMSupportSIMStatusReady;
id CTTelephonyCenterGetDefault(void);
void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);
void CTTelephonyCenterRemoveObserver(id,id,NSString*,void*);
int CTSMSMessageGetUnreadCount(void);
int CTSMSMessageGetRecordIdentifier(void * msg);
NSString * CTSIMSupportGetSIMStatus();
NSString * CTSIMSupportCopyMobileSubscriberIdentity();
id CTSMSMessageCreate(void* unknow/*always 0*/,NSString* number,NSString* text);
void * CTSMSMessageCreateReply(void* unknow/*always 0*/,void * forwardTo,NSString* text);
void* CTSMSMessageSend(id server,id msg);
NSString *CTSMSMessageCopyAddress(void *, void *);
NSString *CTSMSMessageCopyText(void *, void *);
坑:还特别标注,可以參考蘋果官網的 systemconfigure framework 教程
实际上这事有问题的
public API
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
- (IBAction)handleCallButtonPress:(id)sender
{
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
NSString *code = [networkInfo.subscriberCellularProvider mobileCountryCode];
//this is nil if you take out sim card.
if (code == nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"aler.error",nil)
message:NSLocalizedString(@"alert.message.no_sim_card",nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"alert.button_dimiss", nil)
otherButtonTitles:nil];
[alertView show];
return;
}
}