簡體   English   中英

如何在Xcode中的應用程序內發送電子郵件?

[英]How to send an E-Mail inside of an app in Xcode?

我是xcode的新手,我想知道如何在應用程序中發送電子郵件! 我的代碼如下,但我不斷收到錯誤“沒有可見@interface for'jakem'聲明選擇器'presentViewControllerAnimated:'”。 我的代碼完全錯了嗎? 或者我只是忘記聲明選擇器,我如何聲明選擇器? 我已經在互聯網上研究了至少一個小時,但沒有任何工作。 有人請幫幫我!

    -(IBAction)sendEmail{

    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    [composer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
    [composer setToRecipients:[NSArray          arrayWithObjects:@"FrankMurphy.CEO@RomansXIII.com", nil]];
    [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:composer animated:YES];

    }

    }

    -(void)mailComposeController:(MFMailComposeViewController *)controller   didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    if(error) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:[NSString    stringWithFormat:@"error %@", [error description]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil];
    [alert show];
    [self dismissViewControllerAnimated:YES];
    }
    else {
    [self dismissViewControllerAnimated:YES];
    }
    }

在.h頭文件....

 #import <UIKit/UIKit.h>


#import <MessageUI/MessageUI.h>



@interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate
- (IBAction)showEmail:(id)sender;



@end

在.m實現文件中.....

- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = @"Test Email";
// Email Content
NSString *messageBody = @"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"info@finetechnosoft.in"];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];

// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}




- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{



switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}


// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}

檢查您是否是MFMailComposeViewControllerDelegate。 你這樣做

@interface YouClassName : UIViewController <MFMailComposeViewControllerDelegate> 

@end

我認為你使用的是錯誤的方法。 嘗試

[self presentViewController:(UIViewController *) animated:(BOOL) completion:(void)completion];

代替:

[self presentViewController:composer animated:YES];

我為Sendgrid工作。 我們有一個Objective-c庫,可讓您快速從應用程序內部發送電子郵件, https://github.com/sendgrid/sendgrid-objc 您可以使用cocoapods在項目中快速安裝庫。

然后從您的(IBAction)發送電子郵件將如下所示:

-(IBAction)sendEmail{

sendgrid *msg = [sendgrid user:@"username" andPass:@"password"];
msg.to = @"FrankMurphy.CEO@RomansXIII.com";
msg.from = @"me@bar.com";
msg.text = @"hello world";   
msg.html = @"<h1>hello world!</h1>";

[msg sendWithWeb];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM