zoukankan      html  css  js  c++  java
  • iOS系统短信、电话、浏览器、邮件的调用

    在iOS中调用系统的某些功能用几行代码就能搞定,学习一下还是很实用的,在很多项目中都有用到,对项目开发也很有帮助。

    - (void)viewDidLoad {

        [super viewDidLoad];

        // 写一个按钮,点击按钮调用短信

        UIButton *smsButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 80, 30)];

        smsButton.backgroundColor = [UIColor redColor];

        [smsButton setTitle:@"短信" forState:(UIControlStateNormal)];

        [smsButton addTarget:self action:@selector(smsButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];

        [self.view addSubview:smsButton];

        

        // 写一个按钮,点击按钮调用电话

        UIButton *telButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 150, 80, 30)];

        telButton.backgroundColor = [UIColor redColor];

        [telButton setTitle:@"电话" forState:(UIControlStateNormal)];

        [telButton addTarget:self action:@selector(telButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];

        [self.view addSubview:telButton];

        

        // 写一个按钮,点击按钮调用浏览器

        UIButton *urlButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 80, 30)];

        urlButton.backgroundColor = [UIColor redColor];

        [urlButton setTitle:@"网址" forState:(UIControlStateNormal)];

        [urlButton addTarget:self action:@selector(urlButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];

        [self.view addSubview:urlButton];

        

        // 写一个按钮,点击按钮调用系统邮件  实现系统的邮件发送,首先需要引入MessageUI.framework框架

        UIButton *mailButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 250, 80, 30)];

        mailButton.backgroundColor = [UIColor redColor];

        [mailButton setTitle:@"邮件" forState:(UIControlStateNormal)];

        [mailButton addTarget:self action:@selector(mailButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];

        [self.view addSubview:mailButton];

    }

     

    // 短信按钮的点击事件

    - (void)smsButtonAction:(UIButton*)sender

    {

        self.view.backgroundColor = [UIColor blackColor];

        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://123"]];

    }

     

    // 电话按钮的点击事件

    - (void)telButtonAction:(UIButton*)sender

    {

        self.view.backgroundColor = [UIColor yellowColor];

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

    }

     

    // 浏览器按钮的点击事件

    - (void)urlButtonAction:(UIButton*)sender

    {

        self.view.backgroundColor = [UIColor blueColor];

        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://baidu.com"]];

    }

     

    // 邮件按钮的点击事件 

    - (void)mailButtonAction:(UIButton*)sender

    {

        self.view.backgroundColor = [UIColor grayColor];

        MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc]init];

    //  这里首先要在遵循 MFMailComposeViewControllerDelegate 

        mailCompose.mailComposeDelegate = self;

    //    邮件主题

        [mailCompose setSubject:@"test"];

    //    邮件的接收者(可以不写,在后面再写接收者的邮箱)

        [mailCompose setToRecipients:[NSArray arrayWithObjects:@"cityingHe@163.com",@"cityingma@163.com", nil]];

    //    邮件正文

        [mailCompose setMessageBody:@"hello,this is a message!" isHTML:NO];

    //    获取截图附件(NSData

        NSData *imageData = UIImagePNGRepresentation(nil);//nil应该由其他图片名代替

        [mailCompose addAttachmentData:imageData mimeType:@"image/png" fileName:@"attachmentName"];

        [self presentViewController:mailCompose animated:YES completion:^{

            NSLog(@"邮件");

        }];

        

    }

     

     

    //代理方法的实现

    - (void)mailComposeController:(MFMailComposeViewController*)controller

              didFinishWithResult:(MFMailComposeResult)result

                            error:(NSError*)error

    {

        switch (result){

            case MFMailComposeResultCancelled:

                NSLog(@"Mail send canceled…");

                break;

            case MFMailComposeResultSaved:

                NSLog(@"Mail saved…");

                break;

            case MFMailComposeResultSent:

                NSLog(@"Mail sent…");

                break;

            case MFMailComposeResultFailed:

                NSLog(@"Mail send errored: %@…", [error localizedDescription]);

                break;

            default:

                break;

                

        }

        

        [self dismissModalViewControllerAnimated:YES];

    }

    下面是调用邮箱的效果:

     

  • 相关阅读:
    [Linux]常用命令之【tar/zip/unzip/gzip/gunzip】
    [Git]解决: error: unable to create file src/main/webapp/xxxxxx/xxxx: Filename too long
    [Git]解决:error: The following untracked working tree files would be removed by checkout:
    [Linux]命令行分类
    [数据库/MYSQL]#解决缺陷#设置Unique索引时:"[Err] 1071
    [Java EE]辨析: POJO(PO / DTO / VO) | BO/DO | DAO
    [Java]遍历枚举类型为List
    【Vue】在Vue项目中调试Vue源码——修改Vue项目引入的vue文件
    【Vue】Vue源码解读之Component组件注册
    【Vue】驼峰命名法和短横线命名法的转换以及Vue源码中对驼峰式和大写式查找的支持
  • 原文地址:https://www.cnblogs.com/cityingma/p/4879002.html
Copyright © 2011-2022 走看看