ios如何利用系統郵件發送反饋 代碼

來源:互聯網
上載者:User

    主要是使用MFMailComposeViewController  這個類,封裝下就ok.   

  1.  

將ios的appDelegate類  的laungh函數中,將相應的代碼修改為:

[cpp] view
plaincopy

  1. // Set RootViewController to window  
  2. if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)  
  3. {  
  4.     // warning: addSubView doesn't work on iOS6  
  5.      [window setRootViewController:viewController];  
  6.     [window addSubview: viewController.view];  
  7. }  
  8. else  
  9. {  
  10.     // use this method on ios6  
  11.     [window setRootViewController:viewController];  
  12. }  


   
主要目的是 當SDK< 6.0的時候,也設定  

    [window  setRootViewController:viewController];

   以免後面調用 window.rootViewController的時候crash.


2.   標頭檔內容如下:

   

@interface EmailIOS : UIViewController <MFMailComposeViewControllerDelegate>{    MFMailComposeViewController* mailPicker;    UIWindow * window;}- (void)SendMail;@end


3. *.mm檔案實現如下:

   

@implementation EmailIOS- (void)SendMail{    if (![MFMailComposeViewController canSendMail])    {        UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:nil  message:[NSString stringWithUTF8String:LanguageManager::sharedLanguageManager()->getLocalizedString("Please configure your Email first").c_str()]  delegate:nil  cancelButtonTitle:@"OK"  otherButtonTitles:nil];        [alert show];        [alert release];        return ;    }    if(mailPicker != nil)    {        [mailPicker release];    }        mailPicker = [[MFMailComposeViewController alloc] init];    mailPicker.mailComposeDelegate = self;    mailPicker.navigationBar.tintColor=[UIColor colorWithRed:(11.0/255.0) green:(81.0/255.0) blue:(112.0/255.0) alpha:1];    //    mailPicker.navigationBar.tintColor=[UIColor colorWithRed:(90.0/255.0) green:(182.0/255.0) blue:(45.0/255.0) alpha:1];    NSString* strSubject = [NSString stringWithFormat:@"XXX APP反饋"];    [mailPicker setSubject:strSubject];    // Set up recipients    NSArray *toRecipients = [NSArray arrayWithObject:@"XXX@fuckU.com"];    [mailPicker setToRecipients:toRecipients];        NSString * device = [[UIDevice currentDevice] model];    NSString * ios = [[UIDevice currentDevice] systemVersion];         NSString *body = [NSString stringWithFormat:@"請留下您的寶貴建議和意見:\n\n\n以下資訊有助於我們確認您的問題,建議保留。\nApp id:\nApp Version: %s\nDevice: %@\nOS Version: %@\n", GAME_VERSION, device, ios];    [mailPicker setMessageBody:body isHTML:NO];        [self presentModalViewController:mailPicker animated:YES];         mailPicker.view.frame = [UIScreen mainScreen].bounds;    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];        if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)    {        // warning: addSubView doesn't work on iOS6        [window addSubview: mailPicker.view];    }    else    {        // use this method on ios6        window.rootViewController = self;        [window setRootViewController:self];    }         [window makeKeyAndVisible];     //    cocos2d::CCDirector::sharedDirector()->////[[[CCDirector sharedDirector] view] insertSubview:mailPicker.view atIndex:30];}- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{   NSString * message;// Notifies users about errors associated with the interfaceswitch (result){case MFMailComposeResultSent:{message = @"Successfully sent!";UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email"message:message   delegate:self  cancelButtonTitle:@"OK"  otherButtonTitles:nil];[alert show];[alert release];}break;case MFMailComposeResultFailed:{//message = @"Result: failed";UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email"message:[NSString stringWithFormat:@"Error! Please try again later. (error msg = %@)", @"Failed to send email"]   delegate:self  cancelButtonTitle:@"OK"  otherButtonTitles:nil];[alert show];[alert release];}break;default:{//message = @"Result: not sent";//DLog(@"%@",message);}break;}        //add 的時候是分版本的,所以這裡也是分版本,否則crash    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)    {         [mailPicker.view removeFromSuperview];    }    else    {        [mailPicker dismissModalViewControllerAnimated:YES];    }    [mailPicker release];    mailPicker = nil;        //這個地方不需要remove  self             [window removeFromSuperview];    [window release];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    return UIInterfaceOrientationIsLandscape( interfaceOrientation );}// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead- (NSUInteger) supportedInterfaceOrientations{#ifdef __IPHONE_6_0    return UIInterfaceOrientationMaskLandscape;#endif    return 0;}- (BOOL) shouldAutorotate {    return YES;}@end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.