13

We currently are developping an app that is sort of "add-on" app for our main app to extends its possibilities.

We asked ourselves if there's simple inter-app communication on same device (nothing found on the internet for this...). Our first thought was to create a server socket on our add-on app, and send data from main app to it.

Here's C# code for server :

    public async Task Start()
    {
        Listener = new TcpListener(IPAddress.Parse(GetIP()), 7777);
        var client = Listener.AcceptTcpClient();
        while (true)
        {
            while (!client.GetStream().DataAvailable) ;
            using (NetworkStream stream = client.GetStream())
            {
                byte[] data = new byte[client.Available];
                stream.Read(data, 0, client.Available);
                if (data.Length > 0)
                {
                    String s = Encoding.UTF8.GetString(data);
                    if (!string.IsNullOrWhiteSpace(s))
                        OnMessageRecevied?.Invoke(s);

                }
            }
        }
    }

And for client :

    public async Task SendMessage(string msg)
    {
        tClient = new TcpClient();
        var buffer = Encoding.UTF8.GetBytes(msg);
        while (!tClient.Connected)
        {
            tClient.Connect(IPAddress.Parse(Server.GetIP()), 7777);
            Thread.Sleep(100);
        }
        await tClient.GetStream().WriteAsync(buffer, 0, buffer.Length);
        tClient.Close();
    }

It seems not working, cause we our main app takes focus, the add-on app seems like to stop listening.

Is this a generic way to communication between these two apps (always on same device) or will we have to develop separate solution ? if separate solutions, what's the best solution for iOS ? Android ? We used Xamarin for our add-on app, and we currently only target iOS and Android.

Note: because it's same device, we don't want to use distant webservice to communicate.

cdie
  • 4,014
  • 4
  • 34
  • 57
  • Will the add-on app have a UI, or does it just provide background functionality? A local service running on device could be part of the solution, either as a "bridge" in the former case, or as the provider of add-on functionality in the latter. – Mark Larter Feb 22 '16 at 17:10
  • The add-on app will have it's own UI if needed, but mainly works as service. However, it seems like local service is only available for Android (or it's named different in iOS and I haven't found it) – cdie Feb 23 '16 at 08:02
  • iOS's sandboxing requirements will prevent this sort of thing, you can communicate between two apps using the new extensions architecture introduced https://developer.apple.com/app-extensions/ – Clint Feb 23 '16 at 11:22
  • Ok for that, but it seems like iOS's extensions are not allowed to run on background... With this, how can we establish dialog with the main app to retrieve and send back data – cdie Feb 23 '16 at 11:43
  • Do you have any tutorial link for generating an add-on app? I need the same thing – Reihaneh Khaksaran Dec 31 '18 at 07:46

1 Answers1

1

After many searches, it seems that the only "cross-platform" solution is Url Scheme.

For iOS : https://developer.xamarin.com/recipes/cross-platform/app-links/app-links-ios/

For Android : https://developer.xamarin.com/recipes/cross-platform/app-links/app-links-android/

It seems that Android can handle 1Mb of data to be passed in an intent, and iOS can handle as much as the system memory allow in URL. We will base64url encode data to pass it for iOS and add it as base64 in intent's extraString array for Android.

We will have to create an Android Activity and an iOS ViewController to handle the Url's calls, so this is platform specific, but the main intelligence can be shared between platforms.

By doing this, our add-on app will not need to be an extension of iOS' app, and can have its own interface and screens.

cdie
  • 4,014
  • 4
  • 34
  • 57