swift 如何在IOS應用表徵圖上添加訊息數,swiftios

來源:互聯網
上載者:User

swift 如何在IOS應用表徵圖上添加訊息數,swiftios

  在應用表徵圖右上方添加訊息數提醒,可以很方便的告知使用者該應用中有無新訊息需要處理。下面用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 UIKit10 11 @UIApplicationMain12 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 true27     }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 UIKit10 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,如所示:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.