3

I am following this tutorial and I'm trying to do it in Swift.

The file object does not have a downloadUrl property and I do not understand why. I checked the API and it says that this property exists, at least for Objective-C. But all other properties exist for Swift, so why not downloadUrl?

I do a simple thing like:

let file: GTLDriveFile!
file.downloadUrl

and downloadUrl get underlined in red with the error: Value of type 'GTLDriveFile' has no member 'downloadUrl'.

Here is my code:

private let service = GTLServiceDrive()

// Parse results and display
func displayResultWithTicket(ticket : GTLServiceTicket,
    finishedWithObject response : GTLDriveFileList,
    error : NSError?) {

        if let error = error {
            showAlert("Error", message: error.localizedDescription)
            return
        }

        var filesString = ""

        if let files = response.files where !files.isEmpty {
            filesString += "Files:\n"

            for file in files as! [GTLDriveFile] {
                var fetcher: GTMSessionFetcher = service.fetcherService.fetcherWithURL(file.downloadUrl)
                filesString += "\(file.name)\n"
            }
        } else {
            filesString = "No files found."
        }

        output.text = filesString
}
magohamote
  • 1,484
  • 1
  • 17
  • 29
  • Can you share more of your code? The `downloadUrl` property should exist as you say, which makes me think that you are getting a misleading error message and something else is actually causing the problem. – Aaron Rasmussen Feb 16 '16 at 17:14
  • I'm having the same issue. I opened GTLDriveFile.h, and that method does not exist – david72 Feb 25 '16 at 20:30

1 Answers1

1

What I ended up doing is generating the download URL myself like this:

let url = "https://www.googleapis.com/drive/v2/files/" + file.identifier + "?alt=media"
let fetcher = service.fetcherService.fetcherWithURLString(url)
fetcher.beginFetchWithCompletionHandler({ (data, error) -> Void in
    if error == nil {
        print(data)
    } else {
        print("An error occurred: " + error!.localizedDescription)
    }
})

Another option is to call without "?alt=media" and extract downloadUrl from the meta data json

david72
  • 7,151
  • 3
  • 37
  • 59