didFinishLaunchingWithOptions未调用

use*_*477 3 iphone push-notification xcode4

我在启用推送通知时遇到问题.

使用此代码我尝试启用通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

它不起作用,所以我在行中添加了一个断点

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Run Code Online (Sandbox Code Playgroud)

但似乎这部分代码永远不会执行.

为什么这不起作用?

Nic*_*ull 7

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

这应该在你的UIApplicationDelegate,而不是你的UIViewController.如果你有它UIViewController,它将永远不会被调用.从那里取出代码并将其放入您的代码中UIApplicationDelegate.