swift 如何在IOS应用图标上添加消息数
在应用图标右上角添加消息数提醒,可以很方便的告知用户该应用中有无新消息需要处理。下面用xcode 7.3.1来简要说明一下如何用swift语言进行此功能的实现。
1、修改 AppDelegate.swift
1 // 2 // AppDelegate.swift 3 // RainbowDemo 4 // 5 // Created by Jackwang on 16/8/17. 6 // Copyright © 2016年 Jackwang . All rights reserved. 7 // 8 9 import UIKit 10 11 @UIApplicationMain 12 class AppDelegate: UIResponder, UIApplicationDelegate { 13 14 var window: UIWindow? 15 16 17 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 // Override point for customization after application launch. 19 //使用UILocalNotification除了可以实现本地消息的推送功能(可以设置推送内容,推送时间,提示音), 20 //还可以设置应用程序右上角的提醒个数。 21 let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], 22 categories: nil) 23 application.registerUserNotificationSettings(settings) 24 25 26 return true 27 } 28 29 func applicationWillResignActive(application: UIApplication) { 30 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 31 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 32 } 33 34 func applicationDidEnterBackground(application: UIApplication) { 35 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 36 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 37 } 38 39 func applicationWillEnterForeground(application: UIApplication) { 40 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 41 } 42 43 func applicationDidBecomeActive(application: UIApplication) { 44 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 45 } 46 47 func applicationWillTerminate(application: UIApplication) { 48 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 49 } 50 51 52 }
2 修改在ViewController.swift
1 // 2 // ViewController.swift 3 // RainbowDemo 4 // 5 // Created by jackwang on 16/8/17. 6 // Copyright © 2016年 jackwang. All rights reserved. 7 // 8 9 import UIKit 10 11 class ViewController: UIViewController { 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // Do any additional setup after loading the view, typically from a nib. 16 //发送通知消息 17 scheduleNotification(); 18 } 19 20 //发送通知消息 21 func scheduleNotification(){ 22 //清除所有本地推送 23 UIApplication.sharedApplication().cancelAllLocalNotifications() 24 25 //创建UILocalNotification来进行本地消息通知 26 let localNotification = UILocalNotification() 27 //设置应用程序右上角的提醒个数 28 localNotification.applicationIconBadgeNumber = 8; 29 UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 30 } 31 32 override func didReceiveMemoryWarning() { 33 super.didReceiveMemoryWarning() 34 // Dispose of any resources that can be recreated. 35 } 36 37 38 }
3 编译运行
第一次会弹出询问是否允许推送消息,确认后,第二次运行该app后,会在图标右上角标注消息数,如下图所示:
修改APP的显示名称,可以单击info.plist,然后修改器Bundle name,如下图所示:
水平有限,望各位园友不吝赐教!如果觉得不错,请点击推荐和关注!
出处:http://www.cnblogs.com/isaboy/
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/isaboy/
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?