简体   繁体   English

使用Xamarin在同一设备iOS / Android上的2个应用程序之间进行通信

[英]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 : 这是服务器的C#代码:

    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 ? 如果单独的解决方案,什么是iOS的最佳解决方案? Android ? Android? We used Xamarin for our add-on app, and we currently only target iOS and Android. 我们将Xamarin用于我们的附加应用程序,目前我们只针对iOS和Android。

Note: because it's same device, we don't want to use distant webservice to communicate. 注意:因为它是同一台设备,我们不希望使用远程Web服务进行通信。

After many searches, it seems that the only "cross-platform" solution is Url Scheme. 经过多次搜索,似乎唯一的“跨平台”解决方案是Url Scheme。

For iOS : https://developer.xamarin.com/recipes/cross-platform/app-links/app-links-ios/ 对于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/ 对于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. 似乎Android可以处理意图中传递的1Mb数据,iOS可以处理URL中系统内存允许的数量。 We will base64url encode data to pass it for iOS and add it as base64 in intent's extraString array for Android. 我们将base64url编码数据传递给iOS,并将其作为base64添加到Android的intent的extraString数组中。

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. 我们将不得不创建一个Android Activity和一个iOS ViewController来处理Url的调用,因此这是特定于平台的,但主要智能可以在平台之间共享。

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. 通过这样做,我们的附加应用程序不需要是iOS应用程序的扩展,并且可以拥有自己的界面和屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM