/
EPPZ_Alert_iOS_ObjC.m
106 lines (79 loc) · 3.62 KB
/
EPPZ_Alert_iOS_ObjC.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Copyright (c) 2016 eppz! mobile, Gergely Borbás (SP)
//
// http://www.twitter.com/_eppz
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "EPPZ_Alert_iOS_ObjC.h"
NSMutableDictionary* _instancesForGameObjectNames = nil;
const char* callbackMethodName = "AlertDidFinishWithResult";
@interface EPPZ_Alert_iOS ()
@property (nonatomic, strong) NSString *gameObjectName;
@property (nonatomic, weak) UIViewController *presentingViewController;
@end
@implementation EPPZ_Alert_iOS
#pragma mark - Instance management (instance pool keyed by `GameObject` names)
+(NSMutableDictionary*)instancesForGameObjectNames
{
if (_instancesForGameObjectNames == nil)
{ _instancesForGameObjectNames = [NSMutableDictionary new]; }
return _instancesForGameObjectNames;
}
+(EPPZ_Alert_iOS*)instanceForGameObjectName:(const char*) gameObjectName_
{
NSString *gameObjectName = NSStringFromUnityString(gameObjectName_);
// Return cached instance (if any).
if ([self.instancesForGameObjectNames.allKeys containsObject:gameObjectName])
{ return [self.instancesForGameObjectNames objectForKey:gameObjectName]; }
// Create / cache new instance (if needed).
EPPZ_Alert_iOS *instance = [self new];
instance.gameObjectName = gameObjectName; // Store reference
[self.instancesForGameObjectNames setObject:instance forKey:gameObjectName];
return instance;
}
#pragma mark - Features
-(void)showAlertWithTitle:(const char*) title_
message:(const char*) message_
buttonTitle:(const char*) buttonTitle_
cancelButtonTitle:(const char*) cancelButtonTitle_
{
// Text.
NSString *title = NSStringFromUnityString(title_);
NSString *message = NSStringFromUnityString(message_);
NSString *buttonTitle = NSStringFromUnityString(buttonTitle_);
NSString *cancelButtonTitle = NSStringFromUnityString(cancelButtonTitle_);
// Setup.
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:
[UIAlertAction actionWithTitle:buttonTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action){ [self handleAction:action]; }]];
[alert addAction:
[UIAlertAction actionWithTitle:cancelButtonTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action){ [self handleAction:action]; }]];
// Present.
[[self topMostViewController] presentViewController:alert animated:YES completion:nil];
}
-(void)handleAction:(UIAlertAction*) action
{
UnitySendMessage(UnityStringFromNSString(self.gameObjectName),
callbackMethodName,
UnityStringFromNSString(action.title));
}
-(UIViewController*)topMostViewController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) { topController = topController.presentedViewController; }
return topController;
}
@end