Skip to content

Commit 8ff2d3b

Browse files
committed
Added branching for alert and confirm dialogs.
1 parent 38ac623 commit 8ff2d3b

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

Plugins/iOS/IOSBridge.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ public static class Alert
5959
{
6060

6161
[DllImport("__Internal")]
62-
private static extern void IOSUIAlertController(string title, string message);
62+
private static extern void IOSUIAlertController(string title, string message, string okGameObjectName, string cancelGameObjectName);
6363

6464
public static void IOSUIAlertController(string title, string message, Action okCallback, Action cancelCallback)
6565
{
6666

67-
var okGameObject = new GameObject("IOSBridgeEvents - UIAlertController - OK");
67+
var okGameObjectName = "IOSBridgeEvents - UIAlertController - OK";
68+
var okGameObject = new GameObject(okGameObjectName);
6869
var okBridgeEvents = okGameObject.AddComponent<IOSBridgeEvents>();
6970
okBridgeEvents.action += okCallback;
7071

71-
var cancelGameObject = new GameObject("IOSBridgeEvents - UIAlertController - Cancel");
72+
var cancelGameObjectName = "IOSBridgeEvents - UIAlertController - Cancel";
73+
var cancelGameObject = new GameObject(cancelGameObjectName);
7274
var cancelBridgeEvents = cancelGameObject.AddComponent<IOSBridgeEvents>();
7375
cancelBridgeEvents.action += cancelCallback;
7476

@@ -86,14 +88,25 @@ public static void IOSUIAlertController(string title, string message, Action okC
8688

8789
};
8890

89-
IOSUIAlertController(title, message);
91+
IOSUIAlertController(title, message, okGameObjectName, cancelGameObjectName);
9092

9193
}
9294

9395
public static void IOSUIAlertController(string title, string message, Action okCallback)
9496
{
9597

96-
IOSUIAlertController(title, message, okCallback, () => {});
98+
var okGameObjectName = "IOSBridgeEvents - UIAlertController - OK";
99+
var okGameObject = new GameObject(okGameObjectName);
100+
var okBridgeEvents = okGameObject.AddComponent<IOSBridgeEvents>();
101+
okBridgeEvents.action += okCallback;
102+
103+
okBridgeEvents.action += () => {
104+
105+
UnityEngine.Object.Destroy(okGameObject);
106+
107+
};
108+
109+
IOSUIAlertController(title, message, okGameObjectName, "");
97110

98111
}
99112

Plugins/iOS/IOSBridge.mm

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,38 @@ bool IOSUIAccessibilityIsSpeakSelectionEnabled() {
125125
return UIAccessibilityIsSpeakSelectionEnabled();
126126
}
127127

128-
void IOSUIAlertController(const char* title, const char* message) {
128+
void IOSUIAlertController(const char* title, const char* message, const char* okGameObjectName, const char* cancelGameObjectName) {
129129

130130
NSString* titleString = [NSString stringWithUTF8String: title];
131131
NSString* messageString = [NSString stringWithUTF8String: message];
132132

133+
NSString* okGameObjectNameString = [NSString stringWithUTF8String: okGameObjectName];
134+
NSString* cancelGameObjectNameString = [NSString stringWithUTF8String: cancelGameObjectName];
135+
133136
UIAlertController* alert = [UIAlertController alertControllerWithTitle:titleString
134-
message:messageString
135-
preferredStyle:UIAlertControllerStyleAlert];
137+
message:messageString
138+
preferredStyle:UIAlertControllerStyleAlert];
136139

137140
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
138-
handler:^(UIAlertAction * action) {
141+
handler:^(UIAlertAction * action) {
139142

140-
UnitySendMessage("IOSBridgeEvents - UIAlertController - OK", "CallbackOneShot", "");
143+
UnitySendMessage([okGameObjectNameString UTF8String], "CallbackOneShot", "");
141144

142-
}];
145+
}];
143146
[alert addAction:defaultAction];
144-
[alert setPreferredAction:defaultAction];
145147

146-
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
147-
handler:^(UIAlertAction * action) {
148+
if ([cancelGameObjectNameString length] > 0) {
149+
150+
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
151+
handler:^(UIAlertAction * action) {
148152

149-
UnitySendMessage("IOSBridgeEvents - UIAlertController - Cancel", "CallbackOneShot", "");
153+
UnitySendMessage([cancelGameObjectNameString UTF8String], "CallbackOneShot", "");
150154

151-
}];
152-
[alert addAction:cancelAction];
155+
}];
156+
[alert addAction:cancelAction];
157+
[alert setPreferredAction:defaultAction];
158+
159+
}
153160

154161
UIViewController* view = [[[UIApplication sharedApplication] keyWindow] rootViewController];
155162

0 commit comments

Comments
 (0)