How to handle socket connection's events when app is in background?

后端 未结 2 854
鱼传尺愫
鱼传尺愫 2020-12-09 22:57

I want use the following function even when app is in background?

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
 {
        case         


        
2条回答
  •  情深已故
    2020-12-09 23:48

    Use this code to keep your app alive in ios

    var backgroundUpdateTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier(rawValue: 0)
    
    func endBackgroundUpdateTask() {
        UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
        self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
    }
    func applicationWillResignActive(_ application: UIApplication) {
        self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            self.endBackgroundUpdateTask()
        })
    }
    func applicationDidBecomeActive(_ application: UIApplication) {
        self.endBackgroundUpdateTask()
    
    }
    

    i hope you will get success by this help

提交回复
热议问题