在应用图标右上角添加消息数提醒,可以很方便的告知用户该应用中有无新消息需要处理。下面用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,如下图所示:

转载于:https://www.cnblogs.com/isaboy/p/swift_ios_app_UILocalNotification_msg.html

swift 如何在IOS应用图标上添加消息数相关推荐

  1. 苹果计算机怎么添加在快捷方式,如何在 iPhone 主屏幕上添加文件快捷方式?

    如何在 iPhone 主屏幕上添加文件快捷方式?文件管理系统一直是 iOS 的弱项,这是由于 iOS 文件操作逻辑与 Windows 完全不同,是iOS「沙盒」机制的产物,那么如何在 iOS 现有框架 ...

  2. IOS应用图标上的数字提示实例

    作者:朱克锋 邮箱:zhukefeng@iboxpay.com 转载请注明出处:http://blog.csdn.net/linux_zkf IOS应用图标上的数字提示实例 - (void) upda ...

  3. android读信息会话,在融云 IMkit 会话界面基础上添加消息已读未读.

    在融云 IMkit 会话界面基础上添加消息已读未读. 使用过融云的同学们可能知道. 融云 IMkit 的会话界面, 发送玩消息后, 如果对方已读, 发送端则会显示小对号的图片. 但是更具需求要把小对号 ...

  4. Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒

    在Android手机上,如QQ.微信当有未读消息的时候.我们可以看到在应用的启动图标的右上角会有一个红色圈圈.且圈圈里会动态显示未读消息的数目,如下图显示: 那么该功能是怎么实现的呢? 在万能的互联网 ...

  5. 应用启动图标未读消息数显示 工具类

    /* * 应用启动图标未读消息数显示 工具类 (效果如:QQ.微信.未读短信 等应用图标) * */ public class BadgeUtil { /** * Set badge count * ...

  6. 如何在 iOS、MacOS 上使用 ChatGPT 和适用于 iPhone 的最佳 ChatGPT 应用程序

    目录 什么是聊天 GPT? 如何在我的 iPhone 上使用 ChatGPT? 适用于 iPhone 的最佳 ChatGPT 应用程序 在过去的几周里,出现了许多 ChatGPT 应用程序和网站.如果 ...

  7. iOS在图片上添加文字或图片

    Objective-C在图片上添加文字,请使用如下方法: /**在图片上添加文字,只支持英文,如果想添加其他文字,请看下面的方法@param image 图片@param string 要添加的文字@ ...

  8. iOS在图片上添加文字 图片

    图片上添加文字,在网上找的都是iOS10弃用的一个方法(怕以后麻烦就找了另一种方法替代)弃用链接 简洁版 - (UIImage*)text:(NSString*)text addToImage:(UI ...

  9. 如何在iOS或macOS上检查活动的Internet连接?

    我想检查一下我是否在使用Cocoa Touch库的iOS上或在使用Cocoa库的macOS上建立了Internet连接. 我想出了一种使用NSURL做到这一点的方法. 我这样做的方式似乎有点不可靠(因 ...

最新文章

  1. 为Office Communicator启用超链接
  2. Asp.net用户管理API的应用(上)
  3. python解释器是什么-python-解释器在这里做什么
  4. python四大高阶函数求导_4个python常用高阶函数的使用方法
  5. 【牛客网】迷途的牛牛 C++
  6. php判断对象属于哪个类,PHP instanceof:判断对象是否属于某个类
  7. 前端转行大数据?没必要
  8. oracle比mysql查询快的原因_Oracle查询速度慢的原因总结
  9. 数字能排序字符串不能排序_动图解说堆排序原理,让体育生也能看得明白
  10. oracle不存在共享内存域,Oracle数据库共享内存分配不足怎么办
  11. vs2013使用记录
  12. 讨论下怎么能实现保证新闻时效性推荐算法机制
  13. FCK添加远程图片自动下载
  14. WinRAR去广告:只需六步,教你去除WinRAR的广告
  15. 计蒜客蓝桥杯模拟赛---青出于蓝而胜于蓝
  16. 区块链技术指南学习(五)双花
  17. 电信物联网平台插件开发相关总结
  18. pandas爬虫爬取网页表格
  19. 【Java开发语言 03】第三章 面向对象编程(面向对象与面向过程+类和对象+类成员一:属性+类成员二:方法+对象的创建和使用+封装和隐藏+构造器+关键字this,package,import)
  20. 记录本地 Docker 新建镜像,推送到Daocloud

热门文章

  1. 淘宝双12惊喜——“寻”千兆,万兆光模块等你来
  2. java 学习之List 的 add 与set方法区别
  3. 【持久化框架】SpringMVC+Spring4+Mybatis3 集成,开发简单Web项目+源码下载
  4. java 面向对象之内存管理
  5. php ActiveMQ的安装与使用
  6. Spring mvc 拦截静态资源配置管理
  7. Laravel教程 六:表单 Forms
  8. HTML5 的定位一些重要知识点
  9. 软件工程里的UML序列图的概念和总结
  10. android优化(json工具,message新建/传递,avtivity深入学习视频)