UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to get the URL for an image on a web page?

Forums > iOS

I know how to download an image for a given URL on the web. But I want the user to be able to pick an image on a webpage and have that as a image in the app. Surely that must be possible?

So how do I get the URL for a selected images on a webpage? One way I think it should be possible is respond to a copy (paste) action, but I have no idea how to set that up. Or maybe it is possible using a WKWebView?

Please can someone nudge me in the right direction? I've been lost on this for too long ... 🥵

3      

You need to dive a bit into JavaScript. With WKWebView you can run custom script which could alert you to user tapping on a image. You could then send the URL to your Swift code, close the webview and show downloaded image.

I have written a bit about it here: https://dev.to/nemecek_f/dynamically-modify-page-in-wkwebview-in-ios-3pb https://dev.to/nemecek_f/wkwebview-communicate-from-web-to-app-with-javascript-a16

I would say I have some experience with this so feel free to ask :-)

3      

Thanks @nemecek-filip. I've solved it like so:

I present a Safari ViewController. When the user long presses on an image and then selects share > copy, an image is added to the Pasteboard. Sometimes it's not an Image, but an image URL. I handle both cases:

//MARK: - Ext. Delegate SFSafariViewControllerDelegate
extension ProjectImagePicker: SFSafariViewControllerDelegate {
    public func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
        //image was returned by Copy
        if pasteboard.hasImages {
            guard let image = pasteboard.image else { return }
            self.delegate?.didSelect(image: image)
        //Image Url was returned by Copy
        } else if pasteboard.hasURLs {
            guard let url = pasteboard.url else { return }

            if let data = try? Data(contentsOf: url) {
                if let image = UIImage(data: data) {
                    self.delegate?.didSelect(image: image)
                }
            }
        }
        pasteboard.items.removeAll()
        controller.dismiss(animated: true, completion: nil)
    }
}

Seems to work as intended! 👍

3      

Nice. I would say that is pretty interesting out of the box solution :-)

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.