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

后端 未结 4 881
名媛妹妹
名媛妹妹 2020-12-02 09:53

An iPhone app, connecting to a remote server via TCP. The use scenarios are:

  1. app (user) sends data to server and server responds data back.
  2. server mig
4条回答
  •  一个人的身影
    2020-12-02 10:21

    Please have a look at Implementing a VoIP Application in Apple's iOS Application Programming Guide. This is no tested solution, but I think it can be used to achieve what you are asking for.

    Implementing a VoIP Application

    A Voice over Internet Protocol (VoIP) application allows the user to make phone calls using an Internet connection instead of the device’s cellular service. Such an application needs to maintain a persistent network connection to its associated service so that it can receive incoming calls and other relevant data. Rather than keep VoIP applications awake all the time, the system allows them to be suspended and provides facilities for monitoring their sockets for them. When incoming traffic is detected, the system wakes up the VoIP application and returns control of its sockets to it.

    I am not sure if you're developing a VoIP app but you'll be able to access the sockets and react on incoming packets. The text after the quote refers to [setKeepAliveTimeout:handler:][2] which allows you to set a block of code which allows you to keep alive a(your VoIP) connection. Minimum scheduling time is 10 minutes, though.

    In Implementing a VoIP Application is a paragraph on Installing a Keep-Alive Handler. I am curious if this will help. Please give us a note if this solved your problem.

提交回复
热议问题