Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get launch options without overriding didFinishLaunchingWithOptions:

I'm embedded in an environment (Adobe AIR) where I cannot override didFinishLaunchingWithOptions. Is there any other way to get those options? Are they stored in some global variable somewhere? Or does anyone know how to get those options in AIR?

I need this for Apple Push Notification Service (APNS).

like image 763
Chon Derry Avatar asked Dec 15 '11 19:12

Chon Derry


1 Answers

Following the path in the link Michiel left ( http://www.tinytimgames.com/2011/09/01/unity-plugins-and-uiapplicationdidfinishlaunchingnotifcation/ ), you can create a class who's init method adds an observer to the UIApplicationDidFinishLaunchingNotification key. When the observer method is executed, the launchOptions will be contained in the notification's userInfo. I was doing this with local notifications so this was the implementation of my class:

static BOOL _launchedWithNotification = NO;
static UILocalNotification *_localNotification = nil;

@implementation NotificationChecker

+ (void)load
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createNotificationChecker:)
               name:@"UIApplicationDidFinishLaunchingNotification" object:nil];

}

+ (void)createNotificationChecker:(NSNotification *)notification
{
    NSDictionary *launchOptions = [notification userInfo] ;

    // This code will be called immediately after application:didFinishLaunchingWithOptions:.    
    UILocalNotification *localNotification = [launchOptions objectForKey: @"UIApplicationLaunchOptionsLocalNotificationKey"];
    if (localNotification) 
    {
        _launchedWithNotification = YES;
        _localNotification = localNotification;
    }
    else 
    {
        _launchedWithNotification = NO;
    }
}

+(BOOL) applicationWasLaunchedWithNotification
{
    return _launchedWithNotification;
}

+(UILocalNotification*) getLocalNotification
{
    return _localNotification;
}

@end

Then when my extension context is initialized I check the NotificationChecker class to see if the application was launched with a notification.

BOOL appLaunchedWithNotification = [NotificationChecker applicationWasLaunchedWithNotification];
if(appLaunchedWithNotification)
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    UILocalNotification *notification = [NotificationChecker getLocalNotification];
    NSString *type = [notification.userInfo objectForKey:@"type"];

    FREDispatchStatusEventAsync(context, (uint8_t*)[@"notificationSelected" UTF8String], (uint8_t*)[type UTF8String]);
}

Hope that helps someone!

like image 107
Colby Avatar answered Oct 22 '22 03:10

Colby