In APP development, phone calls, text messages, emails and other functions may be involved.

There are three ways to make a call: 1.1: Directly dial to switch to the dialing screen

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", @ "The number you need to call"]]];Copy the code

The second 1.2: prompt dialing (apple native) will pop up the box to ask before dialing, after the call can automatically return to the original application.

1.2.1 create a UIWebView to load the URL. After dialing, it will automatically return to the original application. Recommend using this solution!

NSMutableString * STR =[[NSMutableString alloc] initWithFormat:@"tel:%@"The @"The number you need to call"];
 UIWebView * callWebview = [[UIWebView alloc] init];
 [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self.view addSubview:callWebview];
Copy the code

1.2.2 Disadvantages: Proprietary API, so it may not pass apple’s official review. If you’re an enterprise app (which doesn’t require an appStore), you can use this method.

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@"The @"The number you need to call"]]. / / call openURL:if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
 }
Copy the code

The third type 1.3: Pop-up prompt dialing (modified according to the UI design interface) needs to encapsulate the customized pop-up file: QTXAlterView

@property (nonatomic, strong) QTXAlterView *telAlterView; QTXAlterView *alter = [[QTXAlterView alloc] initWithMessage:[NSString stringWithFormat:@"The number you need to call"] delegate:self rightButtonTitle:@"Sure" otherButtonTitles:@"Cancel"];
    [alter show];
    self.telAlterView = alter;
Copy the code
#pragma mark - QTXAlterViewDelegate

- (void)alertView:(QTXAlterView *)alertView clickedAtIndex:(NSInteger)buttonIndex {
    if(alertView == self.telAlterView) {// Make a callif (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", @ "The number you need to call"]]]; }}}Copy the code

##2. There are two ways to send SMS messages: ** (1) ** Directly jumps to the interface of sending SMS messages. Disadvantages: You cannot define the content of sending SMS messages, and cannot automatically return to the original application after sending SMS messages.

NSURL *url = [NSURL URLWithString:@"sms://10010"]; [[UIApplication sharedApplication] openURL:url];Copy the code

** (2) ** Use MessageUI framework to send SMS messages, the second method is recommended.

The header file needs to be included#import <MessageUI/MessageUI.h>MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc]init]; // Set the SMS content vc.body = @"Did you eat?"; // Set the recipient list vc.recipients = @[@"10010"The @"10086"]; / / set agent vc. MessageComposeDelegate = self; // Display controller [self presentViewController: VC Animated :YES completion:nil]; // Implement the proxy function: Click on the cancel button will automatically call the - (void) messageComposeViewController (MFMessageComposeViewController *) controller didFinishWithResult:(MessageComposeResult)result { [controller dismissViewControllerAnimated:YES completion:nil]; }Copy the code

##3. There are two ways to send email: ** (1) ** Use your own email client. Cons: Email does not automatically return to the original app

NSURL *url = [NSURL URLWithString:@"mailto://[email protected]"]; [[UIApplication sharedApplication] openURL:url];Copy the code

** (2) ** Similar to the second method of texting, use MessageUI,

if(! [MFMailComposeViewController canSendMail])return; MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init]; // Set the email subject [vc]setSubject:@"Test mail"]; // Set the email content [vc]setMessageBody:@"Test content"isHTML:NO]; // Set the recipient list [vc]setToRecipients:@[@"[email protected]"]]. // Set the cc list [vc]setCcRecipients:@[@"[email protected]"]]. // Set agent vc.mailComposeDelegate = self; // Display controller [self presentViewController: VC Animated :YES completion:nil]; // Implement the proxy method:  - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result  error:(NSError *)error { [controller dismissViewControllerAnimated:YES completion:nil]; }Copy the code