使用arc不允许隐式转换'unsigned long'UIUserNotificationSettings*'

use*_*584 6 cocoa-touch objective-c ios xcode6 ios8

iOS 8中的推送通知不起作用.

错误显示:

implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arc
Run Code Online (Sandbox Code Playgroud)

码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [application registerUserNotificationSettings:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)];
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 我正在使用ios 8.0和xcode 6 beta.

iPa*_*tel 7

我是从iOS 8的官方文档中获取的.

  • 使用本地或推送通知的应用必须使用UIUserNotificationSettings对象显式注册它们向用户显示的警报类型.此注册过程与注册远程通知的过程是分开的,用户必须授予通过请求的选项传递通知的权限.
  • 本地和推送通知可以包括自定义操作作为警报的一部分.自定义操作在警报中显示为按钮.点按后,系统会通知您的应用并要求您执行相应的操作.通过与核心位置区域的交互也可以触发本地通知.

还读了

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIUserNotificationSettings_class/index.html#//apple_ref/occ/cl/UIUserNotificationSettings

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/registerUserNotificationSettings:

所以答案应该是......

/// First register notification setting with settings type like 
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.
Run Code Online (Sandbox Code Playgroud)


ale*_*rez 5

- (void)registerForRemoteNotificationTypes:(NSUInteger)notificationTypes categories:(NSSet *)categories
{
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:notificationTypes categories:categories]];
    }
    else if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotificationTypes:)])
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
    }
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)])
    {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试使用UIUserNotificationSettings-Extension,提供帮助方法,使您更容易处理新的#iOS8 #Interactive #Notifications.

  • 请注意,Apple建议仅​​在registerUserNotificationSettings:已完成后调用registerForRemoteNotifications(即已调用application:didRegisterUserNotificationSettings:). (2认同)