iOS調(diào)用手機(jī)相關(guān)(短信,電話,通訊錄,內(nèi)存,手電筒,手機(jī)信息)

設(shè)備信息
注獲取手機(jī)型號(hào)需要導(dǎo)入

import "sys/utsname.h"

獲取運(yùn)行商需要導(dǎo)入

import <CoreTelephony/CTCarrier.h>

import <CoreTelephony/CTTelephonyNetworkInfo.h>

-(void)networktype{

//設(shè)備唯一標(biāo)識(shí)符
NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSLog(@"設(shè)備唯一標(biāo)識(shí)符:%@",identifierStr);
//手機(jī)別名: 用戶定義的名稱
NSString* userPhoneName = [[UIDevice currentDevice] name];
NSLog(@"手機(jī)別名: %@", userPhoneName);
//設(shè)備名稱
NSString* deviceName = [[UIDevice currentDevice] systemName];
NSLog(@"設(shè)備名稱: %@",deviceName );
//手機(jī)系統(tǒng)版本
NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
NSLog(@"手機(jī)系統(tǒng)版本: %@", phoneVersion);
//手機(jī)型號(hào)
struct utsname systemInfo;
uname(&systemInfo);

NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
//iPhone

// NSString * phoneModel = [self ];
NSLog(@"手機(jī)型號(hào):%@",deviceString);
//地方型號(hào) (國(guó)際化區(qū)域名稱)
NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];
NSLog(@"國(guó)際化區(qū)域名稱: %@",localPhoneModel );
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// 當(dāng)前應(yīng)用軟件版本 比如:1.0.1
NSString appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSLog(@"當(dāng)前應(yīng)用軟件版本:%@",appCurVersion);
// 當(dāng)前應(yīng)用版本號(hào)碼 int類型
NSString appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];
NSLog(@"當(dāng)前應(yīng)用版本號(hào)碼:%@",appCurVersionNum);
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize size = rect.size;
CGFloat width = size.width;
CGFloat height = size.height;
NSLog(@"物理尺寸:%.0f × %.0f",width,height);
CGFloat scale_screen = [UIScreen mainScreen].scale;
NSLog(@"分辨率是:%.0f × %.0f",width
scale_screen ,height
scale_screen);
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = info.subscriberCellularProvider;
NSLog(@"運(yùn)營(yíng)商:%@", carrier.carrierName);

}

手機(jī)電池信息
//獲取電池
// [[UIDevice currentDevice].batteryLevel];
//獲取電池狀態(tài)
// [UIDevice currentDevice].batteryState;


手電筒
-(void)torchaOntou:(UIButton *)sender{
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) { // 判斷是否有閃光燈
// 請(qǐng)求獨(dú)占訪問(wèn)硬件設(shè)備
[device lockForConfiguration:nil];
if (sender.tag == 105) {
//
sender.tag = 205;
[device setTorchMode:AVCaptureTorchModeOn]; // 手電筒開(kāi)
}else if(sender.tag==205){

          sender.tag = 105;
            [device setTorchMode:AVCaptureTorchModeOff]; // 手電筒關(guān)
        }
        // 請(qǐng)求解除獨(dú)占訪問(wèn)硬件設(shè)備
        [device unlockForConfiguration];
    }
}

}


電話
// 1.第一種
// [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://10010"]];

2.第二種(別忘了tel:)
UIWebView * callWebview = [[UIWebView alloc]init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",@"10086"]];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
區(qū)別:網(wǎng)上有解釋為第一種打完電話留在打電話界面,第二種打完電話回到原來(lái)的app
// 真實(shí)測(cè)試:兩種打完電話都是回到原來(lái)的app界面,
// 區(qū)別一:第一種會(huì)先跳出程序到系統(tǒng)的打電話程序,第二種是一直都在自己的app中運(yùn)行,沒(méi)有出去過(guò)。
// 區(qū)別二:第一種觸發(fā)直接到打電話界面,第二種會(huì)先彈出一個(gè)對(duì)話框,可以選擇打不打電話,對(duì)話框如下。


短信

2種方式
1直接調(diào)用
第二種
導(dǎo)入

import <MessageUI/MessageUI.h>

遵守實(shí)現(xiàn)代理方法MFMessageComposeViewControllerDelegate

//第一種
// [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://13888888888"]];

//第二種
[self showMessageView:[NSArray arrayWithObjects:@"121",@"131",nil] title:@"" body:@"土豪你好"];

-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
if( [MFMessageComposeViewController canSendText] )
{
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = phones;
controller.navigationBar.tintColor = [UIColor redColor];
controller.body = body;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面標(biāo)題
}
else
{
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
// message:@"該設(shè)備不支持短信功能"
// delegate:nil
// cancelButtonTitle:@"確定"
// otherButtonTitles:nil, nil];
// [alert show];
}
}

