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

Posted

技术标签:

【中文标题】arc 不允许隐式转换 \'unsigned long \'UIUserNotificationSettings *\'【英文标题】:implicit conversion of 'unsigned long 'UIUserNotificationSettings *' is disallowed with arcarc 不允许隐式转换 'unsigned long 'UIUserNotificationSettings *' 【发布时间】:2014-06-03 04:12:11 【问题描述】:

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 6 beta。

【问题讨论】:

是的image.noelshack.com/fichiers/2014/23/… 似乎 API 可能会保密,因此没有查看文档,但编译错误看起来像 registerUserNotificationSettings 需要指向设置的指针而不是设置本身。换句话说,您可能需要仔细查看文档。 这违反了 Apple 的 NDA 协议。您不能在公共论坛上发布有关 iOS 8 或 Xcode 6 的问题。在产品正式发布之前,必须在 Apple 开发者论坛上提出这些问题。 @JohnathonSullinger 是吗?今年,Apple 取消了 NDA……甚至文档都是公开的。 【参考方案1】:

我来自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.

【讨论】:

我不明白- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions // Let the device know we want to receive push notifications [[UIApplication sharedApplication] registerUserNotificationSettings: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; return YES; 所以你提到了保密协议,然后立即违反了它? ;) 不,它不在 NDA 之下。 §10.1 iOS 开发者程序许可协议:对于 Apple 在 WWDC(Apple 的全球开发者大会)上披露的有关预发布 Apple 软件和服务的技术信息,您将不受上述保密条款的约束,但您不得发布屏幕拍摄、撰写公开评论或重新分发任何预发布的 Apple 软件或服务。【参考方案2】:
- (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。

【讨论】:

请注意,Apple 建议仅在 registerUserNotificationSettings: 完成后(即 application:didRegisterUserNotificationSettings: 已被调用)调用 registerForRemoteNotifications。【参考方案3】:

这里是:

- (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】:

请查看运行时提供的日志。 起初,没有用户注册本地事件,日志确实提示

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.

【讨论】:

【参考方案5】:

这是你需要处理 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];

【讨论】:

以上是关于arc 不允许隐式转换 'unsigned long 'UIUserNotificationSettings *'的主要内容,如果未能解决你的问题,请参考以下文章

ARC 中不允许从 NSInteger 到 NSString 的隐式转换.. 应该使用啥解决方法来处理整数

ARC 不允许将非 Objective-C 指针类型“const char *”隐式转换为“id”

使用 ARC 将 NSUInteger 转换为字符串 [重复]

关于int转换成unsigned的隐式转换问题?

xcode构建失败隐式转换失去整数精度:'size_t'(又名'unsigned long')到'socklen_t'(又名'unsigned int')

隐式转换将符号性“int”更改为“unsigned int”