Mfmailcomposeviewcontroller sends an email

Source: Internet
Author: User
Tags email account

1. The iPhone API already provides an interface for writing emails to the system. You can use mfmailcomposeviewcontroller to display the interface.

2. Add messageui. Framework to the project. Add the header file mfmailcomposeviewcontrollerdelegate. # Import <messageui/messageui. h>

 

-(Void) viewdidload

{

// The instantiate button is used to call the mailbox.

Uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect];

Button. Frame = cgrectmake (0, 40,320, 50 );

[Button settitle: @ "mail" forstate: uicontrolstatenormal];

[Button addtarget: Self action: @ selector (sendemailaction) forcontrolevents: uicontroleventtouchupinside];

[Self. View addsubview: button];

}

-(Void) sendemailaction

{

Class mailclass = (nsclassfromstring (@ "mfmailcomposeviewcontroller "));

If (mailclass! = Nil)

{

If ([mailclass cansendmail])

{

[Self displaycomposersheet];

} Else {

[Self launchmailappondevice];

}

} Else {

[Self launchmailappondevice];

}

}

-(Void) messagecomposeviewcontroller :( mfmessagecomposeviewcontroller *) controller didfinishwithresult :( messagecomposeresult) Result

{

// [Self dismissmodalviewcontrolleranimated: Yes];

[Self dismissviewcontrolleranimated: Yes completion: Nil];

Nsstring * MSG;

Switch (result ){

Case messagecomposeresultcancelled:

MSG = @ "cancel sending ";

Break;

Case messagecomposeresultsent:

MSG = @ "sent successfully ";

Break;

Case messagecomposeresultfailed:

MSG = @ "failed to send ";

Break;

Default:

Break;

}

Uialertview * Alert = [[uialertview alloc] initwithtitle: Nil

Message: msg

Delegate: Nil

Cancelbuttontitle: @ "close"

Otherbuttontitles: Nil];

[Alert show];

}

// Email interface, which is added to window

-(Void) displaycomposersheet

{

Mfmailcomposeviewcontroller * picker = [[mfmailcomposeviewcontroller alloc] init];

[Picker setaccessibilitylanguage: @ "Chinese"];

If (! Picker ){

// Mailviewcontroller is empty when no email account is added to the device. The following present View Controller will cause the program to crash. Make a judgment here

Nslog (@ "No email account has been added to the device ");

Return;

}

Picker. mailcomposedelegate = self;

If ([[[uidevice currentdevice] systemversion] floatvalue]> = 5.0 ){

Uiview * barbgv = [[uiview alloc] initwithframe: cgrectmake (0, 0,320, 44)];

Barbgv. backgroundcolor = [uicolor colorwithred: 55/255. 0 Green: 60/255. 0 Blue: 100/255. 0 ALPHA: 1.0];

Uiimage * barbgimg = [Tool convertviewtoimage: barbgv];

[[Picker navigationbar] setbackgroundimage: barbgimg forbarmetrics: uibarmetricsdefault];

}

 

// Set up recipients

Nsarray * torecipients = [nsarray arraywithobject: staff. Email];

 

[Picker settorecipients: torecipients];

// Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "[email protected]"

// Oftype: Nil

// Indirectory: Nil];

// Nsdata * mydata = [nsdata datawithcontentsoffile: path];

// [Picker addattachmentdata: mydata mimetype: @ "image/PNG" filename: @ "icon"];

[Picker setmessagebody: Nil ishtml: Yes];

[Self presentviewcontroller: picker animated: Yes completion: ^ {

Picker. accessibilityelementshidden = yes;

}];

// [Self presentmodalviewcontroller: picker animated: Yes];

}

// Jump to the system email Interface

-(Void) launchmailappondevice

{

Nsstring * recipients = [nsstring stringwithformat: @ "mailto: % @? & Subject = ", Staff. Email];

Nsstring * Email = recipients;

Email = [email stringbyaddingpercentescapesusingencoding: nsutf8stringencoding];

 

[[Uiapplication sharedapplication] Openurl: [nsurl urlwithstring: email];

}

# Pragma mark-proxy

-(Void) mailcomposecontroller :( mfmailcomposeviewcontroller *) controller didfinishwithresult :( mfmailcomposeresult) Result error :( nserror *) Error

{

// Close the email sending window

[Self dismissviewcontrolleranimated: Yes completion: Nil];

Nsstring * MSG;

Switch (result)

{

Case mfmailcomposeresultcancelled:

MSG = @ "the user cancels email editing ";

Break;

Case mfmailcomposeresultsaved:

MSG = @ "the user successfully saves the email ";

Break;

Case mfmailcomposeresultsent:

MSG = @ "sent successfully ";

Break;

Case mfmailcomposeresultfailed:

MSG = @ "failed to save or send the email ";

Break;

Default:

Break;

}

[[[Uialertview alloc] initwithtitle: Nil message: MSG delegate: Nil cancelbuttontitle: @ "close" otherbuttontitles: nil, nil] Show];

 

}

 

Set Chinese characters:

1. You can set the attribute value of localization Native Development Region in the list attribute file of the project to: China;

2. you can set localization in the XIB that calls mfmailcomposeviewcontroller (the method is as follows: Find the corresponding XIB file, right-click the file and select get info à [General tab and click {Add localization}]) for example, Chinese Simplified {zh_cn}

3. Or on your mobile phone: Set --> General ---> multi-language environment --> language ---> Simplified Chinese;

General ---> International --> language ---> Simplified Chinese;

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.