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

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

错误显示:

implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arc 

码:

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

在这里输入图像说明 我正在使用ios 8.0和xcode 6testing版。

我从iOS 8的官方文档中获得。

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

也读

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. 
 - (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]; } } 

尝试UIUserNotificationSettings-Extension ,提供帮助程序方法,这将使您更轻松地处理新的#iOS8 #Interactive#通知。

这里是:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { // are you running on iOS8? if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:settings]; } else // iOS 7 or earlier { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } } 

请查看运行时提供的日志。 首先,没有本地事件的用户注册,日志build议

UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, 4 June 2014 9:27:24 pm India Standard Time, user info = (null)} with an alert but haven't received permission from the user to display alerts

这是iOS 8。

所以,在这种情况下,你也需要使用

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]];

didFinishLaunchingWithOptions

这就是你需要处理iOS 8和任何小于iOS 8的东西

 if (SYSTEM_VERSION_LESS_THAN(@"8.0")) { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert]; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeNone]; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge]; } else { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; }