2010年7月9日星期五

iphone 中发送短信代码(ios 4)

//MFMessageComposeViewController 只在ios 4.0 后可用

Class smsClass = NSClassFromString(@"MFMessageComposeViewController");
if (smsClass != nil){

  if ([smsClass canSendText]){
    [self displaySMSComposerSheet];
  }
 else {
     [self launchSmsAppOnDevice];
 }
}
else {
  [self launchSmsAppOnDevice];
}

-(void)launchSmsAppOnDevice
{
  UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  pasteboard.string =@"sms body";
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:// "]];
}

#pragma mark -
#pragma mark Componse sms

-(void)displaySMSComposerSheet
{

  MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
  picker.body = "sms body";
  picker.messageComposeDelegate = self;
  [self presentModalViewController:picker animated:YES];
  [picker release];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
  [self dismissModalViewControllerAnimated:YES];
}
发表评论