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

后端 未结 2 831
鱼传尺愫
鱼传尺愫 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:47

    I was dealing with similiar problem a while ago. Few important things to keep in mind:

    • background "voip" functionality only works on device - don't use simulator to test it
    • you will probably (tested) got rejected if your app registers as a voip app and isn't really voip app

    So if this is not a voip app you might actually want to use remote notifications to alert user directly rather than showing local notification. I guess this is the only way for your app to pass App Store validation.

    Anyway, two links here on SO helped you might find helpful:

    How can an iOS app keep a TCP connection alive indefinitely while in the background?

    I ended up using voip (as you do) and playing silent audio loop as suggested here - it worked. Not sure if this silent audio loop is still neccessary.

    What happens to TCP and UDP (with multicast) connection when an iOS Application did enter background

    Make sure you read Tips for Developing a VoIP App and Technical Note TN2277:Networking and Multitasking

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题