iOS開發——發簡訊,郵件

來源:互聯網
上載者:User

標籤:

  在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

 

iOS開發——發簡訊,郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.