UIActionSheet打开邮件应用程序iPhone

问题描述:

这个问题是我难倒了,所以希望有人可以帮忙。我在一个视图上有一个UIActionSheet,它有三个选项。一个将我的用户带到一个新视图,一个通过电子邮件分享,另一个通过短信分享。UIActionSheet打开邮件应用程序iPhone

我已经创建了UIActionSheet这没有问题的作品,在AlertSheet的新视图部分也适用。我已经导入了Message.UI框架,并设置了邮件和短信收件人和作曲者,这很好。但是,我无法在UIActionSheet上设置两个“按钮”来打开邮件和短信。

通常我会通过界面生成器做到这一点,一个UIButton连接到我创建了行动,但因为这是一个UIActionSheet不能那么做。很抱歉,LONG代码,但我觉得我需要显示一切,所以请看下面;

-(IBAction)showActionSheet { 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose an Option" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Application Support",@"Share Via Email",@"Share Via SMS",nil]; 
    [actionSheet showInView:self.view]; 
    [actionSheet release]; 
} 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(buttonIndex == 0) { 
    AppSupportView *controller = [[AppSupportView alloc] initWithNibName:@"AppSupportView" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 
    } 

    if(buttonIndex == 1) { 

    } 

    if(buttonIndex == 2) { 

    } 

} 

- (void)dealloc { 
    [feedbackMsg release]; 
    [super dealloc]; 
} 

- (void)viewDidUnload { 
    self.feedbackMsg = nil; 
} 

-(IBAction)showMailPicker:(id)sender { 
    // The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 
    // So, we must verify the existence of the above class and provide a workaround for devices running 
    // earlier versions of the iPhone OS. 
    // We display an email composition interface if MFMailComposeViewController exists and the device 
    // can send emails. Display feedback message, otherwise. 
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); 

    if (mailClass != nil) { 
     //[self displayMailComposerSheet]; 
     // We must always check whether the current device is configured for sending emails 
     if ([mailClass canSendMail]) { 
      [self displayMailComposerSheet]; 
     } 
     else { 
      feedbackMsg.hidden = NO; 
      feedbackMsg.text = @"Device not configured to send mail."; 
     } 
    } 
    else { 
     feedbackMsg.hidden = NO; 
     feedbackMsg.text = @"Device not configured to send mail."; 
    } 
} 

-(IBAction)showSMSPicker:(id)sender { 
    // The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later. 
    // So, we must verify the existence of the above class and log an error message for devices 
    //  running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support 
    //  MFMessageComposeViewController API. 
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController")); 

    if (messageClass != nil) {   
     // Check whether the current device is configured for sending SMS messages 
     if ([messageClass canSendText]) { 
      [self displaySMSComposerSheet]; 
     } 
     else { 
      feedbackMsg.hidden = NO; 
      feedbackMsg.text = @"Device not configured to send SMS."; 

     } 
    } 
    else { 
     feedbackMsg.hidden = NO; 
     feedbackMsg.text = @"Device not configured to send SMS."; 
    } 
} 

// Displays an email composition interface inside the application. Populates all the Mail fields. 
-(void)displayMailComposerSheet 
{ 
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    picker.mailComposeDelegate = self; 

    [picker setSubject:@"My BMR Index Rating from Total:Health App"]; 


    // Set up recipients 
    //NSArray *toRecipients = [NSArray arrayWithObject:@""]; 

    //[picker setToRecipients:toRecipients]; 
    NSString *emailSharing = @"I Just discovered that I have a Target Heart Rate of"; 
    // Fill out the email body text 
    [picker setMessageBody:emailSharing isHTML:YES]; 

    [self presentModalViewController:picker animated:YES]; 
    [picker release]; 
} 

// Displays an SMS composition interface inside the application. 
-(void)displaySMSComposerSheet 
{ 
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; 
    picker.messageComposeDelegate = self; 
    NSString *SMSShare = @"I Just discovered that I have a Target Heart Rate of"; 
    // Fill out the email body text 
    picker.body = SMSShare; 

    [self presentModalViewController:picker animated:YES]; 
    [picker release]; 
} 

// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the 
// message field with the result of the operation. 
- (void)mailComposeController:(MFMailComposeViewController*)controller 
      didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 

    feedbackMsg.hidden = NO; 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      feedbackMsg.text = @"Result: Mail sending canceled"; 
      break; 
     case MFMailComposeResultSaved: 
      feedbackMsg.text = @"Result: Mail saved"; 
      break; 
     case MFMailComposeResultSent: 
      feedbackMsg.text = @"Result: Mail sent"; 
      break; 
     case MFMailComposeResultFailed: 
      feedbackMsg.text = @"Result: Mail sending failed"; 
      break; 
     default: 
      feedbackMsg.text = @"Result: Mail not sent"; 
      break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 


// Dismisses the message composition interface when users tap Cancel or Send. Proceeds to update the 
// feedback message field with the result of the operation. 
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 
       didFinishWithResult:(MessageComposeResult)result { 

    feedbackMsg.hidden = NO; 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MessageComposeResultCancelled: 
      feedbackMsg.text = @"Result: SMS sending canceled"; 
      break; 
     case MessageComposeResultSent: 
      feedbackMsg.text = @"Result: SMS sent"; 
      break; 
     case MessageComposeResultFailed: 
      feedbackMsg.text = @"Result: SMS sending failed"; 
      break; 
     default: 
      feedbackMsg.text = @"Result: SMS not sent"; 
      break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 

@end 

的问题是很明显,我不知道如何与(如果buttonIndex == 1)等的代码打开邮件和短信

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(buttonIndex == 0) { 
    AppSupportView *controller = [[AppSupportView alloc] initWithNibName:@"AppSupportView" bundle:nil]; 
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 
    } 

    if(buttonIndex == 1) { 

    } 

    if(buttonIndex == 2) { 

    } 

} 

任何帮助,将不胜感激继续。

感谢

看起来像你所有需要的方法有已经.. 只需添加[self showMailPicker:nil][self showSMSPicker:nil]

if(buttonIndex == 1) { 

} 

if(buttonIndex == 2) { 

} 

如果您从上第二个按钮是您的短信按钮,添加showSMSPicker到buttonIndex == 1

+0

非常感谢你,我知道这会很简单。我以前把[自己showMailPicker];但没有包含:无,并得到一个方法不存在,与应用程序崩溃恢复为ID错误。这工作完美。谢谢 – 2011-06-05 21:31:22

+0

如果你不使用Interface Builder中的这些方法,你也可以将方法声明从“ - (IBAction)showMailPicker:(id)sender;”到“ - (void)showMailPicker;”..然后你可以用[self showMailPicker]来调用这些方法; – Sascha 2011-06-05 21:33:49

+0

啊,那是我的错误。我确实将它们从IBAction更改为void,但是我离开了:(id)sender;在上面。再次感谢。 – 2011-06-05 21:38:40