4

I am writing an app with the "App Groups" capability enabled. The Action Extension in this app is open to PDF files and is available when a web page loaded in Safari is opened in reader mode and then converted to PDF.

In short, the app can take in the Webpage converted to PDF file from Safari. It was working alright before upgrading to Swift 4.2. Ever since downloading Xcode 10, it has stopped working with the following error:

(Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load 
representation of type com.adobe.pdf"  UserInfo   
{NSLocalizedDescription=Cannot load representation of type 
com.adobe.pdf, NSUnderlyingError=0x600002dd9a70 {Error
Domain=NSPOSIXErrorDomain Code=22 "Invalid argument" UserInfo=    
{NSLocalizedDescription=Cannot issue a sandbox extension for file  
"/Users/xxx/Library/Developer/CoreSimulator/Devices/FE5463C2-FAA3-
41A9938B-C1C234EA966A/data"/Containers/Data/Application/B6FB42C6-B4E3-
46D8-B9F95856FF88F0B6/tmp//Safari - Sep 22, 2018 at 10:00 PM.pdf": 
Invalid argument}}})`

Can anyone throw some light on what is happening? Both the app and its action extension belong to the same App Group. The Action Extension has the following entries in the info.plist:

<dict>
  <key>NSExtensionAttributes</key>
  <dict>
  <key>NSExtensionActivationRule</key>
  <string>
                SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                $extensionItem.attachments,
                $attachment,
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url"
                ).@count == $extensionItem.attachments.@count
                ).@count == 1
            </string>
  </dict>
  <key>NSExtensionMainStoryboard</key>
  <string>MainInterface</string>
  <key>NSExtensionPointIdentifier</key>
  <string>com.apple.ui-services</string>
  </dict>
Mihir
  • 137
  • 2
  • 12

2 Answers2

1

It looks like it is a bug in Xcode 10 iOS 12 Simulator only.

Error is nil on real iOS 12 device and share extension works on both iOS 11 and iOS 12 devices.

I faced with this issue on our project. Shared extension stopped working under iOS 12 Simulators, but it is still working under iOS 11 Simulators in Xcode 10.

itemProvider.loadItem(forTypeIdentifier: typeIdentifier, options: nil) { secureCoding, error in
}

I spent more than a half of a day trying different parameters according to the doc: https://developer.apple.com/documentation/foundation/nsitemprovider/completionhandler

Nothing helped - always same error from "NSItemProvider's loadItem" under iOS 12 Simulators:

"Cannot load representation of type public.jpeg" "Invalid argument"

Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load representation of type public.jpeg" UserInfo={NSLocalizedDescription=Cannot load representation of type public.jpeg, NSUnderlyingError=0x6000005e1fe0 {Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument" UserInfo={NSLocalizedDescription=Cannot issue a sandbox extension for file "/Users/user/Library/Developer/CoreSimulator/Devices/4771D8A8-E366-43CB-8A2E-7FF397E4CF6A/data/Media/PhotoData/OutgoingTemp/82644FB8-E3B8-45DE-A0BF-563DF597872D/IMG_0003.JPG": Invalid argument}}}

But then, I decided to check it on a real device under iOS 12.

Everything is working perfectly on a device!

slamor
  • 3,355
  • 2
  • 14
  • 13
0

The solutions works for me is iOS has stopped this kind of support in iOS12 so what i did to fix this issue. Earlier i was using this completion handler for this method loadItemForTypeIdentifier

completionHandler:^(id <NSSecureCoding> urlItem, NSError *error)

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 macOS). If the data could not be retrieved or coerced to the specified class, an error is passed to the completion block’s.

- (void)getFilelist :(NSItemProvider *)itemProvider
 setPublicIdentifier:(NSString *)indentifier
                    :(void (^)(void))complete {

    [itemProvider loadItemForTypeIdentifier:indentifier options:nil completionHandler:^(NSURL *  _Nullable item, NSError * _Null_unspecified error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSURL *selectURl = (NSURL*)item;
            if ([[selectURl pathExtension] isEqualToString:@"pdf"] || [[selectURl pathExtension] isEqualToString:@"xlsx"] || [[selectURl pathExtension] isEqualToString:@"csv"]) {
                int randomID = arc4random() % 9000 + 1000;
                NSString *filename = [[(NSURL*)item path] lastPathComponent];
                if ([filename isEqualToString:@"FullSizeRender.jpg"]) {
                    filename = [NSString stringWithFormat:@"MD%d.jpg",randomID];
                }
                [_items addObject:selectURl.absoluteString];
                complete();
            }
        });
    }];
}

Call this like this

    for (NSItemProvider *itemProvider in item.attachments) {

            if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeText]) {
                [self getFilelist:itemProvider
                    setPublicIdentifier:(NSString *)kUTTypeText
                                       :^{
                                           openFile(self, i, totalnt);
                                       }];
            }

            if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePDF]) {
                [self getFilelist:itemProvider
                  setPublicIdentifier:(NSString *)kUTTypePDF
                                     :^{
                                         openFile(self, i, totalnt);
                                     }];
            }

            if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeSpreadsheet]) {
                [self getFilelist:itemProvider
                  setPublicIdentifier:(NSString *)kUTTypeSpreadsheet
                                     :^{
                                         openFile(self, i, totalnt);
                                     }];
            }

    }
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59