Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between 2 apps on same device iOS/Android with Xamarin

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.

like image 590
cdie Avatar asked Feb 22 '16 11:02

cdie


1 Answers

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.

like image 96
cdie Avatar answered Nov 01 '22 07:11

cdie