監(jiān)聽(tīng)發(fā)送以后成功與否回調(diào)
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{result是一個(gè)枚舉類型

MessageComposeResultSent:
//信息傳送成功

MessageComposeResultFailed:
//信息傳送失敗
MessageComposeResultCancelled:
//信息被用戶取消傳送

}

手機(jī)亮度
//獲取屏膜亮度
// [UIScreen mainScreen].brightness;
//設(shè)置屏膜亮度
[UIScreen mainScreen].brightness = 0.5;


電池
//獲取電池
// [[UIDevice currentDevice].batteryLevel];
//獲取電池狀態(tài)
// [UIDevice currentDevice].batteryState;


內(nèi)存
//獲取當(dāng)前設(shè)備可用內(nèi)存以及所占內(nèi)存的頭文件

import <sys/sysctl.h>

import <mach/mach.h>

// 獲取當(dāng)前設(shè)備可用內(nèi)存(單位:MB)

  • (double)availableMemory
    {
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(),
    HOST_VM_INFO,
    (host_info_t)&vmStats,
    &infoCount);

    if (kernReturn != KERN_SUCCESS) {
    return NSNotFound;
    }

    return ((vm_page_size *vmStats.free_count) / 1024.0) / 1024.0;
    }
    // 獲取當(dāng)前任務(wù)所占用的內(nèi)存(單位:MB)

  • (double)usedMemory
    {
    task_basic_info_data_t taskInfo;
    mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
    kern_return_t kernReturn = task_info(mach_task_self(),
    TASK_BASIC_INFO,
    (task_info_t)&taskInfo,
    &infoCount);

    if (kernReturn != KERN_SUCCESS
    ) {
    return NSNotFound;
    }

    return taskInfo.resident_size / 1024.0 / 1024.0;
    }


通訊錄
方案一:AddressBookUI.framework

實(shí)現(xiàn)步驟:

創(chuàng)建選擇聯(lián)系人的控制器
設(shè)置代理:用來(lái)接收用戶選擇的聯(lián)系人信息
彈出聯(lián)系人控制器
實(shí)現(xiàn)代理方法
在對(duì)應(yīng)的代理方法中獲取聯(lián)系人信息

方案二AddressBook.frame
實(shí)現(xiàn)步驟:
請(qǐng)求授權(quán)
判斷授權(quán)狀態(tài)如果已授權(quán)則繼續(xù),如果未授權(quán)則提示用戶
創(chuàng)建通訊錄對(duì)象
從通訊錄中獲取所有的聯(lián)系人
遍歷所有的聯(lián)系人
釋放不再使用的對(duì)象
10.0需要導(dǎo)入plist
<key>NSContactsUsageDescription</key>
<string>請(qǐng)求訪問(wèn)通訊錄</string>

