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

时间:2014-06-03 04:12:12

标签: ios objective-c cocoa-touch xcode6 ios8

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;
}

enter image description here 我正在使用ios 8.0和xcode 6 beta。

5 个答案:

答案 0 :(得分:7)

我来自below from official documentation of 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.

答案 1 :(得分: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];
    }
}

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

答案 2 :(得分:1)

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

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中的

答案 3 :(得分:1)

这是:

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

答案 4 :(得分:0)

这是你需要处理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];
}
相关问题