一、调用电话
- (IBAction)callPhone:(id)sender { #if 0 //tel:// 打电话 NSURL *url = [NSURL URLWithString:@"tel://10010"]; [[UIApplication sharedApplication] openURL:url]; #else //打电话 可以使用webView作为弹窗显示 if (self.webView == nil) { self.webView = [[UIWebView alloc]init]; } NSURL *url = [NSURL URLWithString:@"tel://10010"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; //显示 [self.webView loadRequest:request]; #endif }
电话、邮件调用需要包含头文件#import <MessageUI/MessageUI.h>
二、调用短信,需要遵守协议<MFMessageComposeViewControllerDelegate>
- (IBAction)sendMessage:(id)sender { #if 0 //用下面这种方式发送信息 跳转到信息界面 取消还是发送 不能回到原来的app //sms:// 发短信 NSURL *url = [NSURL URLWithString:@"sms://123456"]; [[UIApplication sharedApplication] openURL:url]; #else //判断是否能发送信息 BOOL ret = [MFMessageComposeViewController canSendText]; if (ret) { MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc]init]; //要发送消息的电话 controller.recipients = [NSArray arrayWithObject:@"10010"]; controller.body = @"nobody nobody";//要发送的短信的内容 //设置代理 controller.messageComposeDelegate = self; //修改短信界面的标题 [[[controller viewControllers] lastObject] setTitle:@"短信"]; NSLog(@"可以发送"); //弹出界面 [self presentViewController:controller animated:YES completion:nil]; }else{ NSLog(@"不能发送"); } #endif } #pragma mark--发送消息协议,发送完成调用此方法。返回发送结果 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ //把当前短信界面消失 [controller dismissViewControllerAnimated:YES completion:nil]; /* MessageComposeResultCancelled, MessageComposeResultSent, MessageComposeResultFailed */ switch (result) { case MessageComposeResultCancelled: NSLog(@"取消发送"); break; case MessageComposeResultSent: NSLog(@"发送成功"); break; case MessageComposeResultFailed: NSLog(@"发送失败"); break; default: break; } }
三、调用邮件,需要遵守协议<MFMailComposeViewControllerDelegate>
- (IBAction)sentMail:(id)sender { #if 0 //mailto:// 发邮件 NSURL *url = [NSURL URLWithString:@"mailto://[email protected]"]; [[UIApplication sharedApplication] openURL:url]; #else //MFMailComposeViewController 发邮件的类 MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init]; if (!mail) { NSLog(@"当前设备不支持应用内发送邮件"); return; } if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *controller = [[MFMailComposeViewController alloc]init]; //设置代理 controller.mailComposeDelegate = self; //添加收件人 [controller setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; [controller setMessageBody:@"马化腾" isHTML:NO]; //弹出界面 [self presentViewController:controller animated:YES completion:nil]; }else{ NSLog(@"不能发送邮件"); } #endif } #pragma mark--发送邮件协议,发送完成调用此方法。返回发送结果 - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ [controller dismissViewControllerAnimated:YES completion:nil]; /* MFMailComposeResultCancelled, MFMailComposeResultSaved, MFMailComposeResultSent, MFMailComposeResultFailed */ switch (result) { case MFMailComposeResultCancelled: NSLog(@"取消发送"); break; case MFMailComposeResultSaved: NSLog(@"保存"); break; case MFMailComposeResultSent: NSLog(@"发送成功"); break; case MFMailComposeResultFailed: NSLog(@"发送失败"); break; default: break; } }四、调用图库或相机
需要遵守的协议<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
//调取提示框的代码
- (IBAction)click:(id)sender { UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle:@"打开相机或照片库" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相机" otherButtonTitles:@"照片库", nil]; [sheet showInView:self.view]; }
//提示框的代理方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 3) return; UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.allowsEditing = YES; picker.delegate = self; if (buttonIndex == 0) { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; //如果没有拍照功能 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } picker.sourceType = sourceType; picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType]; } else if(buttonIndex == 1){ if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } } [self presentViewController:picker animated:YES completion:nil]; }
#pragma mark--点击相册中的图片 货照相机照完后点击use 后触发的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ /* info dictionary keys UIImagePickerControllerOriginalImage// a UIImage UIImagePickerControllerEditedImage// a UIImage UIImagePickerControllerMediaURL// an NSURL */ //获取修改后的图片 UIImage *editedImg = info[UIImagePickerControllerEditedImage]; NSURL * mediaUrl = info[UIImagePickerControllerMediaURL]; if (editedImg) { //更改cell里的图片 [self.headPicture.layer.sublayers.lastObject removeFromSuperlayer]; self.headPicture.image = editedImg; } if (mediaUrl) { self.headPicture.image = nil; [self creatMediaPlayerWithUrl:mediaUrl]; } //移除图片选择的控制器 [self dismissViewControllerAnimated:YES completion:nil]; }
#pragma mark--点击cancel 调用的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ NSLog(@"取消了操作"); [self dismissViewControllerAnimated:YES completion:nil]; }
#pragma mark--创建播放器
-(void)creatMediaPlayerWithUrl:(NSURL * )url{ // 创建播放器 AVPlayer *player = [AVPlayer playerWithURL:url]; [player play]; // 创建播放器图层 AVPlayerLayer *layer = [AVPlayerLayer layer]; layer.player = player; layer.frame = self.headPicture.bounds; // 添加图层到控制器的view [self.headPicture.layer addSublayer:layer]; self.player = player; }