使用 Xamarin 在同一设备 iOS/Android 上的 2 个应用程序之间进行通信 [英] Communication between 2 apps on same device iOS/Android with Xamarin

查看:27
本文介绍了使用 Xamarin 在同一设备 iOS/Android 上的 2 个应用程序之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在开发一个应用程序,它是我们主应用程序的附加"应用程序,以扩展其可能性.

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.

这是服务器的 C# 代码:

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);

                }
            }
        }
    }

对于客户:

    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.

这是在这两个应用程序之间进行通信的通用方式(始终在同一设备上)还是我们必须开发单独的解决方案?如果单独的解决方案,iOS 的最佳解决方案是什么?安卓 ?我们将 Xamarin 用于我们的附加应用,目前我们仅面向 iOS 和 Android.

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.

推荐答案

经过多方搜索,似乎唯一的跨平台"解决方案是Url Scheme.

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

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

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

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

Android 似乎可以处理 1Mb 的数据以在 Intent 中传递,而 iOS 可以处理 URL 中系统内存允许的尽可能多的数据.我们将对数据进行 base64url 编码以将其传递给 iOS,并将其作为 base64 添加到用于 Android 的 Intent 的 extraString 数组中.

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.

我们必须创建一个 Android Activity 和一个 iOS ViewController 来处理 Url 的调用,因此这是特定于平台的,但主要智能可以在平台之间共享.

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.

通过这样做,我们的附加应用程序将不需要是 iOS 应用程序的扩展,并且可以拥有自己的界面和屏幕.

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.

这篇关于使用 Xamarin 在同一设备 iOS/Android 上的 2 个应用程序之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