iOS development-Texting, mail

Source: Internet
Author: User
Tags mail account

In the development of iOS, sometimes we need to use the mail send function. For example, receiving user feedback and program crash notifications, and so on, this feature is very common. In the Apple system, if each other's mobile phones are iOS devices, and the opening of the IMessage function, then the message between each other is to take the network channel, but not the operator's channel, SMS also written by the way.

Or the same as the usual, directly on the code.

//

Viewcontroller.m

Demo-testemail

//

Created by Yyt on 16/5/16.

COPYRIGHT©2016 year 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:@ "share" forstate:uicontrolstatenormal];

[Button addtarget:self action: @selector (Clickbutton) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:button];

}

-(void) Clickbutton {

Uiactionsheet *actionsheet=[[uiactionsheet alloc]initwithtitle:@ "Share" 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:@ "System SMS is not available! "Delegate:nil cancelbuttontitle:@" OK "otherbuttontitles:nil];

[Alert show];

}

}else if (buttonindex==0) {

if ([Mfmailcomposeviewcontroller Cansendmail]) {//user has set up mail account

[Self sendemailaction]; Calling code to send a message

} else {

Uialertview *alert=[[uialertview alloc]initwithtitle:nil message:@ "System Mailbox not set account! "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];

}

Call sendsms function, send content, recipient list

-(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];

}

}

Processing the results of a response sent out

-(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 development-Texting, mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.