Skip to content

Commit

Permalink
add ios mail compose
Browse files Browse the repository at this point in the history
improve ios initialize application
add ios detail iOSPathForTemporaryFileWithPrefix
  • Loading branch information
irov committed Sep 3, 2023
1 parent b9b7144 commit 8118e4a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 60 deletions.
1 change: 1 addition & 0 deletions src/Environment/iOS/iOSDetail.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ namespace Mengine
NSUUID * iOSGetAdIdentifier();
id iOSGetUIProxyApplicationDelegate( Class delegateClass );
void iOSPluginApplicationDelegateEventNotify( NSString * name, id firstArg, ... );
NSString * iOSPathForTemporaryFileWithPrefix( NSString * prefix, NSString * ext );
}
}
25 changes: 24 additions & 1 deletion src/Environment/iOS/iOSDetail.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
//////////////////////////////////////////////////////////////////////////
UIViewController * iOSGetRootViewController()
{
UIWindow * window = [UIApplication sharedApplication].delegate.window;
UIApplication * application = [UIApplication sharedApplication];
id<UIApplicationDelegate> delegate = application.delegate;
UIWindow * window = delegate.window;

UIViewController * viewController = window.rootViewController;

Expand Down Expand Up @@ -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;
}
//////////////////////////////////////////////////////////////////////////
}
}
5 changes: 4 additions & 1 deletion src/Environment/iOS/iOSEnvironmentService.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "Kernel/ServiceBase.h"

#import "Environment/iOS/iOSMailCompose.h"

namespace Mengine
{
class iOSEnvironmentService
Expand All @@ -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;
};
}
44 changes: 20 additions & 24 deletions src/Environment/iOS/iOSEnvironmentService.mm
Original file line number Diff line number Diff line change
@@ -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 <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

//////////////////////////////////////////////////////////////////////////
SERVICE_FACTORY( iOSEnvironmentService, Mengine::iOSEnvironmentService );
Expand All @@ -34,6 +16,7 @@
{
//////////////////////////////////////////////////////////////////////////
iOSEnvironmentService::iOSEnvironmentService()
: m_mailCompose(nil)
{
}
//////////////////////////////////////////////////////////////////////////
Expand All @@ -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;
}
//////////////////////////////////////////////////////////////////////////
}
}
9 changes: 6 additions & 3 deletions src/Environment/iOS/iOSMailCompose.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#import <MessageUI/MessageUI.h>
#import <UIKit/UIKit.h>

typedef void (^FBlockMailComposeCompletion)(void);

@interface iOSMailCompose : NSObject<MFMailComposeViewControllerDelegate>
+ (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
@property(nonatomic, strong) MFMailComposeViewController * _Nullable m_mailCompose;
@property(nonatomic, strong) FBlockMailComposeCompletion _Nullable m_completion;
@end
29 changes: 17 additions & 12 deletions src/Environment/iOS/iOSMailCompose.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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]"
Expand All @@ -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
@end
3 changes: 3 additions & 0 deletions src/SDLApplication/SDLUIApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

#import "Environment/iOS/UIMainApplicationDelegateInterface.h"

#include "SDLApplication.h"

@interface SDLUIApplicationDelegate : NSObject<UIApplicationDelegate, UIMainApplicationDelegateInterface>

@property (nonatomic, strong) NSMutableArray<UIPluginApplicationDelegateInterface> * m_pluginDelegates;
@property (nonatomic) Mengine::SDLApplication m_application;

@end
47 changes: 28 additions & 19 deletions src/SDLApplication/SDLUIApplicationDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<UIPluginApplicationDelegateInterface> array];

for (id className in proxysClassed) {
id c = NSClassFromString(className);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -152,29 +160,30 @@ - (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 {
/* Do nothing. */
}

- (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);
}
Expand Down

0 comments on commit 8118e4a

Please sign in to comment.