繁体   English   中英

UIActionSheet 打开邮件应用程序 iPhone

[英]UIActionSheet to open up Mail Application iPhone

这个问题让我很困惑,所以希望有人能提供帮助。 我在一个视图上有一个 UIActionSheet,其中包含三个选项。一个将我的用户带到一个新视图,一个通过 email 共享,一个通过 SMS 共享。

我创建了 UIActionSheet,它可以正常工作,AlertSheet 的新视图部分也可以工作。 我已经导入了 Message.UI 框架并设置了邮件和 SMS 选择器和作曲器,这些都很好。 但是,我无法在 UIActionSheet 上设置两个“按钮”来打开邮件和 SMS。

通常我会通过界面生成器执行此操作,并将 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 showSMSPicker:nil]

if(buttonIndex == 1) {

}

if(buttonIndex == 2) {

}

如果您从顶部开始的第二个按钮是您的短信按钮,请将 showSMSPicker 添加到 buttonIndex == 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM