Current section

Files

Jump to
mob_new priv templates mob.new ios AppDelegate.m.eex
Raw

priv/templates/mob.new/ios/AppDelegate.m.eex

// AppDelegate.m — Boots the Mob BEAM on a background thread and presents
// a SwiftUI root view via UIHostingController (created by MobUIFactory).
#import <UIKit/UIKit.h>
#import <pthread.h>
#include "mob_beam.h"
#import "MobApp-Swift.h" // generated by swiftc; exposes MobUIFactory
// Declared in mob_nif — delivers {:push_token, :ios, token} to the registered screen process.
extern void mob_send_push_token(const char* hex_token);
static void* beam_thread(void* arg) {
mob_start_beam((const char*)arg);
return NULL;
}
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication*)app
didFinishLaunchingWithOptions:(NSDictionary*)opts {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIViewController* vc = [MobUIFactory makeRootViewController];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
mob_init_ui();
extern const char* mob_app_module(void);
pthread_t t;
pthread_create(&t, NULL, beam_thread, (void*)mob_app_module());
pthread_detach(t);
return YES;
}
- (void)application:(UIApplication*)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
// Convert token bytes to hex string and deliver to Elixir via NIF.
NSMutableString* hex = [NSMutableString stringWithCapacity:deviceToken.length * 2];
const unsigned char* bytes = (const unsigned char*)deviceToken.bytes;
for (NSUInteger i = 0; i < deviceToken.length; i++) {
[hex appendFormat:@"%02x", bytes[i]];
}
mob_send_push_token([hex UTF8String]);
}
- (void)application:(UIApplication*)app
didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(@"[Mob] Failed to register for remote notifications: %@", error);
}
@end
int main(int argc, char* argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"AppDelegate");
}
}