How to switch to texting, calling, and sending emails in iOS

Source: Internet
Author: User
Tags mail account

When we do the app, we will inevitably encounter the need to call SMS, telephone and other procedures. such as American Regiment.

Of course, these are just a few simple methods can be achieved, but the time will be forgotten, so want to write this blog. One is to pick up again to review, another equivalent to leave a backup, later forget, you can pick up to see.

First, say how to call the browser to open the Web page.

Of course, this is very simple, it takes just two words to achieve

Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];

//

[[UIApplication sharedapplication] openurl:url];

Send e-mail

Before we write this, we must first import the Messageui.framework library. and introduce two header files

and introduce a header file

#import <MessageUI/MFMailComposeViewController.h>

Sign an agreement

Mfmailcomposeviewcontrollerdelegate

The actual code is as follows

Class Mailclass = (nsclassfromstring (@ "Mfmailcomposeviewcontroller"));

if (Mailclass = = nil) {//first determine if support

NSLog (@ "Device does not support mail sending function");

}

The Apple system provides an interface for writing the Mail interface, and Mfmailcomposeviewcontroller is used to display the interface

Determine if the user is setting up a mail account

if (![ Mfmailcomposeviewcontroller Cansendmail]) {

NSLog (@ "The user does not have a mailbox account set up");

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Send results" message:@ "The user does not have a mailbox account set up" delegate:self cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil];

[Alertview show];

Return

}

Mfmailcomposeviewcontroller inherited from Uinavigationcontroller

Mfmailcomposeviewcontroller *controller = [[Mfmailcomposeviewcontroller alloc] init];

Message subject

[Controller setsubject:@ "My hardware Research"];

Send a message to .....

[Controller settorecipients:@[@ "[email protected]"];

CC: For example, mail to the technical department, but also want to send to the Ministry of Security, Finance department, you will be in the CC address of the Department of Defense, Finance department they will receive together. Called CC

Secret delivery: such as mail to the technical department, but you also want to send a letter to the general manager, but do not want to let the technical department know, you send the address in the dark to fill the general manager, he will receive and the technical department will not know

As an example,

if: A send mail to B1, B2, B3, CC to C1, C2, C3, secret to D1, D2, D3.

So:

A know that they sent the mail to B1, B2, B3, and copied to C1, C2, C3, secret to D1, D2, D3.

B1 know this letter is a sent to B1, B2, B3 Mail, and copied to C1, C2, C3, but do not know the secret to D1, D2, D3.

C1 know this letter is a sent to B1, B2, B3 Mail, and copied to C1, C2, C3, but do not know the secret to D1, D2, D3.

D1 know this is a sent to B1, B2, B3 Mail, and copied to C1, C2, C3, and secret to himself, but do not know the secret to D2, D3.

Cc

[Controller setccrecipients:@[@ "[email protected]"];

Secret delivery

[Controller setbccrecipients:@[@ "[email protected]"];

Main content of the message

[Controller setmessagebody:@ "edit" Ishtml:yes];

Controller.mailcomposedelegate = self;

[Self Presentviewcontroller:controller animated:yes completion:nil];

Agent method for sending mail

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

{

NSString *str = nil;

if (result = = mfmailcomposeresultfailed) {

str = @ "Send Failed";

}else if (result = = mfmailcomposeresultcancelled) {

str = @ "Send Cancel";

}else if (result = = Mfmailcomposeresultsent)

{

str = @ "sent successfully";

}else

{

str = @ "Send save";

}

[Self dismissviewcontrolleranimated:yes completion:nil];

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Send results" message:str delegate:self cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil];

[Alertview show];

}

Texting

1. Introduction of the MESSAGEUI framework

2. Import the Messageui/mfmessagecomposeviewcontroller.h header file

3. Signing Agreement Mfmessagecomposeviewcontrollerdelegate

4. Determine whether the user device can send text messages if not, return if you can, instantiate an SMS editor controller

5. Sign an agent

6. Modal to SMS Edit Controller view, edit SMS, send

7. Use the Proxy method to listen to send, if the send is completed, recycle the Edit controller view, prompt to send results

Function:

1. Can be returned after sending out information

2. Can send multiple people at once

3. Messages can be customized to support HTML format

Phone on the information set to open the IMessage function, the other side is also the iphone, information does not go carrier channel, but the network channel

The code is as follows:

To determine whether the user device can send information can not be returned

if (![ Mfmessagecomposeviewcontroller Cansendtext]) {

Return

}

Instantiate a controller

Mfmessagecomposeviewcontroller *messagecontroller = [[Mfmessagecomposeviewcontroller alloc] init];

Set SMS Content

Recipient

messagecontroller.recipients = @[@ "1800000000"];

SMS Content

Messagecontroller.body = @ "Call you two big uncle, can";

Messagecontroller.messagecomposedelegate = self;

Mode out Edit SMS Controller

[Self Presentviewcontroller:messagecontroller animated:yes completion:nil];

Then the agent.

Call agent method to close window after send completes

-(void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *) controller didfinishwithresult: ( Messagecomposeresult) Result

{

NSString *str = @ "Result";

Three results sent unsuccessful cancellation

if (result = = Messagecomposeresultsent) {

str = @ "sent successfully";

}else if (result = = messagecomposeresultfailed)

{

str = @ "Send Failed";

}else

{

str = @ "Send Canceled";

}

Mode the controller back

[Self dismissviewcontrolleranimated:yes completion:nil];

Prompt to send results

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Send results" message:str delegate:self cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil];

[Alertview show];

}

Call

There are three ways to call:

1. After the call is not back to the reference will stay in the call log, not back to your application

Nsurl *url = [Nsurl urlwithstring:@ "tel://10086"];

//

[[UIApplication sharedapplication] openurl:url];

NSLog (@ "hit");

2. Using the TELPROMPT protocol, can back the reference, hang up the phone will go back to your application, but this agreement belongs to Apple's private agreement, if the application of the Protocol, the app is not available, for the jailbreak of the machine's development system, you can refer to this Protocol

Nsurl *url = [Nsurl urlwithstring:@ "telprompt://10086"];

[[UIApplication sharedapplication] openurl:url];

3. Use WebView to refer back and be legal, recommended

Self.webview = [[UIWebView alloc] initWithFrame:self.view.bounds];

Lazy loading, also known as deferred loading, is not loaded (overriding the Get method) when needed, (it takes up less memory)

The role of lazy loading

if (Self.webview = = nil) {

Self.webview = [[UIWebView alloc] init];

}

NSLog (@ "%p", Self.webview);

Nsurl *url = [Nsurl urlwithstring:@ "tel://10086"];

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

[Self.webview Loadrequest:request];

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "message" message:@ "Call over" Delegate:self cancelbuttontitle:@ " Determine "otherbuttontitles:nil, nil";

[Alertview show];

 

How to switch to texting, calling, and sending emails in iOS

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.