daniel
daniel

Reputation: 966

How do I access the value of the ubiquitousItemDownloadingStatus resource value?

I have code to retrieve the ubiquitousItemDownloadingStatus resource value of a url:

    do {

        let resourceValues = try item.url.resourceValues(forKeys: [URLResourceKey.ubiquitousItemDownloadingStatusKey]

    } catch {

        print(error.localizedDescription)

    }

However, I don't know what to do with the value. I am able to access the resource value with the following code:

    do {

        let resourceValues = try item.url.resourceValues(forKeys: [URLResourceKey.ubiquitousItemDownloadingStatusKey])

        resourceValues.ubiquitousItemDownloadingStatus // ???

    } catch {

        print(error.localizedDescription)

    }

But I'm not sure what to do after that. The declaration of the resource value in the Foundation Library is a struct, as follows:

public struct URLUbiquitousItemDownloadingStatus : Hashable, Equatable, RawRepresentable {

    public init(rawValue: String)
}
extension URLUbiquitousItemDownloadingStatus {

    @available(iOS 7.0, *)
    public static let notDownloaded: URLUbiquitousItemDownloadingStatus

    @available(iOS 7.0, *)
    public static let downloaded: URLUbiquitousItemDownloadingStatus

    @available(iOS 7.0, *)
    public static let current: URLUbiquitousItemDownloadingStatus
}

I'm confused because I expect an enum.

Please if anyone can help clarify this for me I would appreciate it.

Upvotes: 2

Views: 392

Answers (2)

Darkwonder
Darkwonder

Reputation: 1305

Xcode 10.1. Swift 4.2.

let url = "yourURL" as! URL
    do {
        let attributes = try url.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey])
        if let status = attributes.allValues[.ubiquitousItemDownloadingStatusKey] as? URLUbiquitousItemDownloadingStatus {
            if status == URLUbiquitousItemDownloadingStatus.current {
                print("\nURLUbiquitousItemDownloadingStatus.current")

            } else if status == URLUbiquitousItemDownloadingStatus.downloaded {
                print("\nURLUbiquitousItemDownloadingStatus.downloaded")

            } else if status == URLUbiquitousItemDownloadingStatus.notDownloaded {
                print("\nURLUbiquitousItemDownloadingStatus.notDownloaded")

            }

        }
    } catch {

    }

Upvotes: 0

Doug Knowles
Doug Knowles

Reputation: 983

Don't get excited; it's not working for me, but here's what I've tried:

I've wrapped the access into a var...

var ubiquityDownloadStatus: URLUbiquitousItemDownloadingStatus? {
    get {
        do {
            let keyValues = try rootURL.resourceValues(forKeys: [.ubiquitousItemIsDownloadingKey])
            return keyValues.ubiquitousItemDownloadingStatus
        } catch {
            os_log("Error checking download status of %s", log: .default, type: .error, rootURL.lastPathComponent)
        }
        return nil
    }
}

...then use it thus:

private func statusImage(forNode node: DirectoryNode) -> NSImage? {
    if !node.isUbiquitousItem { return nil }
    let status = node.ubiquityDownloadStatus
    switch status {
    case URLUbiquitousItemDownloadingStatus.current:
        return NSImage(named: NSImage.statusAvailableName)
    case URLUbiquitousItemDownloadingStatus.downloaded:
        return NSImage(named: NSImage.statusPartiallyAvailableName)
    case URLUbiquitousItemDownloadingStatus.notDownloaded:
        return NSImage(named: NSImage.statusUnavailableName)
    default:
        return NSImage(named: NSImage.statusNoneName)
    }
}

...but I'm always getting back nil as a result.

I've tried adding iCloud entitlements to my app with no effect. Next, I'm looking into this alternate solution, which may better suit my needs anyway.

Cocoa - How to detect change in ubiquity container

Upvotes: 2

Related Questions