In a macOS app (target: 12.3), I support dropping images from websites.
I would like to detect the domain of the website that the image was dropped from. I would show this domain in a confirmation dialog: "Are you sure you want to download this file from ?"
This origin is often different than the parsed URL.host
.
E.g. if I drag and drop an image from unsplash.com (https://unsplash.com/photos/<photo-id>
), the host is images.unsplash.com
.
Some other websites don't even use their own subdomain for static files so host
can be e.g. an AWS Cloudfront domain.
I checked the docs for NSItemProvider
, URL
and NSURL
, but haven't found a solution yet.
Do you have any idea, e.g. do you know if NSItemProvider
contains this info?
import SwiftUI
struct ContentView_Drop: View {
var body: some View {
Color.gray.onDrop(of: [.url], isTargeted: nil) { providers in
guard let provider = providers.first else { return false }
provider.loadObject(ofClass: URL.self) { url, _ in
if let url = url {
print("Dropped a url")
print("URL host: \(url.host)") // E.g. something.cloudfront.net
print("Drop origin: ???") // E.g. example.com
}
}
return true
}
}
}