iOS-打电话、发短信、发邮件、打开浏览器

时间:2022-06-25 02:14:55

- (IBAction)showAlert:(UIButton *)sender {

NSString *phoneNumber=@"18500138888";

NSString *url=[NSString stringWithFormat:@"tel://%@",phoneNumber];

[self openUrl:url];

}

- (IBAction)sendMsg:(UIButton *)sender {

NSString *phoneNumber=@"18500138888";

NSString *url=[NSString stringWithFormat:@"sms://%@",phoneNumber];

[self openUrl:url];

}

- (IBAction)sendEmail:(UIButton *)sender {

NSString *mailAddress=@"kenshin@hotmail.com";

NSString *url=[NSString stringWithFormat:@"mailto://%@",mailAddress];

[self openUrl:url];

}

- (IBAction)openBrowser:(UIButton *)sender {

NSString *url=@"http://www.cnblogs.com/kenshincui";

[self openUrl:url];

}

- (void)openUrl:(NSString *)urlStr {

NSURL *url = [NSURL URLWithString:urlStr];

UIApplication *application = [UIApplication sharedApplication];

if (![application canOpenURL:url]) {

NSLog(@"无法打开\"%@\",请确保此应用已经正确安装",url);

return;

}

[[UIApplication sharedApplication] openURL:url];

}