Xamarin-ios share & receive audio file

2019-06-17 05:14发布

问题:

How can I share a audio file from my app to other apps and receive audio files from other apps?

I would like a functionality like this:

I am unable to attach a audio file. Here is the code snippet:

var fileData = NSUrl.FromFilename(path);  
var item = UIActivity.FromObject(@"<html><body><b>URL</b>: <a href='" + url + "'>" + url + "</a></body></html>");// NSObject.FromObject(uiImage);
var activityItems = new[] { fileData };
NSString message = (NSString)"Test";
var activityController = new UIActivityViewController(activityItems, null);
activityController.SetValueForKey(NSObject.FromObject("The subject value"), new NSString("subject"));
var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (topController.PresentedViewController != null)
{
    topController = topController.PresentedViewController;
}
topController.PresentViewController(activityController, true, () => { });

I would also like to add subject and body if file attached as mail.

tried this but no luck, getting an error.:

activityController.SetValueForKey(NSObject.FromObject("The subject value"), new NSString("subject"));
activityController.SetValueForKey(NSObject.FromObject("The body value"), new NSString("body"));

Also I would like to receive audio files shared by other apps. I have done the feature in android using dependency services and intent filters. For android the code is been added to mainactivity.cs where I use intent filters to accept audio files from other app. How to do that in iOS?

回答1:

You can share and receive files by sharing data between the main app and an extension. Note that your app and the app extension should belong to the same app group.

  1. App Extension Essentials https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/index.html#//apple_ref/doc/uid/TP40014214-CH20-SW1
  2. Introduction to Extensions https://developer.xamarin.com/guides/ios/platform_features/introduction_to_extensions/#Overview
  3. Share overview with example http://www.technetexperts.com/mobile/share-extension-in-ios-application-overview-with-example/