如何在应用程序处于后台模式时仍然运行功能?

时间:2016-12-01 09:06:02

标签: ios swift

当应用程序处于后台模式时,它如何仍然运行函数来获取api结果值。 当结果得到正确的值,然后更新图标徽章。

这是可行的吗? 怎么办?

谢谢你的回答。

3 个答案:

答案 0 :(得分:0)

根据需要设置UIApplication的{​​{3}}。

答案 1 :(得分:0)

试试这个 Swift 3

UIApplication.shared.applicationIconBadgeNumber = 1

答案 2 :(得分:0)

如果你已经尝试了" macmoonshine"和#34; Rob"的答案,但你仍然没有获得徽章图标。

试试这个。希望能帮助到你。

<强>目标C

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

[UIApplication sharedApplication].applicationIconBadgeNumber = 1;

** Swift 3 **

let badgeCount: Int = 10
let application = UIApplication.sharedApplication()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Sound], categories: nil))        
application.applicationIconBadgeNumber = badgeCount

Swift 3和UserNotifications框架

  

导入UserNotifications

let badgeCount: Int = 10
let application = UIApplication.shared
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
    // Enable or disable features based on authorization.
}
application.registerForRemoteNotifications()
application.applicationIconBadgeNumber = badgeCount