How to download image from backendless in swift

Hello
I have implemented backendless image uploading and downloading function in my app.

Uploading function is working well.

But, downloading function don’t works.

So I have searched all.

I found that must be set permissions for downloading image using FileService in backendless.

But, I can’t set permission.

Please see this code snippet.

Please give me best solution.
Best regards!
Filip Tasev

Hi Filip
Not sure, I’m not iOS developer but according to example in our docs https://backendless.com/docs/ios/doc.html#file_permissions_api
you should use “grantForUser” method instead of “grant”

I am using swift 4.0 in my iOS app.

There are no such method now.

What problem do you get when you try downloading? Can you provide a sample URL for which you need to download the file?

Hello Filip,

The grant methods signatures have been changed in v5 and don’t return anything in response block.
Here is the example:

func grantAccess() {
        let userId = // your user's objectId here
        let url = // your url here
        
        Backendless.sharedInstance()?.file.permissions.grant(forUser: userId, url: url, operation: FILE_READ, response: {
            print("ACCESS GRANTED")
        }, error: { fault in
            print("ERROR: \(fault!)")
        })
    }

And you can use this async method to download image from Backendless:

func downloadImage(imageURL: String) {
    DispatchQueue.global().async {
        let data = NSData.init(contentsOf: NSURL.init(string: imageURL) as! URL)
        DispatchQueue.main.async {
            let image = UIImage.init(data: data as! Data)
            // ...
        }
    }
}

Regards,
Olga