appdelegate中的NSURLSessionDataTask

时间:2016-04-16 17:37:18

标签: objective-c appdelegate nsurlsessiondatatask

当应用程序从邮件打开时,我会在appDelegate中调用NSURLSessionDataTask,因此在此函数中

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

我试过但不行,为什么?

我需要从这个函数中获取查询

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSURLComponents *components = [[NSURLComponents alloc] init];
components.query = [url query];

BOOL confirmRegistration = NO;
NSString *userToken;
for (NSURLQueryItem *queryItem in components.queryItems) {
    if ([queryItem.name isEqualToString:@"userToken"])
    {
        userToken = queryItem.value;
    }
    else if ([queryItem.name isEqualToString:@"registrationType"])
    {
        if ([queryItem.value isEqualToString:@"confirmRegistration"])
        {
            confirmRegistration = YES;
        }
    }
}

//la app è stata aperta con la mail di conferma registrazione
if (confirmRegistration)
{
    NSDictionary* userInfo = @{@"userToken": userToken};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"confirmRegistration" object:nil userInfo:userInfo];
}

return YES;

}

谢谢

1 个答案:

答案 0 :(得分:0)

从任何地方打开应用程序时,都会调用application:didFinishLaunchingWithOptions:

在那里你检查一下launchingOptions以查看触发申请开放的内容。

例如,如果使用推送通知打开应用程序。你做了类似下面的事情。

if let userInfo = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
    self.application(application, didReceiveRemoteNotification: userInfo)
}

您可以检查从Mail打开应用程序时获得的密钥。

<强>更新

UIApplicationLaunchOptionsSourceApplicationKeyUIApplicationLaunchOptionsURLKey这样的键,您可以打印或比较触发应用程序启动的内容,然后开始工作。

当您从Mail打开应用程序时,请打印以下内容并使用它来比较并开始您的工作。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    print(launchOptions?[UIApplicationLaunchOptionsSourceApplicationKey])
    print(launchOptions?[UIApplicationLaunchOptionsURLKey])
    return true
}