2014-07-25 56 views
2

我試圖使用MessageUI框架以編程方式發送郵件。這是我在MyScene.m中的。當我調用該方法時,將打開一個郵件窗口。但是,當我取消郵件或發送郵件時,它不會返回到MyScene。在Sprite Kit中以編程方式發送郵件

#import <MessageUI/MessageUI.h> 
    -(void)sendMail 
{ 
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
    mc.mailComposeDelegate = self; 
    [mc setSubject:@"Subject"]; 
    [mc setMessageBody:@"Hello!" isHTML:NO]; 
    [mc setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 

    // Present mail view controller on screen 
    [self.view.window.rootViewController 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.view.window.rootViewController dismissViewControllerAnimated:YES completion:NULL]; 
} 

回答

2

要設置場景的委託MFMailComposeViewController

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
mc.mailComposeDelegate = self; 

將其更改爲

mc.mailComposeDelegate = self.view.window.rootViewController; 

但是,這當然是一個不好的做法。 SKScene是一個表示層。您應該從UIViewController提供MFMailComposeViewController

編輯

將此代碼添加到GameSceneViewController

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendMail) name:@"showMailComposer" object:nil]; 
} 

-(void)sendMail 
{ 
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
    mc.mailComposeDelegate = self; 
    [mc setSubject:@"Subject"]; 
    [mc setMessageBody:@"Hello!" isHTML:NO]; 
    [mc setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 

    // Present mail view controller on screen 
    [self.view.window.rootViewController 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]; 
} 

調用此當你想發送郵件:

[[NSNotificationCenter defaultCenter] postNotificationName:@"showMailComposer" object:nil]; 
+0

這也不起作用。我怎樣才能從UIViewController呈現它? –

+0

是 - (void)mailComposeController:(MFMailComposeViewController *)控制器didFinishWithResult:(MFMailComposeResult)結果錯誤:(NSError *)錯誤被觸發? –

+0

不,事實並非如此。我 –

相關問題