最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]iOS Swift中设置程序图标的数字无效

iOS crifan 4510浏览 0评论

之前用:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //reset badge number

        //application.applicationIconBadgeNumber = 0

       

        //for debug

        application.applicationIconBadgeNumber = 20

结果无效:

会去看app图标,还是没有红色的20数字显示:

换成:

UIApplication.sharedApplication().applicationIconBadgeNumber = 20

也没用。

但是调试期间,注意到,上次到调试时用:

application.applicationIconBadgeNumber = 20

设置的20,第二次启动app时数值有效果的:

2016-04-26 10:09:17.017 [Debug] [main] [AppDelegate.swift:54] application(_:didFinishLaunchingWithOptions:) > didFinishLaunchingWithOptions launchOptions=nil

(lldb) po application.applicationIconBadgeNumber

20

(lldb) po UIApplication.sharedApplication().applicationIconBadgeNumber

20

(lldb) po application.applicationIconBadgeNumber

20

(lldb) po UIApplication.sharedApplication().applicationIconBadgeNumber

20

ios applicationIconBadgeNumber not work

ios – Push notification applicationIconBadgeNumber is not update in ios7 – Stack Overflow

ios8 – How to correctly set application badge value in iOS 8? – Stack Overflow

->好像是想要支持本地通知?才可以设置程序app图标数字?

ios – ApplicationIconBadgeNumber is not getting reset on reinstallation of my ipad app. – Stack Overflow

objective c – How do you set applicationIconBadgeNumber with scheduleLocalNotification? – Stack Overflow

模拟器上面有问题,真机就生效了?

applicationIconBadgeNumber is not updated | Apple Support Communities

applicationIconBadgeNumber not work

UILocalNotification Class Reference

-》applicationIconBadgeNumber – UIApplication Class Reference

applicationiconbadgenumber not working

How to reset/clear the iOS App’s badge? – Unity Answers

applicationiconbadgenumber 不显示

applicationIconBadgeNumber不显示 – fangdong65的专栏 – 博客频道 – CSDN.NET

-》好像都表示:

必须注册本地通知(或远程通知)中的允许更新badage,然后才能通过applicationIconBadgeNumber去更新app图标数字?

但是官网的api中没有这个提示啊。。。

ios推送消息的applicationIconBadgeNumber问题,在线等-敏捷大拇指-全球最大的Swift开发者社区 苹果粉丝家园 智能移动门户

有延时???

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;有值但是图标不显示 | iOS开发 – CocoaChina CocoaChina_让移动开发更简单

我是也是:applicationIconBadgeNumber有值,但是app图标不显示

后来去试了试,果然是:

必须先注册,此处的,本地通知,允许设置对应的badge值,然后才起效果

对应的,第一次运行app时,需要提示用户要求通知权限:

之后,即可通过代码:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //enable notification

        let notifSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil)

        application.registerUserNotificationSettings(notifSettings)

        //fixbug to make iPhone local notification work

        application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil)

        //for debug

        application.applicationIconBadgeNumber = 20

去设置app图标数字为20了:

[总结]

之前,虽然已经注册了远程通知,但是没有注册本地通知,所以设置app图标徽章数字badge值:

application.applicationIconBadgeNumber = 0

application.applicationIconBadgeNumber = 20

不起效果。

后来去注册了本地通知,其中支持badge,然后上述代码就起效果了。

代码为:

        //enable notification -> othewise later set badge number will not work

        let notifSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil)

        application.registerUserNotificationSettings(notifSettings)

        //fixbug to make iPhone local notification work

        application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil)

        UIApplication.sharedApplication().applicationIconBadgeNumber = 0

转载请注明:在路上 » [已解决]iOS Swift中设置程序图标的数字无效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.175 seconds, using 22.10MB memory