2

I am trying to use an action extension in my iOS app to allow users to save a shopping item into a UITableView. I would like the share extension to work like this:

  1. The user opens a webpage in Safari (eg. this page on Best Buy)
  2. The user taps the Share button and the Share Sheet appears.
  3. The user can tap on my app icon and the UIViewController and the user should be able to tap done and the webpage details (product name and price) should get saved to my app.

However, I'm not sure how I can get some of the webpage content so I can get the item name and price.

I tried to use Apple's documentation on share extensions, however, it is written in Objective-C, not Swift.

I'm able to grab the webpage URL using the code below in the didSelectPost() method:

if let item = extensionContext?.inputItems.first as? NSExtensionItem {
    if let itemProvider = item.attachments?.first as? NSItemProvider {
        if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
            itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in
                if let shareURL = url as? NSURL {
                    print("Here is the URL: \(shareURL)")
                }
                self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
            })
        }
    }
}

How can I get the item name and item price from a website along with the URL when the user taps my app in the Safari share sheet?

michaelderiso
  • 145
  • 1
  • 1
  • 12

0 Answers0