第三方框架:RHAddressBook

  • (void)requestAuthorizationAddressBook {
    // 判斷是否授權(quán)
    //'ABAddressBookGetAuthorizationStatus 9.0以后
    ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
    if (authorizationStatus == kABAuthorizationStatusNotDetermined) {
    // 請(qǐng)求授權(quán)
    ABAddressBookRef addressBookRef = ABAddressBookCreate();
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
    if (granted) { // 授權(quán)成功

          } else {        // 授權(quán)失敗
              NSLog(@"授權(quán)失??!");
          }
      });
    

    }

    // 2. 獲取所有聯(lián)系人
    ABAddressBookRef addressBookRef = ABAddressBookCreate();
    CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
    long count = CFArrayGetCount(arrayRef);
    for (int i = 0; i < count; i++) {
    //獲取聯(lián)系人對(duì)象的引用
    ABRecordRef people = CFArrayGetValueAtIndex(arrayRef, i);

      //獲取當(dāng)前聯(lián)系人名字
      NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
      
      //獲取當(dāng)前聯(lián)系人姓氏
      NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
      NSLog(@"--------------------------------------------------");
      NSLog(@"firstName=%@, lastName=%@", firstName, lastName);
      
      //獲取當(dāng)前聯(lián)系人的電話 數(shù)組
      NSMutableArray *phoneArray = [[NSMutableArray alloc]init];
      ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
      for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
          NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j));
          NSLog(@"phone=%@", phone);
          [phoneArray addObject:phone];
      }
      
      //獲取當(dāng)前聯(lián)系人的郵箱 注意是數(shù)組
      NSMutableArray *emailArray = [[NSMutableArray alloc]init];
      ABMultiValueRef emails= ABRecordCopyValue(people, kABPersonEmailProperty);
      for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) {
          NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j));
          NSLog(@"email=%@", email);
          [emailArray addObject:email];
      }
      //獲取當(dāng)前聯(lián)系人中間名
      NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
      //獲取當(dāng)前聯(lián)系人的名字前綴
      NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty));
      
      //獲取當(dāng)前聯(lián)系人的名字后綴
      NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty));
      
      //獲取當(dāng)前聯(lián)系人的昵稱
      NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));
      
      //獲取當(dāng)前聯(lián)系人的名字拼音
      NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));
      
      //獲取當(dāng)前聯(lián)系人的姓氏拼音
      NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty));
      
      //獲取當(dāng)前聯(lián)系人的中間名拼音
      NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty));
      
      //獲取當(dāng)前聯(lián)系人的公司
      NSString *organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty));
      
      //獲取當(dāng)前聯(lián)系人的職位
      NSString *job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty));
      
      //獲取當(dāng)前聯(lián)系人的部門(mén)
      NSString *department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty));
      
      //獲取當(dāng)前聯(lián)系人的生日
      NSString *birthday=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonBirthdayProperty));
      
      //獲取當(dāng)前聯(lián)系人的備注
      NSString *notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));
      
      //獲取創(chuàng)建當(dāng)前聯(lián)系人的時(shí)間 注意是NSDate
      NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty));
      
      //獲取最近修改當(dāng)前聯(lián)系人的時(shí)間
      NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonModificationDateProperty));
      
      //獲取地址
      ABMultiValueRef address = ABRecordCopyValue(people, kABPersonAddressProperty);
      for (int j=0; j<ABMultiValueGetCount(address); j++) {
          //地址類型
          NSString *type = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(address, j));
          NSDictionary * tempDic = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(address, j));
          //地址字符串,可以按需求格式化
          NSString *adress = [NSString stringWithFormat:@"國(guó)家:%@\n省:%@\n市:%@\n街道:%@\n郵編:%@",[tempDic valueForKey:(NSString*)kABPersonAddressCountryKey],[tempDic valueForKey:(NSString*)kABPersonAddressStateKey],[tempDic valueForKey:(NSString*)kABPersonAddressCityKey],[tempDic valueForKey:(NSString*)kABPersonAddressStreetKey],[tempDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
      }
      
      //獲取當(dāng)前聯(lián)系人頭像圖片
      NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
      
      //獲取當(dāng)前聯(lián)系人紀(jì)念日
      NSMutableArray *dateArr = [[NSMutableArray alloc]init];
      ABMultiValueRef dates= ABRecordCopyValue(people, kABPersonDateProperty);
      for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
          //獲取紀(jì)念日日期
          NSDate *data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
          //獲取紀(jì)念日名稱
          NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
          NSDictionary *tempDic = [NSDictionary dictionaryWithObject:data forKey:str];
          [dateArr addObject:tempDic];
      }
    

    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容