在iPhone應用中開發中,可使用MFMessageComposeViewController標準短信SMS發送界面來完成。在使用以前,須要實現MFMessageComposeViewControllerDelegate代理,這個代理提供了短信發送相關的接口。ide
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,以下:spa
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"
)
}
|