I am developing a xamarin.forms apps(android, iOS, windows). My app has a share feature which allows user to share audio files to other app and receive audio files from other apps. I have done that in android using intent filers and stuff, but not sure what I need to do for iOS. I read about share extensions. Do I need to use that or any other way? Any help would be appreciated.
Edit How to go about share extension in iOS xamarin?
EDIT
Share logic in 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"));
Receiving Shared file in 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();
}
}
}
...
}
I also managed to share a audio file from iOS but no luck receiving file from other apps.
iOS code to share audio file:
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);
This above code doesn't allow to share audio via mail and doesn't add subject and a body to message. Any help on that? And how to receive a file from other apps?
To share data between your app and arbitrary other apps, you'll need to use a UIActivityViewController
. Read through the documentation and this article from NSHipster for a rundown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With