Home > Enterprise >  How to read a pdfdoc from a iOS DropInteraction in Xamarin
How to read a pdfdoc from a iOS DropInteraction in Xamarin

Time:10-27

Like in this post I want to receive a pdf document via an inter-app drag and drop interaction. The IUIDropSession contains an identifier of com.adobe.pdf when I drag my pdf file over my component, PerformDrop is also called, but LoadPdfsis not. The app crashes. Why?

[Export("dropInteraction:canHandleSession:")]
public bool CanHandleSession(UIDropInteraction interaction, IUIDropSession session)
{
  return session.CanLoadObjects(typeof(PDFDocument)); // This returns true
}

[Export("dropInteraction:sessionDidUpdate:")]
public UIDropProposal SessionDidUpdate(UIDropInteraction interaction, IUIDropSession session)
{
  return new UIDropProposal(UIDropOperation.Copy);
}

[Export("dropInteraction:performDrop:")]
public void PerformDrop(UIDropInteraction interaction, IUIDropSession session)
{
  session.LoadObjects<PDFDocument>(LoadPdfs); // CRASH WHEN EXECUTING THIS LINE
}

private void LoadPdfs(PDFDocument[] items)
{
  // This is not called
}

where the PdfDocument is this class

class PDFDocument : NSObject, INSItemProviderReading
{
  string identifier;
  NSData data;

  public PDFDocument(NSData pdfData, string typeIdentifier)
  {
     data = pdfData;
     identifier = typeIdentifier;
  }

  [Export("objectWithItemProviderData:typeIdentifier:error:")]
  static public INSItemProviderReading GetObject(NSData data, string indentifier, NSError error)
  {
     return new PDFDocument(data, indentifier);
  }

  [Export("readableTypeIdentifiersForItemProvider")]
  static public string[] ReadableTypeIdentifiers  => new string[]
  {
     "com.adobe.pdf"
  };

  [Export("withItemProviderData")]
  public static object WithItemProviderData(NSData data, string typeIdentifier)
    => new PDFDocument(data, typeIdentifier);
}

Crash:

=================================================================
    Native Crash Reporting
=================================================================
Got a segv while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Native stacktrace:
=================================================================
    0x105c485ac - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/Frameworks/Mono.framework/Mono : mono_dump_native_crash_info
    0x105c3e724 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/Frameworks/Mono.framework/Mono : mono_handle_native_crash
    0x105c4ca2c - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/Frameworks/Mono.framework/Mono : mono_sigsegv_signal_handler_debug
    0x1e0b1fd50 - /usr/lib/system/libsystem_platform.dylib : <redacted>
    0x104e29a34 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : xamarin_get_block_descriptor
    0x104e2976c - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : xamarin_get_block_descriptor
    0x104e296b4 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : xamarin_get_block_descriptor
    0x100d41aac - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : 
    0x100d418c8 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : 
    0x1898b7db4 - /System/Library/Frameworks/Foundation.framework/Foundation : <redacted>
    0x189927030 - /System/Library/Frameworks/Foundation.framework/Foundation : <redacted>
    0x187d4e2ec - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187d4f2f0 - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187cf554c - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187cf5ff0 - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187cffae4 - /usr/lib/system/libdispatch.dylib : <redacted>
    0x1e0b2af38 - /usr/lib/system/libsystem_pthread.dylib : _pthread_wqthread
    0x1e0b2aaa4 - /usr/lib/system/libsystem_pthread.dylib : start_wqthread

=================================================================

CodePudding user response:

Try to set the original method signature instead of the extension method .

session.LoadObjects(new Class(typeof(PDFDocument)), (INSItemProviderReading[] items)=>
{
     PDFDocument[] docs = items as PDFDocument[];
});

Refer to

https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination?language=objc .


Update

Try to simply use the following code to see if it works or not .

[Export("dropInteraction:performDrop:")]
public void PerformDrop(UIDropInteraction interaction, IUIDropSession session)
{
    var label = interaction == oddDropInteraction ? OddNumbersLabel : EvenNumbersLabel;
    session.LoadObjects(new Class(typeof(NSString)),strings =>
    {
        ///
    });
}

Refer to

https://devblogs.microsoft.com/xamarin/drag-and-drop-apis-for-xamarin-apps/

  • Related