From 8118e4a4a066ffba82bda1f668c1e7a528b6b717 Mon Sep 17 00:00:00 2001 From: irov Date: Sun, 3 Sep 2023 19:05:24 +0300 Subject: [PATCH] add ios mail compose improve ios initialize application add ios detail iOSPathForTemporaryFileWithPrefix --- src/Environment/iOS/iOSDetail.h | 1 + src/Environment/iOS/iOSDetail.mm | 25 +++++++++- src/Environment/iOS/iOSEnvironmentService.h | 5 +- src/Environment/iOS/iOSEnvironmentService.mm | 44 ++++++++--------- src/Environment/iOS/iOSMailCompose.h | 9 ++-- src/Environment/iOS/iOSMailCompose.mm | 29 +++++++----- src/SDLApplication/SDLUIApplicationDelegate.h | 3 ++ .../SDLUIApplicationDelegate.mm | 47 +++++++++++-------- 8 files changed, 103 insertions(+), 60 deletions(-) diff --git a/src/Environment/iOS/iOSDetail.h b/src/Environment/iOS/iOSDetail.h index 79d14ef077..eb3002b2d7 100644 --- a/src/Environment/iOS/iOSDetail.h +++ b/src/Environment/iOS/iOSDetail.h @@ -13,5 +13,6 @@ namespace Mengine NSUUID * iOSGetAdIdentifier(); id iOSGetUIProxyApplicationDelegate( Class delegateClass ); void iOSPluginApplicationDelegateEventNotify( NSString * name, id firstArg, ... ); + NSString * iOSPathForTemporaryFileWithPrefix( NSString * prefix, NSString * ext ); } } diff --git a/src/Environment/iOS/iOSDetail.mm b/src/Environment/iOS/iOSDetail.mm index 0a51046295..88cbfbdb51 100644 --- a/src/Environment/iOS/iOSDetail.mm +++ b/src/Environment/iOS/iOSDetail.mm @@ -12,7 +12,9 @@ ////////////////////////////////////////////////////////////////////////// UIViewController * iOSGetRootViewController() { - UIWindow * window = [UIApplication sharedApplication].delegate.window; + UIApplication * application = [UIApplication sharedApplication]; + id delegate = application.delegate; + UIWindow * window = delegate.window; UIViewController * viewController = window.rootViewController; @@ -59,5 +61,26 @@ void iOSPluginApplicationDelegateEventNotify( NSString * name, id firstArg, ... [delegate notify:name arrayArgs:send_args]; } ////////////////////////////////////////////////////////////////////////// + NSString * iOSPathForTemporaryFileWithPrefix( NSString * prefix, NSString * ext ) + { + NSString * result; + CFUUIDRef uuid; + CFStringRef uuidStr; + + uuid = CFUUIDCreate(NULL); + assert(uuid != NULL); + + uuidStr = CFUUIDCreateString(NULL, uuid); + assert(uuidStr != NULL); + + result = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.%@", prefix, uuidStr, ext]]; + assert(result != nil); + + CFRelease(uuidStr); + CFRelease(uuid); + + return result; + } + ////////////////////////////////////////////////////////////////////////// } } diff --git a/src/Environment/iOS/iOSEnvironmentService.h b/src/Environment/iOS/iOSEnvironmentService.h index af994f49ce..dc7eb11e3c 100644 --- a/src/Environment/iOS/iOSEnvironmentService.h +++ b/src/Environment/iOS/iOSEnvironmentService.h @@ -4,6 +4,8 @@ #include "Kernel/ServiceBase.h" +#import "Environment/iOS/iOSMailCompose.h" + namespace Mengine { class iOSEnvironmentService @@ -20,7 +22,8 @@ namespace Mengine public: bool openUrlInDefaultBrowser( const Char * _url ) override; bool openMail( const Char * _email, const Char * _subject, const Char * _body ) override; - + protected: + iOSMailCompose * m_mailCompose; }; } diff --git a/src/Environment/iOS/iOSEnvironmentService.mm b/src/Environment/iOS/iOSEnvironmentService.mm index b7ec7cc71b..37e6d6aa49 100644 --- a/src/Environment/iOS/iOSEnvironmentService.mm +++ b/src/Environment/iOS/iOSEnvironmentService.mm @@ -1,31 +1,13 @@ #include "iOSEnvironmentService.h" #include "Interface/PlatformServiceInterface.h" -#include "Interface/ApplicationInterface.h" -#include "Interface/AccountServiceInterface.h" -#include "Interface/ConfigServiceInterface.h" -#include "Interface/OptionsServiceInterface.h" -#include "Interface/LoggerServiceInterface.h" -#include "Interface/StringizeServiceInterface.h" -#include "Interface/FileServiceInterface.h" -#include "Environment/Android/AndroidEnv.h" -#include "Environment/Android/AndroidHelper.h" - -#include "AndroidProxyLogger.h" - -#include "Kernel/AssertionObservable.h" -#include "Kernel/FactorableUnique.h" -#include "Kernel/BuildMode.h" #include "Kernel/Logger.h" -#include "Kernel/Error.h" -#include "Kernel/NotificationHelper.h" -#include "Kernel/ProxyLogger.h" -#include "Kernel/DocumentHelper.h" -#include "Kernel/LoggerHelper.h" -#include "Config/StdString.h" -#include "Config/StdIntTypes.h" +#import "Environment/iOS/iOSDetail.h" + +#import +#import ////////////////////////////////////////////////////////////////////////// SERVICE_FACTORY( iOSEnvironmentService, Mengine::iOSEnvironmentService ); @@ -34,6 +16,7 @@ { ////////////////////////////////////////////////////////////////////////// iOSEnvironmentService::iOSEnvironmentService() + : m_mailCompose(nil) { } ////////////////////////////////////////////////////////////////////////// @@ -57,7 +40,20 @@ ////////////////////////////////////////////////////////////////////////// bool iOSEnvironmentService::openMail( const Char * _email, const Char * _subject, const Char * _body ) { - return false; + if( [iOSMailCompose canSendMail] == NO ) + { + return false; + } + + UIViewController * view = Helper::iOSGetRootViewController(); + + iOSMailCompose * mailCompose = [[iOSMailCompose alloc] initWithViewController:view email:@(_email) subject:@(_subject) message:@(_body) completion:^{ + this->m_mailCompose = nullptr; + }]; + + m_mailCompose = mailCompose; + + return true; } ////////////////////////////////////////////////////////////////////////// -} \ No newline at end of file +} diff --git a/src/Environment/iOS/iOSMailCompose.h b/src/Environment/iOS/iOSMailCompose.h index a009fe20d7..54997471ab 100644 --- a/src/Environment/iOS/iOSMailCompose.h +++ b/src/Environment/iOS/iOSMailCompose.h @@ -1,10 +1,13 @@ #import #import +typedef void (^FBlockMailComposeCompletion)(void); + @interface iOSMailCompose : NSObject + (BOOL)canSendMail; -- (instancetype)initWithViewController:(UIViewController *)viewController; +- (instancetype)initWithViewController:(UIViewController * _Nonnull)viewController email:(NSString * _Nonnull)email subject:(NSString * _Nonnull)subject message:(NSString * _Nonnull)message completion:(FBlockMailComposeCompletion)completion; -@property( nonatomic, strong ) MARewardedAd * _Nullable m_mailCompose; -@end \ No newline at end of file +@property(nonatomic, strong) MFMailComposeViewController * _Nullable m_mailCompose; +@property(nonatomic, strong) FBlockMailComposeCompletion _Nullable m_completion; +@end diff --git a/src/Environment/iOS/iOSMailCompose.mm b/src/Environment/iOS/iOSMailCompose.mm index 3164c5ca25..214d1f1057 100644 --- a/src/Environment/iOS/iOSMailCompose.mm +++ b/src/Environment/iOS/iOSMailCompose.mm @@ -10,22 +10,25 @@ + (BOOL)canSendMail { return result; } -- (instancetype)initWithViewController:(UIViewController *)viewController { +- (instancetype)initWithViewController:(UIViewController * _Nonnull)viewController email:(NSString * _Nonnull)email subject:(NSString * _Nonnull)subject message:(NSString * _Nonnull)message completion:(FBlockMailComposeCompletion)completion { self = [super init]; MFMailComposeViewController * mailCompose = [[MFMailComposeViewController alloc] init]; - mailCompose.mailComposeDelegate = self; + [mailCompose setModalPresentationStyle:UIModalPresentationFormSheet]; + + [mailCompose setMailComposeDelegate:self]; - [mailCompose setSubject:@(_subject)]; - [mailCompose setMessageBody:@(_body) isHTML:NO]; - [mailCompose setToRecipients:[NSArray arrayWithObjects:@(_email), nil]]; + [mailCompose setToRecipients:[NSArray arrayWithObjects:email, nil]]; + [mailCompose setSubject:subject]; + [mailCompose setMessageBody:message isHTML:NO]; - // displaying our modal view controller on the screen (of course animated has to be set on YES if you want to see any transition) - [viewController presentViewController:mailCompose animated:YES completion:nil]; + [viewController presentViewController:mailCompose animated:YES completion:^{ + }]; self.m_mailCompose = mailCompose; - + self.m_completion = completion; + return self; } @@ -35,10 +38,10 @@ - (void)mailComposeController:(MFMailComposeViewController *)controller didFinis LOGGER_MESSAGE( "MFMailComposeResultCancelled: The user cancelled the operation. No email message was queued" ); break; case MFMailComposeResultSaved: - LOGGER_MESSAGE( "MFMailComposeResultSaved: The email message was saved in the user’s Drafts folder" ); + LOGGER_MESSAGE( "MFMailComposeResultSaved: The email message was saved in the useris Drafts folder" ); break; case MFMailComposeResultSent: - LOGGER_MESSAGE( "MFMailComposeResultSent: The email message was queued in the user’s outbox. It is ready to send the next time the user connects to email" ); + LOGGER_MESSAGE( "MFMailComposeResultSent: The email message was queued in the useris outbox. It is ready to send the next time the user connects to email" ); break; case MFMailComposeResultFailed: LOGGER_MESSAGE( "MFMailComposeResultFailed: The email message was not saved or queued, possibly due to an error [%s]" @@ -49,7 +52,9 @@ - (void)mailComposeController:(MFMailComposeViewController *)controller didFinis break; } - [self dismissViewControllerAnimated:YES completion:nil]; + [self.m_mailCompose dismissViewControllerAnimated:YES completion:^{ + self.m_completion(); + }]; } -@end \ No newline at end of file +@end diff --git a/src/SDLApplication/SDLUIApplicationDelegate.h b/src/SDLApplication/SDLUIApplicationDelegate.h index ee40e5e630..cececd8d99 100644 --- a/src/SDLApplication/SDLUIApplicationDelegate.h +++ b/src/SDLApplication/SDLUIApplicationDelegate.h @@ -2,8 +2,11 @@ #import "Environment/iOS/UIMainApplicationDelegateInterface.h" +#include "SDLApplication.h" + @interface SDLUIApplicationDelegate : NSObject @property (nonatomic, strong) NSMutableArray * m_pluginDelegates; +@property (nonatomic) Mengine::SDLApplication m_application; @end diff --git a/src/SDLApplication/SDLUIApplicationDelegate.mm b/src/SDLApplication/SDLUIApplicationDelegate.mm index 068ec5b332..25ac5ec916 100644 --- a/src/SDLApplication/SDLUIApplicationDelegate.mm +++ b/src/SDLApplication/SDLUIApplicationDelegate.mm @@ -7,14 +7,12 @@ #include "Environment/SDL/SDLIncluder.h" -#include "SDLApplication.h" - @implementation SDLUIApplicationDelegate - (id)init { NSArray * proxysClassed = getMengineAppleApplicationDelegates(); - self.m_pluginDelegates = [NSMutableArray array]; + self.m_pluginDelegates = [NSMutableArray array]; for (id className in proxysClassed) { id c = NSClassFromString(className); @@ -74,8 +72,18 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( return NO; } } + + SDL_SetMainReady(); + + SDL_iPhoneSetEventPump(SDL_TRUE); + + bool initialize = self.m_application.initialize( 0, nullptr ); + + if( initialize == false ) { + return NO; + } - [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0]; + [self performSelector:@selector(postFinishLaunch) withObject:self afterDelay:0.0]; return YES; } @@ -152,7 +160,20 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(N } - (UIWindow *)window { - return nil; + if( SERVICE_PROVIDER_EXIST() == false ) { + return nil; + } + + if( SERVICE_IS_INITIALIZE(Mengine::PlatformServiceInterface) == false ) { + return nil; + } + + Mengine::SDLPlatformServiceExtensionInterface * sdlPlatform = PLATFORM_SERVICE() + ->getDynamicUnknown(); + + UIWindow * window = sdlPlatform->getUIWindow(); + + return window; } - (void)setWindow:(UIWindow *)window { @@ -160,21 +181,9 @@ - (void)setWindow:(UIWindow *)window { } - (void)postFinishLaunch { - SDL_SetMainReady(); - - SDL_iPhoneSetEventPump(SDL_TRUE); - - Mengine::SDLApplication application; - - bool initialize = application.initialize( 0, nullptr ); - - if( initialize == true ) { - application.loop(); - } else { - SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, "Mengine initialize", "Mengine invalid initialization", NULL ); - } + self.m_application.loop(); - application.finalize(); + self.m_application.finalize(); SDL_iPhoneSetEventPump(SDL_FALSE); }