华山论剑之浅谈iOS调用大乱斗(电话,短信,浏览器,相机,相册)

我们在程序中或多或少都会使用到手机的一些应用程序,比如电话,短信,浏览器,相机,相册等,那么我们该如何调用这些系统自带的应用程序呢?下面我一一说来.


调用电话、浏览器


对于调用电话和浏览器比较简单,使用UIApplication调用openURL方法.

调用系统电话

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];

调用系统safari浏览器

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.coder-dong.com"]];

调用短信功能


调用系统的短信功能, 同样使用UIApplication调用openURL方法. 我们只能openURL的方法中只能设定号码,不能做内容太上的更改.那么我们就需要导入我们的MessageUI.framework库,然后导入头文件MFMessageComposeViewController.h,并实现代理方法.

导入MFMessageComposeViewController头文件

#import <MessageUI/MFMessageComposeViewController.h>

实现自定义方法并设置代理

- (IBAction)SMSAction:(id)sender {
    
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"]];
    
    [self sendSMS:@"我想咨询一下话费余额是不是一百万." recipientList:@[@"10086客服"]];
    
}


- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients{
    
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
 
    if([MFMessageComposeViewController canSendText]){
        
        controller.body = bodyOfMessage;
        
        controller.recipients = recipients;
    
        controller.messageComposeDelegate = self;
        
        [self showDetailViewController:controller sender:nil];
        
    }   
    
}

当完成消息的发送之后,我们就需要调用我们的代理方法,给用户反馈信息

//代理方法的实现
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    if (result == MessageComposeResultCancelled){
        NSLog(@"取消发送消息");
    } else if (result == MessageComposeResultSent){
        NSLog(@"消息已经发送");
    }else{
        
        NSLog(@"消息发送失败");
    }

}


调用相机、相册


调用相机、相册我们需要使用到UIImagePickerController这个类,所以我们需要首先导入这个类

#import <UIKit/UIImagePickerController.h>

然后我们在点击方法中实现调用相机,当然了,调用的时候,我们要先判断设备是否存在摄像头.

 //设置调用类型为相机
    UIImagePickerControllerSourceType  sourceType = UIImagePickerControllerSourceTypeCamera;
    
        if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        
            NSLog(@"本设备未发现摄像头!");
            
            return;
        
        }
    
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    
    pickerController.sourceType = sourceType;
    
    pickerController.delegate = self;
    
    pickerController.allowsEditing = YES;//设置是否可以进行编辑
    

    [self showDetailViewController:pickerController sender:nil];

调用系统相册,调用系统相册的时候,跟调用相机的时候十分相似.

//设置调用类型为相册.
    UIImagePickerControllerSourceType  sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    
    pickerController.sourceType = sourceType;
    
    pickerController.delegate = self;
    
    pickerController.allowsEditing = YES;
    
    [self showDetailViewController:pickerController sender:nil];

我们调用系统相册和相机最终的目的是为了获得系统中的图片资源.所以我们需要实现代理方法,获取到我们的图片.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
        
        //得到选择后沙盒中图片的完整路径
       NSString * filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
        
        NSLog(@"%@",filePath);
        
        //关闭相册界面
        [picker dismissModalViewControllerAnimated:YES];
        
        //创建一个选择后图片的小图标放在下方
        //类似微薄选择图后的效果
        UIImageView *smallimage = [[UIImageView alloc] initWithFrame:
                                    CGRectMake(50, 120, 40, 40)] ;
        
        smallimage.image = image;
        //加在视图中
        [self.view addSubview:smallimage];
        
    }

}



整体上看 ,调用系统的一些应用还是很简单的,希望这篇调用大乱斗能对大家有所帮助.😃

--->参考博客链接.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,966评论 25 709
  • 1.OC里用到集合类是什么? 基本类型为:NSArray,NSSet以及NSDictionary 可变类型为:NS...
    轻皱眉头浅忧思阅读 5,229评论 0 3
  • 1. 主题明确 标题不能为空,要简洁 2. 开头结尾 开头,和收件人很熟的情况下,可以用Dear,XX,否则用HI...
    硬件工程师技术号阅读 4,811评论 0 0
  • 水石轩主人阅读 1,499评论 0 0

友情链接更多精彩内容