Skip to content

Instantly share code, notes, and snippets.

@JigarM
Last active June 1, 2017 07:48
Show Gist options
  • Save JigarM/60ccee65cfd299613d82ec85dfc8429f to your computer and use it in GitHub Desktop.
Save JigarM/60ccee65cfd299613d82ec85dfc8429f to your computer and use it in GitHub Desktop.
Shre vieew controller Xamarin
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
}
public override void ViewDidAppear (bool animated)
{
try {
base.ViewDidAppear (animated);
//load items
var inputItem = ExtensionContext.InputItems [0];
NSItemProvider[] itemAttachmentsProvider = inputItem.Attachments;
attachmentItemCount = inputItem.Attachments.Length;
if (itemAttachmentsProvider != null && itemAttachmentsProvider.Length>0)
LoadMediaData(itemAttachmentsProvider);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
public override void DidSelectPost ()
{
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
//HACK: instead of uploading photos from here, get path and attach to app url and open host app
OpenHostApp (appSchemaUrl + invokeArgs);
}
//Load media data
void LoadMediaData(NSItemProvider[] itemAttachmentsProvider)
{
foreach(NSItemProvider itemProvider in itemAttachmentsProvider)
{
string typeIdentifier = string.Empty;
//i.e : Any type of image
if (itemProvider.HasItemConformingTo(UTType.Image))
typeIdentifier = UTType.Image;
//i.e : .mp4
if (itemProvider.HasItemConformingTo (UTType.MPEG4))
typeIdentifier = UTType.MPEG4;
//quick time video i.e : .mov
if (itemProvider.HasItemConformingTo (UTType.QuickTimeMovie))
typeIdentifier = UTType.QuickTimeMovie;
//for safari browser url
if (itemProvider.HasItemConformingTo(UTType.URL))
typeIdentifier = UTType.URL;
//for youtube video url from youtube app
if (itemProvider.HasItemConformingTo(UTType.PlainText))
typeIdentifier = UTType.PlainText;
if(!string.IsNullOrEmpty(typeIdentifier))
LoadItemWithTypeIdentifier(itemProvider, typeIdentifier);
}
}
//Load item with type identifier
void LoadItemWithTypeIdentifier(NSItemProvider item, string typeIdentifier)
{
itemIdx++;
// The app won't be able to access the images by path directly in the Camera Roll folder,
// so we temporary copy them to a folder which both the extension and the app can access:
string filePath = SaveMediaToAppGroupFolder (item, typeIdentifier);
if(string.IsNullOrEmpty(filePath))
return;
// Now add the path to the list of arguments we'll pass to the app:
AddMediaPathToArgumentList (filePath);
}
//Open MainHost App
void OpenHostApp(string url){
if (UIApplication.SharedApplication.CanOpenUrl (new NSUrl (url))) {
UIApplication.SharedApplication.OpenUrl (new NSUrl (url));
DispatchQueue.MainQueue.DispatchAfter (new DispatchTime (DispatchTime.Now, 3000000000), () => {
List<NSExtensionItem> list = new List<NSExtensionItem>();
ExtensionContext.CompleteRequest (list.ToArray(), null);
});
} else {
ShowAlert ("Can not share media");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment