benhi
benhi

Reputation: 582

Get HTML from Safari share extension?

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?

Upvotes: 3

Views: 1402

Answers (1)

Azeem Akram
Azeem Akram

Reputation: 120

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

Upvotes: 4

Related Questions