Just a Computer Graphics Studio & My Life

[iOS] 寄信 (Send Mail)

我們所製作的APP,總希望有使用者回饋,來改善使用者體驗,此時可以使用寄信 (Send Mail)的方式,只要使用者有設定電子信箱,即可在APP裡面傳送信件到我們指定的電子信箱。

iOS 寄信 Send Mail

先在專案中加入框架:MessageUI Framework。接著引入.h檔:MessageUI/MessageUI.h。記得遵從協定MFMailComposeViewControllerDelegate。

-(IBAction)submitButtonClicked:(UIButton *)button
{
    // 先檢查是否有信箱帳號
    if (![MFMailComposeViewController canSendMail]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No e-mail account"
                                                        message:@"Please set your e-mail account first."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];
        return;
    }
    // 建立物件並指定代理
    MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
    mailComposeViewController.mailComposeDelegate = self;

    NSArray *toAddressList = @[@"happyman.cg@gmail.com"];
    NSArray *ccAddressList = @[@"happyman.cg@gmail.com"];
    NSArray *bccAddressList = @[@"happyman.cg@gmail.com"];

    mailComposeViewController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

    // 設定收件人與主旨等資訊
    [mailComposeViewController setToRecipients:toAddressList];
    [mailComposeViewController setCcRecipients:ccAddressList];
    [mailComposeViewController setBccRecipients:bccAddressList];
    [mailComposeViewController setSubject:@"For HappyMan :)~"];

    // 設定內文並且使用HTML語法
    NSString *siteLink = @"https://cg2010studio.wordpress.com/";
    NSString *blogLink = @"https://cg2010studio.wordpress.com/";
    NSString *emailBody =
    [NSString stringWithFormat:@"%@\n%@\n", siteLink, blogLink];

    [mailComposeViewController setMessageBody:emailBody isHTML:NO];

    // 加入圖片
    UIImage *theImage = [UIImage imageNamed:@"cat pic.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(theImage, 1.0);
    [mailComposeViewController addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"image.jpg"];

    // 顯示電子郵件畫面
    [self presentViewController:mailComposeViewController animated:YES completion:^{}];
}

內文除了可以有文字之外,還可以加入圖片,甚至還可以使用HTML的語法,豐富地表現信件內容。

iOS 寄信 (Send Mail)

接著寫delegate來處理相對應的行為。

#pragma mark - MFMailComposeViewControllerDelegate Methods
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultSaved:
            break;
        case MFMailComposeResultSent:
            break;
        case MFMailComposeResultFailed:
            break;

        default:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email"
                                                            message:@"Sending Failed - Unknown Error:("
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles: nil];
            [alert show];
        }
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{}];
}

參考:MFMailComposeViewController 的基本使用方法How to attach an image from your photo gallery and send it as an image on iPhone

Advertisements

Comments on: "[iOS] 寄信 (Send Mail)" (1)

  1. […] 之前已試過寄送信件 (Send Mail),這次則要實作傳送簡訊 (Send Message),方式幾乎一模一樣! […]

    喜歡

發表留言

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

標籤雲

%d 位部落客按了讚: