1.加入一個Framework(CoreTelephony.framework).
2.引入頭文件
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
3.初始化
#
import
<UIKit/UIKit.h>
#
import
<CoreTelephony/CTTelephonyNetworkInfo.h>
#
import
<CoreTelephony/CTCarrier.h>
@interface
RootViewController : UITableViewController
{
//聲明變量
CTTelephonyNetworkInfo *networkInfo;
}
@implementation
RootViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad];
self.navigationItem.prompt = @
"CTTelephonyNetworkInfo"
;
self.navigationItem.title = @
"CTCarrier"
;
//初始化
networkInfo = [[CTTelephonyNetworkInfo alloc] init];
//當sim卡更換時彈出此窗口
networkInfo.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@
"Sim card changed"
delegate:nil cancelButtonTitle:@
"Dismiss"
otherButtonTitles:nil];
[alert show];
};
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//獲取sim卡信息
CTCarrier *carrier = networkInfo.subscriberCellularProvider;
static
NSString *CellIdentifier = @
"Cell"
;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if
(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
switch
(indexPath.row) {
case
0
:
//供應商名稱(中國聯通 中國移動)
cell.textLabel.text = @
"carrierName"
;
cell.detailTextLabel.text = carrier.carrierName;
break
;
case
1
:
//所在國家編號
cell.textLabel.text = @
"mobileCountryCode"
;
cell.detailTextLabel.text = carrier.mobileCountryCode;
break
;
case
2
:
//供應商網絡編號
cell.textLabel.text = @
"mobileNetworkCode"
;
cell.detailTextLabel.text = carrier.mobileNetworkCode;
break
;
case
3
:
cell.textLabel.text = @
"isoCountryCode"
;
cell.detailTextLabel.text = carrier.isoCountryCode;
break
;
case
4
:
//是否容許voip
cell.textLabel.text = @
"allowsVOIP"
;
cell.detailTextLabel.text = carrier.allowsVOIP?@
"YES"
:@
"NO"
;
break
;
default
:
break
;
}
return
cell;
}
獲取手機號碼
1.NSString *number = [[NSUserDefaults standardUserDefaults] stringForKey:@"SBFormattedPhoneNumber"];
只能獲取的是機主設置的本機號碼,不能讀取sim卡上的電話號碼
2. CTSettingCopyMyPhoneNumber(); 畢竟私有API