不覆盖didFinishLaunchingWithOptions获取启动选项

9

我处在一个环境中(Adobe AIR),无法覆盖didFinishLaunchingWithOptions。是否有其他方法可以获取这些选项?它们存储在某个全局变量中吗?或者有人知道如何在AIR中获取这些选项吗?

我需要这些选项用于Apple Push Notification Service (APNS)。


我想你应该看一下这个链接,并且如果有时间的话,我也会去看:http://www.tinytimgames.com/2011/09/01/unity-plugins-and-uiapplicationdidfinishlaunchingnotifcation/ - Michiel
我应该补充说明,为了使其工作,您需要制作一个ANE(AIR本机扩展)。http://www.adobe.com/devnet/air/native-extensions-for-air.html - Michiel
1个回答

11

按照Michiel留下的链接(http://www.tinytimgames.com/2011/09/01/unity-plugins-and-uiapplicationdidfinishlaunchingnotifcation/)中的路径,您可以创建一个类,该类的init方法向UIApplicationDidFinishLaunchingNotification键添加观察者。当执行观察者方法时,启动选项将包含在通知的userInfo中。我正在使用本地通知进行此操作,因此这是我的类的实现:

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

当我的扩展上下文被初始化时,我会检查NotificationChecker类以查看应用程序是否是通过通知启动的。
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]);
}

希望能帮到某些人!

1
我终于自己动手做了这件事,并且我确认它像魔法一样有效。 - Michiel
[notification userInfo] 对我来说是一个空字典 =[ count == 0 - grisevg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接