1

Inside my app I have a CustomShareViewController and with that the workflow should be like this:

  1. User is in Safari on some website and looks up any kind of product

  2. User taps on "share" and selects my App

  3. I parse data from the current URL and use it/store it inside my app

Problem:

How do I get the data in Swift? I managed to get the current URL and with that the HTML like this:

@objc func actionButtonTapped(){

        var html: String?

        if let item = extensionContext?.inputItems.first as? NSExtensionItem,
            let itemProvider = item.attachments?.first,
            itemProvider.hasItemConformingToTypeIdentifier("public.url") {
            itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { (url, error) in
                if (url as? URL) != nil {

                    html = (self.getHTMLfromURL(url: url as? URL))

                    self.doStuff(html: html)
                }
            }
        }
}

But is there any way to get the data that Apple provides? (image and title) ?

That's the data from Apple I mean:

enter image description here

Chris
  • 1,828
  • 6
  • 40
  • 108
  • 1
    https://developer.apple.com/documentation/foundation/nsextensioncontext/1414827-inputitems – Paulw11 Apr 26 '20 at 22:55
  • @Paulw11 sorry this is the first time I am using a shareExtension. Could you maybe elaborate on this or explain how I can use it in my case? – Chris Apr 26 '20 at 22:58
  • Your view controller has an [extensionContext](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621411-extensioncontext) property. The `inputItems` of this extension context are the items that were shared – Paulw11 Apr 26 '20 at 23:01
  • @Paulw11 thanks for your answers but I am still struggling. How exactly would I retrieve the current URL or the data in the first picture? (product name and photo) – Chris Apr 26 '20 at 23:05
  • `inputItems` is an array of [`NSExtensionItem`](https://developer.apple.com/documentation/foundation/nsextensionitem) - You need to go through the array of items, look at their [attachments](https://developer.apple.com/documentation/foundation/nsitemprovider) to find the data you are after. I'm afraid there isn't a simple one line of "give me the URL" – Paulw11 Apr 26 '20 at 23:08
  • urgh, in my mind I thought this was going to be quite easy. Getting the current URL doesn't seem to be that complicated but I guess it is? Too bad.. Thanks for your help anyway. Unfortunately I am still not getting any further :/ Updated my question, maybe that helps someone – Chris Apr 26 '20 at 23:15
  • Updated the answer, found a way to get the URL! – Chris Apr 26 '20 at 23:23
  • @Paulw11 do you know how to get the `image` and `title` that apple already provides? (first picture) – Chris Apr 27 '20 at 18:47
  • 1
    @Chris I needed something similar days ago. Check out my question, the edit has example of `LPLinkMetadata` which could help you https://stackoverflow.com/questions/61291139/ios-sharing-via-uiactivityviewcontroller-does-not-work-with-uiactivityitemsourc – Filip Apr 27 '20 at 19:48
  • @Filip thanks man! With that information I found an awesome git-rep that solved my problem! – Chris Apr 29 '20 at 00:37
  • @Filip do you maybe know how to get the HTML Content after the Javascript is loaded?? https://stackoverflow.com/questions/61490069/get-html-content-after-it-is-loaded – Chris Apr 29 '20 at 12:01
  • @Chris I am afraid I dont have much experience with sharing extensions – Filip Apr 29 '20 at 18:48

1 Answers1

0

With @Filip's hint I found this Git-Repo:

URLEmbeddedView

With this I can simply get the data like this:

let urlString = ...
OpenGraphDataDownloader.shared.fetchOGData(urlString: urlString) { result in
    switch result {
    case let .success(data, isExpired):
        // do something
    case let .failure(error, isExpired):
        // do something
    }
}
Chris
  • 1,828
  • 6
  • 40
  • 108