我们在程序中或多或少都会使用到手机的一些应用程序,比如电话,短信,浏览器,相机,相册等,那么我们该如何调用这些系统自带的应用程序呢?下面我一一说来.
调用电话、浏览器
对于调用电话和浏览器比较简单,使用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];
}
}
网友评论