ConsoleUtilities.mm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #import "ConsoleUtilities.h"
  2. void UnityPause( int pause );
  3. @implementation ConsoleUtilities
  4. MFMailComposeViewController* mMailer;
  5. + (ConsoleUtilities*)instance
  6. {
  7. static ConsoleUtilities *instance = nil;
  8. if( !instance )
  9. instance = [[ConsoleUtilities alloc] init];
  10. return instance;
  11. }
  12. - (void)beginEmail:(NSString*)toAddress subject:(NSString*)subject body:(NSString*)body isHTML:(BOOL)isHTML
  13. {
  14. mMailer = [[MFMailComposeViewController alloc] init];
  15. if(mMailer == nil)
  16. return;
  17. [mMailer setSubject:subject];
  18. [mMailer setMessageBody:body isHTML:isHTML];
  19. mMailer.mailComposeDelegate = self;
  20. if( toAddress && toAddress.length && [toAddress rangeOfString:@"@"].location != NSNotFound )
  21. [mMailer setToRecipients:[NSArray arrayWithObject:toAddress]];
  22. }
  23. - (void)addAttachment:(NSData*)data mimeType:(NSString*)mimeType filename:(NSString*)filename
  24. {
  25. if(mMailer == nil)
  26. return;
  27. if( data && filename && mimeType )
  28. [mMailer addAttachmentData:data mimeType:mimeType fileName:filename];
  29. }
  30. - (void)finishEmail
  31. {
  32. if(mMailer != nil)
  33. {
  34. [self showViewControllerModallyInWrapper:mMailer];
  35. }
  36. mMailer = nil;
  37. }
  38. - (void)showViewControllerModallyInWrapper:(UIViewController*)viewController
  39. {
  40. // pause the game
  41. UnityPause( true );
  42. // cancel the previous delayed call to dismiss the view controller if it exists
  43. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  44. UIViewController *vc = UnityGetGLViewController();
  45. // show the mail composer on iPad in a form sheet
  46. if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && [viewController isKindOfClass:[MFMailComposeViewController class]] )
  47. viewController.modalPresentationStyle = UIModalPresentationFormSheet;
  48. // show the view controller
  49. [vc presentModalViewController:viewController animated:YES];
  50. }
  51. - (void)dismissWrappedController
  52. {
  53. UnityPause( false );
  54. UIViewController *vc = UnityGetGLViewController();
  55. if( !vc )
  56. return;
  57. [vc dismissModalViewControllerAnimated:YES];
  58. }
  59. - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
  60. {
  61. [self dismissWrappedController];
  62. [controller performSelector:@selector(autorelease) withObject:nil afterDelay:2.0];
  63. }
  64. @end