2

In the share extension, I managed to get the URL of the Safari page with the following code:

NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]){
    [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL
                                    options:nil
                          completionHandler:^(NSURL *url, NSError *error){
                              NSLog(@"%@", url.absoluteString);
                          }];
}

Can I get also the HTML of the page?

grg
  • 5,023
  • 3
  • 34
  • 50
benhi
  • 574
  • 1
  • 6
  • 24

1 Answers1

4

Check the following code,

[itemProvider loadItemForTypeIdentifier: (NSString *) kUTTypePropertyList
                                options: 0
                      completionHandler: ^(id<NSSecureCoding> item, NSError *error) {
                          if (item != nil) {
                              NSDictionary *resultDict = (NSDictionary *) item;
                              NSString *jsString = resultDict[NSExtensionJavaScriptPreprocessingResultsKey][@"content"];
                          }
                      }];

Creating an iOS App Extension to perform custom actions with Safari content - swiftiostutorials.com

grg
  • 5,023
  • 3
  • 34
  • 50
Azeem Akram
  • 120
  • 1
  • 2
  • 12
  • thank you for your answer but its not working... _Printing description of *(item): (id) [0] = _ _Printing description of item: _ – benhi Aug 11 '15 at 06:48
  • Printing description of error: Error Domain=NSItemProviderErrorDomain Code=6902136592 "No item available for requested type identifier." UserInfo=0x174269b80 {NSLocalizedDescription=No item available for requested type identifier.} – benhi Aug 11 '15 at 07:04
  • I have updated my answer. Please check that tutorial – Azeem Akram Aug 11 '15 at 20:18
  • @benhi It may be missed key in info.plist NSExtensionActivationSupportsWebPageWithMaxCount with nonzero value. – fir Feb 14 '17 at 21:35