0

我正在创建一个应用程序,一旦导航到页面并选择了音调,用户就可以从各个页面下载声音文件并将其通过电子邮件发送给自己。

我想知道是否有人知道如何连接每个页面上的下载按钮并将其连接到邮件编写器。我已经在我的联系页面上找到了邮件编写器,但是当我尝试将邮件编写器连接到每个页面上的下载按钮时,没有任何反应,我尝试使用标签并且仍然丢失任何帮助将不胜感激。提前致谢

4

1 回答 1

0

我假设下载按钮是您的按钮。我假设每个“页面”都是不同的视图控制器?

如果您想真正高效,您将创建一个单独的类来保存您的邮件编写器逻辑。

为了快速修复,您可以将按钮连接到视图控制器中的 IBAction,然后将邮件编写器逻辑放在那里或从那里调用它的方法。

这是一个简单的例子:

- (IBAction)sendProblemReport:(UIButton*)sender {  //note: connect your button here 

    UIButton *button;
    if ([sender isKindOfClass:[UIButton class]]) {
       button = sender;
    }
    else {
       return;  //leave if not a button
    }

       //note: if you do it this way be sure to set tag values in interface builder on your button.  The other way is to have an action for each button.

       switch (button.tag) {
       case 1:
         NSURL  *fileURL1 = [[NSURL alloc] initFileURLWithPath:@"YOUR FILE PATH HERE"];
         NSData *soundfile = [[NSData alloc] initWithContentsOfURL:fileURL1];
         NSString *fileTitle = @"YOUR Nice File Name";  //sound.mp3
         [self showDiagMailSheetAttachingSoundFile:soundFile usingFileName:filename];
        break; 
        }
 }

- (void)showDiagMailSheetAttachingSoundFile:(NSData*)soundFile usingFileName:(NSString*)fileName {

    MFMailComposeViewController *diagMail = [[MFMailComposeViewController alloc]init];
    [diagMail setMailComposeDelegate:self];

        if ([MFMailComposeViewController canSendMail]) {
        [diagMail setToRecipients:@[@"fromaddress@gmail.com"]];
        [diagMail setCcRecipients:nil];
        [diagMail setBccRecipients:nil];
        [diagMail setSubject:@"subject message"];        
        [diagMail setMessageBody:nil isHTML:NO];

        [diagMail addAttachmentData:soundFile file mimeType:@"audio/mpeg" fileName:fileName];  //note: not sure of the mime type you need.

         diagMail.modalPresentationStyle = UIModalPresentationCurrentContext;
         [self presentViewController:diagMail animated:YES completion:nil];
     }
  }
  - (void)mailComposeController:(MFMailComposeViewController*)controller
      didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {

    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
        // @"Result: Mail sending canceled";
        break;
    case MFMailComposeResultSaved:
        // @"Result: Mail saved";
        break;
    case MFMailComposeResultSent:
        // @"Result: Mail sent";
        break;
    case MFMailComposeResultFailed:
        // @"Result: Mail sending failed";
        break;
    default:
        // @"Result: Mail not sent";
        break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
      [self cancel:self];
    }];
 }

注意:要将它放在一个单独的子类中,您可以使用该类简单地返回一个“MFMailComposeViewController”,然后将它呈现在每个视图控制器中。这种方法更有效,并且允许您在多个地方调用相同的代码。

好的,希望有帮助。如果不让我知道。

于 2013-03-15T00:06:17.077 回答