c# - iOs 等同于 Android 的共享功能

标签 c# xamarin.ios

我正在开发 xamarin.forms 应用程序(android、iOS、windows)。我的应用程序具有共享功能,允许用户将音频文件共享到其他应用程序并从其他应用程序接收音频文件。我已经在 android 中使用意图文件管理器和其他东西完成了这项工作,但不确定我需要为 iOS 做什么。我阅读了有关共享扩展的信息。我需要使用那个或任何其他方式吗?任何帮助将不胜感激。

编辑 如何在 iOS xamarin 中进行共享扩展?

编辑

Android 中的共享逻辑:

Intent sharingIntent = new Intent(Android.Content.Intent.ActionSend);
sharingIntent.SetType("audio/*");
sharingIntent.PutExtra(Android.Content.Intent.ExtraSubject, "Share");
if (!String.IsNullOrEmpty(url))
{
    sharingIntent.PutExtra(Android.Content.Intent.ExtraText, Html.FromHtml(new StringBuilder()
        .Append("<p><b>URL</b> : ")
        .Append("<a href='" + url + "'>" + url + "</a></p>")
        .ToString()));
}
sharingIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(new Java.IO.File(path)));
Forms.Context.StartActivity(Intent.CreateChooser(sharingIntent, "Share via"));

在 MainActivity.cs 中接收共享文件:

[IntentFilter(new[] {  Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "audio/*")]
[IntentFilter(new[] { Intent.ActionSendMultiple }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "audio/*")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    async protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Intent intent=Intent;
        String action = Intent.Action;
        String type = Intent.Type;
        if (Intent.ActionSend.Equals(action) && type != null)
        {
            if (type.StartsWith("audio/"))
            {
                Bundle bundleExtra = Intent.Extras;
                if (bundleExtra != null)
                {
                    Android.Net.Uri sharedFileURI = (Android.Net.Uri)bundleExtra.Get(Intent.ExtraStream);
                    await Upload(sharedFileURI); //upload logic here
                }
            }
            else
            {
                new AlertDialog.Builder(this)
                .SetPositiveButton("Ok", (sender, args) =>
                {
                    // User pressed yes
                })
                .SetMessage("Not an audio file!")
                .SetTitle("Alert")
                .Show();
            }
        }
        else if (Intent.ActionSendMultiple.Equals(action) && type != null)
        {
            if (type.StartsWith("audio/"))
            {
                Bundle bundleExtra = Intent.Extras;
                if (bundleExtra != null)
                {
                    System.Collections.IList imageUris = intent.GetParcelableArrayListExtra(Intent.ExtraStream);

                    if (imageUris.Count <= 1)
                    {
                        foreach (var p in imageUris)
                        {
                            Android.Net.Uri sharedFileURI = (Android.Net.Uri)p;
                            await Upload(sharedFileURI); //upload logic here
                        }
                    }
                    else
                    {
                        new AlertDialog.Builder(this)
                            .SetPositiveButton("Ok", (sender, args) =>
                            {
                                // User pressed yes
                            })
                            .SetMessage("Not an audio file!")
                            .SetTitle("Alert")
                            .Show();
                    }
                }
            }
            else
            {
                new AlertDialog.Builder(this)
                .SetPositiveButton("Ok", (sender, args) =>
                {
                    // User pressed yes
                })
                .SetMessage("You have choosen more than one file!")
                .SetTitle("Alert")
                .Show();
            }
        }
    }

    ...
}

我还成功地从 iOS 共享了一个音频文件,但没有成功地从其他应用程序接收文件。

分享音频文件的iOS代码:

var subviews = window.Subviews;
var view = subviews.Last();
var frame = view.Frame;
frame = new RectangleF(0, 0, 0, 0);
var uidic = UIDocumentInteractionController.FromUrl(new NSUrl(path, false));
var rc = uidic.PresentOpenInMenu(frame, view, true);

上述代码不允许通过邮件共享音频,也不会向消息添加主题和正文。有什么帮助吗?以及如何从其他应用程序接收文件?

最佳答案

要在您的应用和任意其他应用之间共享数据,您需要使用 UIActivityViewController。通读the documentationthis article from NSHipster一个破败。

关于c# - iOs 等同于 Android 的共享功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37590621/

相关文章:

ios - Xamarin - 绝对布局中带有文本和图像的按钮导致元素未对齐

ios - UITapGestureRecognizer 在 iOS 模拟器上崩溃

c# - 接口(interface)方法返回类型为实现接口(interface)的类

c# - 不同形式的 WCF 服务契约接口(interface)

c# - 如何使用 Parallel.ForEach 正确写入文件?

c# - 在 Properties.Settings.Default 与注册表中存储设置

c# - 查找 2 个数组中 2 个点之间的最小距离

c# - 使用 MonoTouch.Dialog 根据 RadioElement 的值显示多个 RootElement

xamarin.forms - Xamarin 形式的自定义键盘

c# - 从 NuGet 安装 Xamarin.Auth 时出错