使用Swift在iOS 8中为app图标添加徽章 [英] Add badge to app icon in iOS 8 with Swift

查看:123
本文介绍了使用Swift在iOS 8中为app图标添加徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序的图标上设置徽章,就像在Apple的邮件应用程序中一样(图标顶部的数字)。
如何在Swift(iOS8)中执行此操作?

I'd like to set a badge on the icon of my app like in apple's mail app (number on top of the icon). How can I do this in Swift (iOS8)?

推荐答案

图标顶部的数字是叫徽章。除了应用程序图标(包括导航栏工具栏图标)之外,还可以在许多项目上设置徽章。

The "number on top of the icon" is called a Badge. Badges can be set on a number of things besides Application Icons including Navigation Bar toolbar icons.

有很多方法可以更改应用程序图标徽章。大多数用例涉及在应用程序处于后台时设置此项以警告用户他们可能感兴趣的某些更改。这将涉及推送通知。

There are many ways to change the Application Icon Badge. Most use cases involve setting this when the Application is in the background to alert the user that there is some change that they may be interested in. This would involve a push notification.

有关详情,请参阅: https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

但是,您也可以在应用处于活动状态时进行更改。您需要通过注册UserNotificationType来获得用户的许可。获得许可后,您可以将其更改为您希望的任何数量。

However, you can also change it while your app is active. You will need permission from the user by registering the UserNotificationType. Once you get permission, you can change it to whatever number you wish.

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
        UIUserNotificationType.Badge, categories: nil
        ))

application.applicationIconBadgeNumber = 5

< img src =https://i.stack.imgur.com/a2PID.pngalt =在此处输入图像说明>

这篇关于使用Swift在iOS 8中为app图标添加徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