ios實現發郵件


1.首先添加 MessageUI.framework 框架

2. 引入框架

  在類的頭部

  #import <MessageUI/MessageUI.h>

  #import <MessageUI/MFMailComposeViewController.h>

3. 實現接口 

  <MFMailComposeViewControllerDelegate>

4.  當點擊一個button 跳轉到發郵件的頁面 調用我們發郵件 

發郵件是有兩種方式 :

1. 當你的設備支持的時候 the current device is configured for sending emails

    我們使用一下的tool methods 中的displayComposerSheet 方法來發送郵件(其中使用了apple 集成好的 郵件picker --         MFMailComposeViewController) 在這里 我們將這個picker 看做是一個  模式視圖 ModalViewController 推出了 

2. 當設備不支持的時候 我們采用  

launchMailAppOnDevice 方法發送 ( 采用打開一個url地址的 方式來發)  ok..

-----點擊按鈕出發的方法

- (IBAction)contactBtnPressed:(id)sender {

    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

{

[selflaunchMailAppOnDevice];

}

}

else

{

[selflaunchMailAppOnDevice];

}

}

----- tool Methods 工具方法

 

// 1.  Launches the Mail application on the device.

-(void)launchMailAppOnDevice

{

NSString *recipients = @"mailto:maxwellsoftware@gmail.com&subject=Pocket Truth or Dare Support";

NSString *body = @"&body=email body!";

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];

email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:email]];

}

 

// 2. Displays an email composition interface inside the application. Populates all the Mail fields. 

-(void)displayComposerSheet 

{

    MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc] init];/*MFMailComposeViewController郵件發送選擇器*/

picker.mailComposeDelegate = self;

    [picker setSubject:@"Pocket Truth or Dare Support"];/*emailpicker標題題行*/

    

    // Custom NavgationBar background And set the backgroup picture

    picker.navigationBar.tintColor = [UIColorcolorWithRed:209.0/255green:183.0/255blue:126.0/255alpha:1.0];

 

    //    picker.navigationBar.tintColor = [UIColor colorWithRed:178.0/255 green:173.0/255 blue:170.0/255 alpha:1.0];

    

    if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 5.0) {

        [picker.navigationBarsetBackgroundImage:[UIImageimageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];

    }

    

// Set up recipients

NSArray *toRecipients = [NSArrayarrayWithObject:@"maxwellsoftware@gmail.com"]; 

 

[picker setToRecipients:toRecipients];

 

// Fill out the email body text

    struct utsname device_info;

 

    uname(&device_info);

    NSString *emailBody = [NSString 

            stringWithFormat:@"Model: %s\nVersion: %@\nApp: %@\nFeedback here:\n",device_info.machine,

            [[UIDevicecurrentDevice] systemVersion],/*設備系統環境*/

            [[[NSBundlemainBundle] infoDictionary]

             objectForKey:@"CFBundleShortVersionString"]];/**/

    

    NSLog(@"ios 應用發布后 .app 應用文件路徑::%@",[NSBundle mainBundle] );

    NSLog(@"ios 應用發布后 .app 應用文件 ::%@",[[NSBundle mainBundle] infoDictionary]);

    

[picker setMessageBody:emailBody isHTML:NO];

   

[selfpresentModalViewController:picker animated:YES];

    [picker release];

}

 

// 3. 一個備用的方法

//- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg   

//{  

//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_   

//                                                    message:msg   

//                                                   delegate:nil   

//                                          cancelButtonTitle:@"Sure"   

//                                          otherButtonTitles:nil];  

//    [alert show];  

//    [alert 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 

{

//    NSString *title = @"Mail";

//    NSString *msg;

//    switch (result)  

//    {  

//        case MFMailComposeResultCancelled:  

//            msg = @"Mail canceled";//@"郵件發送取消";  

//            break;  

//        case MFMailComposeResultSaved:  

//            msg = @"Mail saved";//@"郵件保存成功";  

//            [self alertWithTitle:title msg:msg];  

//            break;  

//        case MFMailComposeResultSent:  

//            msg = @"Mail sent";//@"郵件發送成功";  

//            [self alertWithTitle:title msg:msg];  

//            break;  

//        case MFMailComposeResultFailed:  

//            msg = @"Mail failed";//@"郵件發送失敗";  

//            [self alertWithTitle:title msg:msg];  

//            break;  

//        default: 

//            msg = @"Mail not sent";

//            [self alertWithTitle:title msg:msg];

//            break;  

//    }  

[self  dismissModalViewControllerAnimated:YES];

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM