编程实现iPhone发送短信SMS

分享到:

在iPhone应用中开发中,可以使用MFMessageComposeViewController标准短信SMS发送界面来完成。在使用之前,需要实现MFMessageComposeViewControllerDelegate代理,这个代理提供了短信发送相关的接口。

#import

#import

@interface TestViewController : UIViewController

{
  UIButton *buttonSMS;
}
 
@end


在TestViewController实现体中编写发送短信的方法sendSMS,如下:

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

sendSMS:方法中打开MFMessageComposeViewController发送短信界面:

编程实现iPhone发送短信SMS

文章出处:http://www.ctolive.com/space-1023-do-blog-id-2095.html

昵    称:
验证码:

相关文档: