diff --git a/app/os_ios.go b/app/os_ios.go index 3da860719..53a22eaf5 100644 --- a/app/os_ios.go +++ b/app/os_ios.go @@ -12,6 +12,7 @@ package app #include #include +__attribute__ ((visibility ("hidden"))) int gio_applicationMain(int argc, char *argv[]); __attribute__ ((visibility ("hidden"))) void gio_viewSetHandle(CFTypeRef viewRef, uintptr_t handle); struct drawParams { @@ -81,6 +82,7 @@ import "C" import ( "image" "io" + "os" "runtime" "runtime/cgo" "runtime/debug" @@ -394,12 +396,47 @@ func newWindow(win *callbacks, options []Option) { <-mainWindow.windows } +var mainMode = mainModeUndefined + +const ( + mainModeUndefined = iota + mainModeExe + mainModeLibrary +) + func osMain() { + if !isMainThread() { + panic("app.Main must be run on the main goroutine") + } + switch mainMode { + case mainModeUndefined: + mainMode = mainModeExe + var argv []*C.char + for _, arg := range os.Args { + a := C.CString(arg) + defer C.free(unsafe.Pointer(a)) + argv = append(argv, a) + } + C.gio_applicationMain(C.int(len(argv)), unsafe.SliceData(argv)) + case mainModeExe: + panic("app.Main may be called only once") + case mainModeLibrary: + // Do nothing, we're embedded as a library. + } } //export gio_runMain func gio_runMain() { - runMain() + if !isMainThread() { + panic("app.Main must be run on the main goroutine") + } + switch mainMode { + case mainModeUndefined: + mainMode = mainModeLibrary + runMain() + case mainModeExe: + // Do nothing, main has already been called. + } } func (UIKitViewEvent) implementsViewEvent() {} diff --git a/app/os_ios.m b/app/os_ios.m index de57aaa59..bea506900 100644 --- a/app/os_ios.m +++ b/app/os_ios.m @@ -280,3 +280,23 @@ void gio_viewSetHandle(CFTypeRef viewRef, uintptr_t handle) { GioView *v = (__bridge GioView *)viewRef; v.handle = handle; } + +@interface _gioAppDelegate : UIResponder +@property (strong, nonatomic) UIWindow *window; +@end + +@implementation _gioAppDelegate +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + GioViewController *controller = [[GioViewController alloc] initWithNibName:nil bundle:nil]; + self.window.rootViewController = controller; + [self.window makeKeyAndVisible]; + return YES; +} +@end + +int gio_applicationMain(int argc, char *argv[]) { + @autoreleasepool { + return UIApplicationMain(argc, argv, nil, NSStringFromClass([_gioAppDelegate class])); + } +}