UIActionSheet打开邮件应用程序iPhone

UIActionSheet打开邮件应用程序iPhone,iphone,objective-c,xcode,uiactionsheet,mfmailcomposeviewcontroller,Iphone,Objective C,Xcode,Uiactionsheet,Mfmailcomposeviewcontroller,这个问题一直困扰着我,希望有人能帮上忙。我在视图上有一个UIActionSheet,其中有三个选项。一个将我的用户带到新视图,一个通过电子邮件共享,一个通过短信共享 我创建了UIActionSheet,它可以正常工作,AlertSheet的新视图部分也可以工作。我已经导入了Message.UI框架,并设置了邮件和短信选择器以及编写器,这些都很好。但是,我在设置UIActionSheet上的两个“按钮”以打开邮件和短信时遇到了问题 通常我会通过interface builder完成这项工作,并将一

这个问题一直困扰着我,希望有人能帮上忙。我在视图上有一个UIActionSheet,其中有三个选项。一个将我的用户带到新视图,一个通过电子邮件共享,一个通过短信共享

我创建了UIActionSheet,它可以正常工作,AlertSheet的新视图部分也可以工作。我已经导入了Message.UI框架,并设置了邮件和短信选择器以及编写器,这些都很好。但是,我在设置UIActionSheet上的两个“按钮”以打开邮件和短信时遇到了问题

通常我会通过interface builder完成这项工作,并将一个UIButton连接到我创建的操作,但由于这是一个UIActionSheet,所以不能这样做。很抱歉代码太长,但我觉得我需要显示所有内容,请参见下面的内容

-(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
问题很明显,我不知道如何使用(if 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-showmspicker:nil]
添加到

if(buttonIndex == 1) {

}

if(buttonIndex == 2) {

}

如果顶部的第二个按钮是您的sms按钮,请将showSMSPicker添加到buttonIndex==1

非常感谢,我知道这会很简单。我以前把[自我展示邮件选择器];但未包含:nil且正在获取的方法不存在,将恢复为id错误并导致应用程序崩溃。这非常有效。谢谢如果您不在Interface Builder中使用这些方法,您也可以将方法声明从“-(iAction)showMailPicker:(id)sender;”更改为“-(void)showMailPicker;”。。然后您可以使用[self-showMailPicker]调用这些方法;啊,那是我的错误。我确实将它们从iAction更改为void,但是我离开了:(id)发送者;在上面。再次感谢。