• iOS开发——发短信,邮件


      在IOS开发中,有时候我们会需要用到邮件发送的功能。比如,接收用户反馈和程序崩溃通知等等,这个功能是很常用的。在苹果系统中,如果彼此的手机都是iOS设备,并且开通了iMessage功能,那么彼此之间的短信是走网络通道,而不走运营商的通道,短信也顺便写写喽。

      还是老规矩,直接上代码。

    //

    //  ViewController.m

    //  Demo-testEmail

    //

    //  Created by yyt on 16/5/16.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "ViewController.h"

    #import <MessageUI/MessageUI.h>

    @interface ViewController ()<UIActionSheetDelegate,MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate>

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake(self.view.bounds.size.width/2 - 30, 200, 60, 40);

        button.backgroundColor = [UIColor orangeColor];

        [button setTitle:@"分享" forState:UIControlStateNormal];

        [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

    }

    - (void)clickButton {

        UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"分享" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Email",@"Message", nil];

        [actionSheet showInView:self.view];

    }

    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

        if(buttonIndex==1){

            if ([MFMessageComposeViewController canSendText]) {

                [self sendSMS:@"I‘m using iHeper,it is great!" recipientList:nil];

            } else {

                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"系统短信不可用!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

                [alert show];

            }

        }else if(buttonIndex==0){

            if ([MFMailComposeViewController canSendMail]) { // 用户已设置邮件账户

                [self sendEmailAction]; // 调用发送邮件的代码

            } else {

                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"系统邮箱未设置账号!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

                [alert show];

            }

        }

    }

    - (void)sendEmailAction {

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

        mc.mailComposeDelegate = self;

        [mc setSubject:@"Hi!"];

        [mc setMessageBody:@"I‘m using iHeper,it is great!" isHTML:NO];

        [self presentViewController:mc animated:YES completion:nil];

    }

    //调用sendSMS函数,发送内容,收件人列表

    - (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 presentViewController:controller animated:YES completion:nil];

        }

    }

    // 处理发送完的响应结果

    - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

        [self dismissViewControllerAnimated:YES completion:nil];

        

        if (result == MessageComposeResultCancelled)

            NSLog(@"Message cancelled");

        else if (result == MessageComposeResultSent)

            NSLog(@"Message sent");

        else

            NSLog(@"Message failed") ;

    }

    - (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 dismissViewControllerAnimated:YES completion:nil];

    }

    @end

  • 相关阅读:
    606. Construct String from Binary Tree
    696. Count Binary Substrings
    POJ 3255 Roadblocks (次短路)
    POJ 2823 Sliding Window (单调队列)
    POJ 1704 Georgia and Bob (博弈)
    UVa 1663 Purifying Machine (二分匹配)
    UVa 10801 Lift Hopping (Dijkstra)
    POJ 3281 Dining (网络流之最大流)
    UVa 11100 The Trip, 2007 (题意+贪心)
    UVaLive 4254 Processor (二分+优先队列)
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5498599.html
Copyright © 2020-2023  润新知