Call a custom method in NetworkExtension framework from ViewController?

Hi there,


Wondering if it is possible to call a custom method written in NetworkExtension framework as a response to a button click on a viewcontroller? Here is what I am trying to do:


- Press a button on the viewcontroller (that belongs to a different target) triggering a method call in the NetworkExtension framework.


Thanks for reading!

Answered by DTS Engineer in 411298022

If this is an app extension, the standard approach is called “app messages”. The app calls

-sendProviderMessage:returnError:responseHandler:
and the message arrives on the provider via
-handleAppMessage:completionHandler:
.

This should also work for a system extension but in that case you have an XPC option as well.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

This is not possible out of the box without something like XPC to delegate the communication between the host app and the Network Extension. Also, this not something that is a common place pattern in most Network Extension applications, as state observation changes along with starting and stopping a VPN connection can be done in a host app. Can you tell me more about what you are trying to achieve?



Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

Thanks for a quick response.


On a higher level, I am using PacketTunnelProvider and inside the extension, as part of writing the custom VPN protocol, I would like a method to be called as a response to the interaction from the user with the host app view controller. Is this possible?


If so, is the delegate the way to go? I would appreciate an example for this.
If not, I would then like to explore other options.
Thanks again for taking the time to respond.

Accepted Answer

If this is an app extension, the standard approach is called “app messages”. The app calls

-sendProviderMessage:returnError:responseHandler:
and the message arrives on the provider via
-handleAppMessage:completionHandler:
.

This should also work for a system extension but in that case you have an XPC option as well.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks so much! The answer was exactly what I needed.


I just finished implementing and tested; such an elegant way to exchange messages between the host app and the extension.

Call a custom method in NetworkExtension framework from ViewController?
 
 
Q