app进入后台通知 swift
2016-06-15 22:22:38 lcg910978041 阅读数 1199
class ViewController: UIViewController {
    
    @IBOutlet weak var message: UILabel!
    @IBOutlet weak var myNotification: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.updateInterface(_:)), name: UIApplicationWillEnterForegroundNotification, object: nil)
    }
    
    func updateInterface (notification: NSNotification){
        myNotification.text = "Back to background with notification"
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}
按下home键之后, myNotification的text永久的改为 "Back to background with notification"
2019-01-07 14:15:27 u011146511 阅读数 556
 //注册进入前台的通知
        NotificationCenter.default.addObserver(self, selector:#selector(becomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
        //注册进入后台的通知
        NotificationCenter.default.addObserver(self, selector:#selector(becomeDeath), name: UIApplication.willResignActiveNotification, object: nil)
       
  
   
   
    //必须要加@objc-----进入前台
    @objc  func becomeActive(noti:Notification){
        
        print("进入前台")
    }
    //必须要加@objc----进后台
    @objc  func becomeDeath(noti:Notification){
        
        print("进入后台")
    }
    

 

2016-07-05 17:58:48 Emperor_huanzi 阅读数 1401
 NSNotificationCenter.defaultCenter().addObserver(self, selector: "becomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "becomeDeath", name: UIApplicationWillResignActiveNotification, object: nil)

2016-07-15 15:14:42 walkerwqp 阅读数 826

 //添加本地通知

    func  addLocalNotification() {

        

        //定义本地通知对象

        let notification : UILocalNotification = UILocalNotification()

        

        //设置调用时间

        notification.fireDate = NSDate.init(timeIntervalSinceNow: 1800.0)//通知触发的时间,10s以后


        

        notification.repeatInterval = NSCalendarUnit.init(rawValue: 2) //通知重复次数

        

        //设置通知属性

        notification.alertBody = "您是否有未结束的乘车备案记录,记得结束备案哟!"  //通知主体

        

        //应用程序图标右上角显示的消息数

        notification.applicationIconBadgeNumber = 1

        

        //通过点击通知打开应用时的启动图片,这里使用程序启动图片

        notification.alertLaunchImage = "Default"

        

        //通知声音(需要真机才能听到声音)

        notification.soundName = "msg.caf"

        

         //调用通知

        UIApplication.sharedApplication().scheduleLocalNotification(notification)

        

    }

    

    //移除本地通知,在不需要此通知时记得移除

    

    func removeNotification() {

        

        UIApplication.sharedApplication().cancelAllLocalNotifications()

        

        

    }

    

只需要调用即可
2017-03-10 09:48:37 lausen34 阅读数 785

偶然的机会碰到了UIVisualEffectView(菜鸟一只,不要嘲笑),发现原来苹果就是靠这个实现高斯模糊的.
在appdelegate里面先声明一个全局的常量,
然后当应用程序辞去激活的时候,添加一个高斯模糊的试图到应用程序上,
当应用程序已经被激活的时候,在移除这个高斯模糊的试图,
代码如下:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    let visualEffect = UIVisualEffectView.init(effect: UIBlurEffect.init(style: .extraLight))

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))

        window?.makeKeyAndVisible()

        window?.rootViewController = JCTTabBarController()

        return true
    }

    //MARK -- 程序辞去激活时调用的方法
    func applicationWillResignActive(_ application: UIApplication) {

        visualEffect.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        visualEffect.alpha = 0.7

        application.keyWindow?.addSubview(visualEffect)
    }

    func applicationDidEnterBackground(_ application: UIApplication) {

    }

    func applicationWillEnterForeground(_ application: UIApplication) {

    }

    //MARK -- 程序已经被激活时调用的方法
    func applicationDidBecomeActive(_ application: UIApplication) {

        visualEffect.removeFromSuperview()
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

swift - 通知

阅读数 121

swift 通知

阅读数 269

swift通知

阅读数 50

swift 通知的封装

阅读数 334

swift 发送通知

阅读数 4

没有更多推荐了,返回首页