美文网首页
iOS调用手机相关(短信,电话,通讯录,内存,手电筒,手机信息)

iOS调用手机相关(短信,电话,通讯录,内存,手电筒,手机信息)

作者: 三好夫人 | 来源:发表于2018-02-05 15:30 被阅读0次

    设备信息
    注获取手机型号需要导入

    import "sys/utsname.h"

    获取运行商需要导入

    import <CoreTelephony/CTCarrier.h>

    import <CoreTelephony/CTTelephonyNetworkInfo.h>

    -(void)networktype{

    //设备唯一标识符
    NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSLog(@"设备唯一标识符:%@",identifierStr);
    //手机别名: 用户定义的名称
    NSString* userPhoneName = [[UIDevice currentDevice] name];
    NSLog(@"手机别名: %@", userPhoneName);
    //设备名称
    NSString* deviceName = [[UIDevice currentDevice] systemName];
    NSLog(@"设备名称: %@",deviceName );
    //手机系统版本
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    NSLog(@"手机系统版本: %@", phoneVersion);
    //手机型号
    struct utsname systemInfo;
    uname(&systemInfo);
    
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //iPhone
    

    // NSString * phoneModel = [self ];
    NSLog(@"手机型号:%@",deviceString);
    //地方型号 (国际化区域名称)
    NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];
    NSLog(@"国际化区域名称: %@",localPhoneModel );
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // 当前应用软件版本 比如:1.0.1
    NSString appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"当前应用软件版本:%@",appCurVersion);
    // 当前应用版本号码 int类型
    NSString appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];
    NSLog(@"当前应用版本号码:%@",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(@"运营商:%@", carrier.carrierName);

    }

    手机电池信息
    //获取电池
    // [[UIDevice currentDevice].batteryLevel];
    //获取电池状态
    // [UIDevice currentDevice].batteryState;


    手电筒
    -(void)torchaOntou:(UIButton *)sender{
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) { // 判断是否有闪光灯
    // 请求独占访问硬件设备
    [device lockForConfiguration:nil];
    if (sender.tag == 105) {
    //
    sender.tag = 205;
    [device setTorchMode:AVCaptureTorchModeOn]; // 手电筒开
    }else if(sender.tag==205){

              sender.tag = 105;
                [device setTorchMode:AVCaptureTorchModeOff]; // 手电筒关
            }
            // 请求解除独占访问硬件设备
            [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]];
    区别:网上有解释为第一种打完电话留在打电话界面,第二种打完电话回到原来的app
    // 真实测试:两种打完电话都是回到原来的app界面,
    // 区别一:第一种会先跳出程序到系统的打电话程序,第二种是一直都在自己的app中运行,没有出去过。
    // 区别二:第一种触发直接到打电话界面,第二种会先弹出一个对话框,可以选择打不打电话,对话框如下。


    短信

    2种方式
    1直接调用
    第二种
    导入

    import <MessageUI/MessageUI.h>

    遵守实现代理方法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];//修改短信界面标题
    }
    else
    {
    // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
    // message:@"该设备不支持短信功能"
    // delegate:nil
    // cancelButtonTitle:@"确定"
    // otherButtonTitles:nil, nil];
    // [alert show];
    }
    }

    监听发送以后成功与否回调
    -(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
    {result是一个枚举类型

    MessageComposeResultSent:
    //信息传送成功

    MessageComposeResultFailed:
    //信息传送失败
    MessageComposeResultCancelled:
    //信息被用户取消传送

    }

    手机亮度
    //获取屏膜亮度
    // [UIScreen mainScreen].brightness;
    //设置屏膜亮度
    [UIScreen mainScreen].brightness = 0.5;


    电池
    //获取电池
    // [[UIDevice currentDevice].batteryLevel];
    //获取电池状态
    // [UIDevice currentDevice].batteryState;


    内存
    //获取当前设备可用内存以及所占内存的头文件

    import <sys/sysctl.h>

    import <mach/mach.h>

    // 获取当前设备可用内存(单位: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;
      }
      // 获取当前任务所占用的内存(单位: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

    实现步骤:

    创建选择联系人的控制器
    设置代理:用来接收用户选择的联系人信息
    弹出联系人控制器
    实现代理方法
    在对应的代理方法中获取联系人信息

    方案二AddressBook.frame
    实现步骤:
    请求授权
    判断授权状态如果已授权则继续,如果未授权则提示用户
    创建通讯录对象
    从通讯录中获取所有的联系人
    遍历所有的联系人
    释放不再使用的对象
    10.0需要导入plist
    <key>NSContactsUsageDescription</key>
    <string>请求访问通讯录</string>

    第三方框架:RHAddressBook

    • (void)requestAuthorizationAddressBook {
      // 判断是否授权
      //'ABAddressBookGetAuthorizationStatus 9.0以后
      ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
      if (authorizationStatus == kABAuthorizationStatusNotDetermined) {
      // 请求授权
      ABAddressBookRef addressBookRef = ABAddressBookCreate();
      ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
      if (granted) { // 授权成功

            } else {        // 授权失败
                NSLog(@"授权失败!");
            }
        });
      

      }

      // 2. 获取所有联系人
      ABAddressBookRef addressBookRef = ABAddressBookCreate();
      CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
      long count = CFArrayGetCount(arrayRef);
      for (int i = 0; i < count; i++) {
      //获取联系人对象的引用
      ABRecordRef people = CFArrayGetValueAtIndex(arrayRef, i);

        //获取当前联系人名字
        NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
        
        //获取当前联系人姓氏
        NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
        NSLog(@"--------------------------------------------------");
        NSLog(@"firstName=%@, lastName=%@", firstName, lastName);
        
        //获取当前联系人的电话 数组
        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];
        }
        
        //获取当前联系人的邮箱 注意是数组
        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];
        }
        //获取当前联系人中间名
        NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
        //获取当前联系人的名字前缀
        NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty));
        
        //获取当前联系人的名字后缀
        NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty));
        
        //获取当前联系人的昵称
        NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));
        
        //获取当前联系人的名字拼音
        NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));
        
        //获取当前联系人的姓氏拼音
        NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty));
        
        //获取当前联系人的中间名拼音
        NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty));
        
        //获取当前联系人的公司
        NSString *organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty));
        
        //获取当前联系人的职位
        NSString *job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty));
        
        //获取当前联系人的部门
        NSString *department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty));
        
        //获取当前联系人的生日
        NSString *birthday=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonBirthdayProperty));
        
        //获取当前联系人的备注
        NSString *notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));
        
        //获取创建当前联系人的时间 注意是NSDate
        NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty));
        
        //获取最近修改当前联系人的时间
        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:@"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@",[tempDic valueForKey:(NSString*)kABPersonAddressCountryKey],[tempDic valueForKey:(NSString*)kABPersonAddressStateKey],[tempDic valueForKey:(NSString*)kABPersonAddressCityKey],[tempDic valueForKey:(NSString*)kABPersonAddressStreetKey],[tempDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
        }
        
        //获取当前联系人头像图片
        NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
        
        //获取当前联系人纪念日
        NSMutableArray *dateArr = [[NSMutableArray alloc]init];
        ABMultiValueRef dates= ABRecordCopyValue(people, kABPersonDateProperty);
        for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
            //获取纪念日日期
            NSDate *data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
            //获取纪念日名称
            NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
            NSDictionary *tempDic = [NSDictionary dictionaryWithObject:data forKey:str];
            [dateArr addObject:tempDic];
        }
      

      }

    }

    相关文章

      网友评论

          本文标题:iOS调用手机相关(短信,电话,通讯录,内存,手电筒,手机信息)

          本文链接:https://www.haomeiwen.com/subject/sefizxtx.html