iOS5編程--官方例子代碼研究--6.MailComposer

本文檔版權歸NickTang所有,沒有本人書面或電子郵件允許,不許轉載,摘錄,發表。多謝!

我們很多時候需要在iOS程序中內置郵件發送功能,而這個例子基本上提供了我們所需要的所有功能。
注意一下幾點
1.這個程序必須在設備上運行,關於如何在設備上運行程序,不是本文需要講的,我會在以後的文章中講述。
2.你的設備必須配置好一個郵件帳戶,不然你沒法看到好的效果。
3.關於如何發送多個附件,我會在最後提到。
4.基本的代碼不會再分析,基礎部分請看我前面的文章。
5.在你自己建立的工程中,比如加入後面這個framework:MessageUI.framework
分析代碼如下:
1.

-(IBAction)showPicker:(id)sender

{

// This sample can run on devices running iPhone OS 2.0 or later  

// 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.

// We launch the Mail application on the device, otherwise.

//上面的解釋的非常清楚,關於這些類和api在那個版本中引入,必須加以關注。

//下面是一個動態運行期查詢類名的一個很好的例子,不解釋了,因爲在前面的文章有解釋到。

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

if (mailClass != nil)

{

// We must always check whether the current device is configured for sending emails

if ([mailClass canSendMail])

{

[self displayComposerSheet];

}

else

{

[self launchMailAppOnDevice];

}

}

else

{

[self launchMailAppOnDevice];

}

}

所以重點關注函數displayComposerSheet

-(void)displayComposerSheet 

{

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

[picker setSubject:@"Hello from California!"];


// Set up recipients

NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 

NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

[picker setToRecipients:toRecipients];

[picker setCcRecipients:ccRecipients];

[picker setBccRecipients:bccRecipients];

// Attach an image to the email

NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];

    NSData *myData = [NSData dataWithContentsOfFile:path];

[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

//代碼非常清楚,我也不加以解釋了,注意下面這個註釋掉的地方,很多人問如何發送多個附件,就是下面演示的這樣就可以多加一個附件,

//既多次調用addAttachmentData:fileName:

/*path = [[NSBundle mainBundle] pathForResource:@"circle" ofType:@"png"];

    myData = [NSData dataWithContentsOfFile:path];

[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"circle"];*/

// Fill out the email body text

NSString *emailBody = @"It is raining in sunny California!";

[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];

    [picker release];

}


在點擊發送後,協議

MFMailComposeViewControllerDelegate

的下面這個函數會被調用。

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

{

message.hidden = NO;

// Notifies users about errors associated with the interface

switch (result)

{

case MFMailComposeResultCancelled:

message.text = @"Result: canceled";

break;

case MFMailComposeResultSaved:

message.text = @"Result: saved";

break;

case MFMailComposeResultSent:

message.text = @"Result: sent";

break;

case MFMailComposeResultFailed:

message.text = @"Result: failed";

break;

default:

message.text = @"Result: not sent";

break;

}

[self dismissModalViewControllerAnimated:YES];

}


上面的這個代碼也是很簡單的,所以也不再解釋。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章