iOS之打电话、发短信、发邮件、相机

打开相机代码
[objc]  view plain copy
  1. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  2.     UIImagePickerController *picker = [[UIImagePickerController alloc]init];  
  3.     picker.delegate = self;  
  4.     picker.allowsEditing = YES;  
  5.     picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
  6.     [self presentViewController:picker animated:YES completion:^{}];  
  7. }  
打开相册代码
[objc]  view plain copy
  1. UIImagePickerController *picker = [[UIImagePickerController alloc]init];  
  2. picker.delegate = self;  
  3. picker.allowsEditing = YES;  
  4. [self presentViewController:picker animated:YES completion:^{}];  

选择照片代码
[objc]  view plain copy
  1. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{  
  2.       
  3.     UIImageView *imageview = (UIImageView*)[self.view viewWithTag:100];  
  4.     imageview.image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  5.     [picker dismissViewControllerAnimated:YES completion:^{}];  
  6.     NSLog(@"%@",info);  
  7. }  

代理方法

[objc]  view plain copy
  1. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{  
  2.     [self dismissViewControllerAnimated:YES completion:^{}];  
  3. }  
  4. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{  
  5.       
  6.     UIImageView *imageview = (UIImageView*)[self.view viewWithTag:100];  
  7. <div class="line number67 index66 alt2"><code class="cpp spaces">    </code><code class="cpp comments">// UIImagePickerControllerOriginalImage 原始图片</code></div><div class="line number68 index67 alt1"><code class="cpp spaces">    </code><code class="cpp comments">// UIImagePickerControllerEditedImage 编辑后图片</code></div>  
  8.     imageview.image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  9.     [picker dismissViewControllerAnimated:YES completion:^{}];  
  10.     NSLog(@"%@",info);  
  11. }  

电话、短信、发邮件是手机的基础功能,iOS中提供了接口,让我们调用。这篇文章简单的介绍一下iOS的打电话、发短信在程序中怎么调用

1、发短信

MFMessageComposeViewController提供了操作界面使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.

messageComposeDelegate :代理,处理发送结果

recipients  :收信人<列表,支持群发>

body :短信内容


Frameworks中要引入MessageUI.framework 

#import <MessageUI/MessageUI.h>
添加协议:
<MFMessageComposeViewControllerDelegate>

[objc]  view plain copy
  1. //    MFMessageComposeViewController *mf = [[MFMessageComposeViewController alloc]init];  
  2. //    if ([MFMessageComposeViewController canSendText]) {  
  3. //        mf.body = @"中午我回去不";  
  4. //        mf.recipients = [NSArray arrayWithObjects:@"13888888888", nil];  
  5. //        mf.messageComposeDelegate = self;  
  6. //        [self presentViewController:mf animated:YES completion:^{}];  
  7. //    }  

必要的代理方法

  1. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{  
  2.       
  3.     [self dismissViewControllerAnimated:YES completion:^{}];  
  4.       
  5.     switch ( result ) {  
  6.               
  7.         case MessageComposeResultCancelled:  
  8.   
  9.             [self alertWithTitle:@"提示信息" msg:@"发送取消"];   
  10.             break;  
  11.         case MessageComposeResultFailed:// send failed  
  12.             [self alertWithTitle:@"提示信息" msg:@"发送失败"];   
  13.             break;  
  14.         case MessageComposeResultSent:  
  15.             [self alertWithTitle:@"提示信息" msg:@"发送成功"];   
  16.             break;  
  17.         default:  
  18.             break;   
  19.     }  


  1. - (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {  
  2.   
  3.       
  4.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title  
  5.            message:msg  
  6.            delegate:self  
  7.            cancelButtonTitle:nil  
  8.            otherButtonTitles:@"确定", nil];  
  9.                            
  10.    [alert show];  
  11.                    
  12. }  





2、打电话

  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];//打电话  

       使用openURL这个API打电话结束后,返回的是系统的拨打电话界面,如何才能返回自己的应用呢?有两种方法与大家分享。

 

第一种是用UIWebView加载电话,这种是合法的,可以上App Store的。

代码如下:

  1. UIWebView*callWebview =[[UIWebView alloc] init];  
  2. NSURL *telURL =[NSURL URLWithString:@"tel:10010"];  
  3. [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];  
  4. //记得添加到view上  
  5. [self.view addSubview:callWebview];  


第二种是私有方法,不能上App Store的(自己没试过)。 

  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10010"]];  

上面的代码只是把第一个方法中的tel为telprompt.


3、发邮件

发送邮件代码

[objc]  view plain copy
  1. MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init];  
  2.     mail.mailComposeDelegate = self;  
  3.     [mail setMessageBody:@"jintiantianqibucuo" isHTML:YES];//邮箱内容  
  4.     [mail setToRecipients:[NSArray arrayWithObjects:@"1161913145@qq.com",nil]];//发送对象  
  5.     [mail setCcRecipients:[NSArray arrayWithObjects:@"1161913145@qq.com",nil]];//抄送人  
  6.     [mail setBccRecipients:[NSArray arrayWithObjects:@"1161913145@qq.com",nil]];//密送对象  
  7.     [mail setSubject:@"hello world"];//主题  
  8.     [self presentViewController:mail animated:YES completion:^{}];  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值