在iPhone应用中开发中,可以使用MFMessageComposeViewController标准短信SMS发送界面来完成。在使用之前,需要实现MFMessageComposeViewControllerDelegate代理,这个代理提供了短信发送相关的接口。
1
2
3
4
5
6
7
8
9
|
#import <UIKit/UIKit.h> #import <MessageUI/MFMessageComposeViewController.h> <a class = "referer" href= target= "_blank" >@interface</a> TestViewController : UIViewController <MFMessageComposeViewControllerDelegate> { UIButton *buttonSMS; } @end |
在TestViewController实现体中编写发送短信的方法sendSMS,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
- ( void )sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients { MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; if ([MFMessageComposeViewController canSendText]) { controller.body = bodyOfMessage; controller.recipients = recipients; controller.messageComposeDelegate = self; [self presentModalViewController:controller animated:YES]; } } // 处理发送完的响应结果 - ( void )messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissModalViewControllerAnimated:YES]; if (result == MessageComposeResultCancelled) NSLog(@ "Message cancelled" ) else if (result == MessageComposeResultSent) NSLog(@ "Message sent" ) else NSLog(@ "Message failed" ) } |