iOS程序内发短信
Posted Mr_tao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS程序内发短信相关的知识,希望对你有一定的参考价值。
1、程序外发短信
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@sms://10086]];
2、程序内发短信,发完短信自动返回应用
(1)首先导入框架MessageUI.framework
(2) 引入头文件 #import <MessageUI/MessageUI.h>
(3) 实现代理方法 <MFMessageComposeViewControllerDelegate, UINavigationControllerDelegate>
3、直接上代码
- (void)showMessageView{ if ([MFMessageComposeViewController canSendText]) { MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init]; controller.recipients = [NSArray arrayWithObject:@"10086"]; controller.body = @"测试发短信"; controller.messageComposeDelegate = self; [self presentViewController:controller animated:YES completion:nil]; [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"]; }else{ [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"]; } }
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ [controller dismissViewControllerAnimated:YES completion:nil]; switch (result) { case MessageComposeResultCancelled: [self alertWithTitle:@"提示信息" msg:@"发送取消"]; break; case MessageComposeResultFailed: [self alertWithTitle:@"提示信息" msg:@"发送失败"]; break; case MessageComposeResultSent: [self alertWithTitle:@"提示信息" msg:@"发送成功"]; break; default: break; } } - (void) alertWithTitle:(NSString *)title msg:(NSString *)msg { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; [alert show]; }
以上是关于iOS程序内发短信的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Xamarin iOS 应用程序中将文本共享到电子邮件或短信中