Use device VPN when app is open

So far my googling has resulted in nothing.

I am developing an app completely for personal use to manage my home media server. Unfortunately I can only access this media server on my local network as my isp hasn’t given me my own IP.

The solution around this is settting up a vpn on my media server. I can access the device only when my phone is connected to the vpn.

Here’s what I want to do. I want to open my app, have my vpn automatically turn on, app can then do it’s functionality from anywhere on not just my local network, turn off vpn when app is closed.

I’m fairly new to Swift but have decent experience as a software engineer