AppRuntimesNative.mm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. extern "C" UIViewController *UnityGetGLViewController(void);
  6. static void ShowNetworkDialog(const char *message, void (*callback)(int), bool allowCancel)
  7. {
  8. NSString *text = message ? [NSString stringWithUTF8String:message] : @"";
  9. dispatch_async(dispatch_get_main_queue(), ^{
  10. UIViewController *presenter = UnityGetGLViewController();
  11. while (presenter.presentedViewController != nil)
  12. presenter = presenter.presentedViewController;
  13. if (presenter.isBeingDismissed || presenter.isBeingPresented)
  14. {
  15. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 10), dispatch_get_main_queue(), ^{
  16. ShowNetworkDialog([text UTF8String], callback, allowCancel);
  17. });
  18. return;
  19. }
  20. if (presenter == nil || presenter.view.window == nil)
  21. {
  22. callback(0);
  23. return;
  24. }
  25. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Network Error"
  26. message:text preferredStyle:UIAlertControllerStyleAlert];
  27. [alert addAction:[UIAlertAction actionWithTitle:(allowCancel ? @"Retry" : @"retry") style:UIAlertActionStyleDefault
  28. handler:^(UIAlertAction *action) { callback(1); }]];
  29. if (allowCancel)
  30. [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
  31. handler:^(UIAlertAction *action) { callback(0); }]];
  32. [presenter presentViewController:alert animated:YES completion:nil];
  33. });
  34. }
  35. extern "C"
  36. {
  37. int AppRuntimes_GetEnableUnityLog()
  38. {
  39. id value = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"EnableUnityLog"];
  40. if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]])
  41. return [value boolValue] ? 1 : 0;
  42. return 0;
  43. }
  44. const char *AppRuntimes_GetTestUserID()
  45. {
  46. id value = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"TestUserID"];
  47. return strdup([value isKindOfClass:[NSString class]] ? [value UTF8String] : "");
  48. }
  49. void AppRuntimes_FreeString(const char *value)
  50. {
  51. free((void *)value);
  52. }
  53. void AppRuntimes_ShowDialog(const char *message, void (*callback)(int))
  54. {
  55. ShowNetworkDialog(message, callback, true);
  56. }
  57. void AppRuntimes_ShowRetryDialog(const char *message, void (*callback)(int))
  58. {
  59. ShowNetworkDialog(message, callback, false);
  60. }
  61. }