Raisen
Raisen

Reputation: 4495

Getting an error when implementing drop support for my Mac application

I'm trying to get the URL of a web page when it's dragged into my application view. I'm using the code below as the DropDelegate.

    func performDrop(info: DropInfo) -> Bool {
        guard let itemProvider = info.itemProviders(for: ["public.url"]).first else {
            return false
        }
        guard itemProvider.canLoadObject(ofClass: URL.self) else {
            return false
        }
        _ = itemProvider.loadObject(ofClass: URL.self) {url, _ in
            if let url = url {
                DispatchQueue.main.async {
                    do {
                        try self.store.urlToOpen = url.absoluteString
                    } catch {
                        
                    }
                }
            }
        }
        return true
    }

The error I'm getting is:

Could not instantiate class NSURL. Error: Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive

I'm not sure what's going on and how to fix this. Any ideas?

Upvotes: 2

Views: 147

Answers (1)

Asperi
Asperi

Reputation: 257789

Here is working solution. Tested with Xcode 12 / macOS 10.15.5

_ = itemProvider.loadDataRepresentation(forTypeIdentifier: "public.url") {url, _ in
    if let data = data, 
       let path = String(bytes: data, encoding: .utf8), 
       let url = URL(string: path) {
        DispatchQueue.main.async {
            do {
                try self.store.urlToOpen = url.absoluteString
            } catch {
                
            }
        }
    }
}

Upvotes: 1

Related Questions