A simple example of email and SMS delivery in IOS development _ios

Source: Internet
Author: User

Send mail
1. Import library file: Messageui.framework
2. Introduction of header Files
3. Implement Agent <MFMailComposeViewControllerDelegate> and <UINavigationControllerDelegate>
code example:

Copy Code code as follows:

-(void) didclicksendemailbuttonaction{

if ([mfmailcomposeviewcontroller cansendmail] = YES) {

Mfmailcomposeviewcontroller *MAILVC = [[Mfmailcomposeviewcontroller alloc] init];
Set Agent (unlike the previous agent, not "delegate", must not forget Ah, Agent has 3 steps)
Mailvc.mailcomposedelegate = self;
Recipient
Nsarray *sendtoperson = @[@ "humingtao2014@gmail.com"];
[MAILVC Settorecipients:sendtoperson];
Cc
Nsarray *copytoperson = @[@ "humingtao2013@126.com"];
[MAILVC Setccrecipients:copytoperson];
Secret Send
Nsarray *secrettoperson = @[@ "563821250@qq.com"];
[MAILVC Setbccrecipients:secrettoperson];
Theme
[MAILVC setsubject:@ "Hello World"];
[Self PRESENTVIEWCONTROLLER:MAILVC animated:yes completion:nil];
[MAILVC setmessagebody:@ "legacy, haha hehe rattling" ishtml:no];
}else{

NSLog (@ "This device does not support mail forwarding");

}

}

-(void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: (mfmailcomposeresult) Result Error: (Nserror *) error{

Switch (Result) {
Case mfmailcomposeresultcancelled:
NSLog (@ "Cancel sending");
Break
Case mfmailcomposeresultfailed:
NSLog (@ "send failed");
Break
Case mfmailcomposeresultsaved:
NSLog (@ "save draft document");
Break
Case Mfmailcomposeresultsent:
NSLog (@ "send success");
Break
Default
Break
}

[Self dismissviewcontrolleranimated:yes completion:nil];
}

The system sends, the simulator does not support, must use the real machine test
-(void) didclicksendsystememailbuttonaction{

Nsurl *url = [Nsurl urlwithstring:@ "humingtao2014@gmail.com"];
if ([[[UIApplication sharedapplication] canopenurl:url] = YES) {

[[UIApplication sharedapplication] openurl:url];

}else{

NSLog (@ "This device is not supported");
}

}

Send SMS
the previous three steps introduce configuration and send as mail

Copy Code code as follows:

Call system API Send SMS
-(void) didclicksendmessagebuttonaction{

if ([mfmessagecomposeviewcontroller cansendtext] = YES) {

Mfmessagecomposeviewcontroller *MESSAGEVC = [[Mfmessagecomposeviewcontroller alloc] init];
Set up Agent <MFMessageComposeViewControllerDelegate>
Messagevc.messagecomposedelegate = self;
Send to WHO
messagevc.recipients = @[@ "18757289870"];
Messagevc.body = @ "Hello world";
[Self PRESENTVIEWCONTROLLER:MESSAGEVC animated:yes completion:nil];

}else{

NSLog (@ "This device is not supported");
}
}

-(void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *) controller didfinishwithresult: ( Messagecomposeresult) result{

Switch (Result) {
Case messagecomposeresultcancelled:
NSLog (@ "Cancel sending");
Break
Case messagecomposeresultfailed:
NSLog (@ "send failed");
Break
Case Messagecomposeresultsent:
NSLog (@ "send success");
Break
Default
Break
}

[Self dismissviewcontrolleranimated:yes completion:nil];

}

Calling the system application to send a message
-(void) didclicksendmessage2buttonaction{

Nsurl *url = [Nsurl urlwithstring:@ "sms:18656348970"];
if ([[[UIApplication sharedapplication] canopenurl:url] = YES) {

[[UIApplication sharedapplication] openurl:url];

}else{

NSLog (@ "failure");
}

}

Related Article

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.