Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share Extension loadItemForTypeIdentifier returns error for NSURL

I have the following code to read in passed URLs. I'm testing this with the Pocket app and although hasItemConformingToTypeIdentifier is returning YES for kUTTypeURL, trying to load it in returns a error instead stating

"Unexpected value class."

. If I try to load it as an id<NSSecureCoding> item and debug, I find that the passed in object is indeed just the title of the page and not the URL. How do I read the URL?

  NSURL *pageURL = nil;
  for (NSExtensionItem *item in self.extensionContext.inputItems) {
    for (NSItemProvider *itemProvider in item.attachments) {
     if ([itemProvider hasItemConformingToTypeIdentifier: (NSString*) kUTTypeURL]) {
        [itemProvider loadItemForTypeIdentifier:(NSString*) kUTTypeURL options:nil completionHandler:^(id <NSSecureCoding> urlItem, NSError *error) {
          if ([((NSObject*)urlItem) isKindOfClass: [NSURL class]]) {
              pageURL = [((NSURL*)urlItem) absoluteString];
          }
        }];
      }
    }
  }
like image 792
strangetimes Avatar asked Feb 04 '15 17:02

strangetimes


1 Answers

If you read the documentation for:

loadItemForTypeIdentifier(_:options:completionHandler:)

You'll see that:

The type information for the first parameter of your completionHandler block should be set to the class of the expected type. For example, when requesting text data, you might set the type of the first parameter to NSString or NSAttributedString. An item provider can perform simple type conversions of the data to the class you specify, such as from NSURL to NSData or NSFileWrapper, or from NSData to UIImage (in iOS) or NSImage (in OS X). If the data could not be retrieved or coerced to the specified class, an error is passed to the completion block’s.

Maybe you can experiment by coercing to different types?

like image 152
Vamos Avatar answered Nov 02 '22 16:11

Vamos