iOS application lifecycle and network extension. How?

Hello all!

The aplication that is developing by me now contain Network Extension and Packet Filtering functionality. The question is about lifecycles and how to handle application lifecycles (active, background, will be terminated) and network extension lifecycles together.

There are few questions:

  • Is there any manuals about iOS Application Extensions lifecycles and how to handle it in case of main application lifecycle?
  • Is there something special in case of Network Extension lifecycle?
  • How to correctly handle Network extension crash?
  • How to correctly handle Background mode?

Is there any examples, references or manuals about all of this topics?

The app extensions running your Network Extension provider has a very different lifecycle from that of a normal app. It’s also very different from a GUI app extension, like say a share extension. Specifically:

  • It always run in the background.

  • The system starts and stops the app extension based on the desired lifecycle of the NE provider that it hosts.

So:

Is there any manuals about iOS Application Extensions lifecycles and how to handle it in case of main application lifecycle?

The best general introduction to app extensions is the App Extension Programming Guide but…

Is there something special in case of Network Extension lifecycle?

… it’s not particularly relevant to NE app extensions.

How to correctly handle Network extension crash?

A crash of what?

NE app extensions shouldn’t crash. If they do, that’ll ‘break’ the network functionality that they provide. For example, if your NE app extension hosts a packet tunnel provider, it crashing will tear down the tunnel interface it’s providing. Of course, depending on how VPN On Demand is configured, the system may then attempt to bring it back up again.

How to correctly handle Background mode?

This question doesn’t make much sense in the context of an NE app extension; it is always running in the background.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

As far as it known for me there are classical lifecycle of iOS application For me it's a little shadowed:

  1. When in classical lifecycle Network Extension running?
  2. What should be done for Network Extension in case of Application is in or out of Active/Inactive/Background/Suspended state?
  3. What should be done by Application in case of something wrong in Network Extension and how NE could be noticing about own state?
  4. Is there any limits for Network Extension? Memory? CPU usage Time? Access to something? Etc ...
  5. Is there any examples of "best practice" of how to orginise communication between Application and Network Extension?

If there any info that you can send me to help, it would be greate. My application written on C++ with CMake.

As far as it known for me there are classical lifecycle of iOS application

That’s true, but it doesn’t really apply to an NE appex. An NE appex runs in a separate process from the container app. The lifecycle of that process is independent of the container app. Rather, the appex acts like a plug-in to the system — in appex terminology, the plug-in’s host is the system itself — and it’s the system that controls when it starts and stops.

1. When in classical lifecycle Network Extension running?

2. What should be done for Network Extension in case of Application is in or out of Active/Inactive/Background/Suspended state?

Given the architecture I’ve explained above, these questions aren’t meaningful.

3. What should be done by Application in case of something wrong in Network Extension and how NE could be noticing about own state?

If the user actively runs your NE appex’s container app, it can monitor the extensions state using three different mechanisms:

However, the container app may not be running, in which case your provider is on its own. If it needs to get the user’s attention, it should post a local notification using the User Notifications framework.

IMPORTANT In a managed environment it’s common for a site admin to configure your provider using a configuration profile. In that case it’s possible for your appex to run without the user ever having run your app.

4. Is there any limits for Network Extension? Memory? CPU usage Time? Access to something? Etc ...

NE providers have many limits. For example, they can’t display UI. However, the one that folks most commonly bump into is the memory limit. See this post.

5. Is there any examples of "best practice" of how to orginise communication between Application and Network Extension?

From Apple? Not really. We have no good NE provider sample code [1]. Moreover, it’s hard to capture best practice because there’s such a wide variety of requirements across NE providers.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] The canonical example here is SimpleTunnel, but it’s now very out of date.

NE providers have many limits. - Is there any list of all of limits, not just memory?

Is there any list of all of limits

Nothing definitive. I’m not ever sure how you would come up with such a list. It’s pretty hard to document the stuff that you can’t do.

Rather, focus on the expected use case for your NE provider. For example, if you’re building a packet tunnel provider, it’s intended to be used as a VPN, similar to IKEv2 or OpenVPN or whatever. If you try to use it for something else, you might run into weird problems [1]. If you try to do something completely off the wall — say using SwiftUI to interact with the user — that’ll just fail.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] See TN3120 Expected use cases for Network Extension packet tunnel providers.

iOS application lifecycle and network extension. How?
 
 
Q