iOS实现APP调用打电话,发短信,发邮件,打开Safari

时间:2022-11-10 12:57:33

打电话:

- (IBAction)phone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://123456789"]];
}

打开Safari:

- (IBAction)Safari:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com.hk"]];
}

发短信:
发短信需要导入#import <MessageUI/MFMessageComposeViewController.h>头文件,并遵守MFMessageComposeViewControllerDelegate代理,实现如下步骤,

- (IBAction)msg:(id)sender {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://123456789"]];
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

if([MFMessageComposeViewController canSendText])

{

controller.body = @"hhhhHhhhhhhhhhhhhhhh";//信息内容

controller.recipients = @[@"123456789"];//接收者,是一个数组,可以发送给多人

controller.messageComposeDelegate = self;


[self presentViewController:controller animated:YES completion:nil];
}

}

发送短信完成的代理方法:

- (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");

}

发邮件:
发邮件需要导入#import <MessageUI/MFMailComposeViewController.h>头文件,并遵守MFMailComposeViewControllerDelegate代理,实现如下步骤,

- (IBAction)mail:(id)sender {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];

if([MFMailComposeViewController canSendMail])

{

[controller setSubject:@"send email!!!"];//邮件主题

[controller setToRecipients:@[@"123456789@qq.com"]];//收件人

[controller setCcRecipients:@[@"12345678@qq.com"]];//抄送

[controller setMessageBody:@"hello world!" isHTML:NO];

controller.mailComposeDelegate = self;

[self presentViewController:controller animated:YES completion:nil];

}
}

发送邮件完成的代理方法:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

[self dismissViewControllerAnimated:YES completion:nil];

if (result == MessageComposeResultCancelled)
NSLog(@"Message cancelled");
else if (result == MessageComposeResultSent)
NSLog(@"Message sent");
else
NSLog(@"Message failed");

}