Getting Image URLs in Swift 4.0

images
swift

#1

I’ve asked this question before but I’m still getting nowhere. I’m a novice so bear with me.

I have the following function:

private func loadProducts() {
let query = MoltinQuery(offset: nil, limit: 20, sort: nil, filter: nil, include: [.products, .files])
Moltin.product.list(withQuery: query) { (result) in
switch result {
case .failure(let error):
print(“Failed to get products:\n(error)”)
case .success(let productList):
print(“Got products (productList.products)”)
DispatchQueue.main.async {
self.productsCollectionView.reloadData()
}
}
}
}

This returns a whole load of JSON (some of it, not needed but I’m sure the Moltin team will clean this up!)

Got products [Moltin.Product(id: “fad1eb64-2248-4798-938b-60cbffd8bf95”, name: “Hat”, slug: “hat”, sku: “sku”, description: “Some hat”, commodityType: Moltin.Product.CommodityType.physical, dimensions: nil, weight: nil, files: [], collections: [], categories: [], brands: [], json: [“commodity_type”: physical, “name”: Hat, “sku”: sku, “manage_stock”: 1, “id”: fad1eb64-2248-4798-938b-60cbffd8bf95, “slug”: hat, “meta”: {
stock = {
availability = “in-stock”;
level = 5;
};
timestamps = {
“created_at” = “2018-02-21T18:57:15+00:00”;
“updated_at” = “2018-02-24T06:36:40+00:00”;
};
}, “status”: live, “description”: Some hat, “type”: product, “price”: <__NSSingleObjectArrayI 0x604000007f80>(
{
amount = 3499;
currency = USD;
“includes_tax” = 1;
}
)
, “relationships”: {
“main_image” = {
data = {
id = “e016d89b-13e3-40e4-a247-24c1d00a1411”;
type = “main_image”;
};
};
}], prices: [Moltin.Price(amount: 3499, currency: “USD”, includesTax: true, json: [“includes_tax”: 1, “currency”: USD, “amount”: 3499])], displayPriceWithTax: nil, displayPriceWithoutTax: nil)]

There is still no URL to pull the main image from?..

Please give me an EXAMPLE code of how to get these URLs. I was lead to believe I would have to request a query and include the product and files. However, this has not worked. Frustrated please help.


#2

Hi David,

I’m sorry to hear of your frustration. Unfortunately at the moment the iOS SDK does not support the main_image property being pulled directly into the response. I’ve logged an issue for this and will resolve it ASAP.

In the meantime, you can instead add your image to the Files section of the product in your dashboard. You’ll then be able to access something like productList.products[0].files which will retrieve all the files for your product.

If you don’t want to do this, you could also pull together all of the main_image ID’s, and call Moltin.file.get(withFileID:include:completion) for each, which will return the file information which contains the URL.

We’re making a big push to improve our mobile architecture, so I hope to resolve issues like this and make the SDK more reliable, well documented, and simpler to use.

Hope this helps David.
Thanks,
Craig


#3

Hi David,

I’ve just added in the ability for you to request main_image from the SDK. You can use something like this:


let query = MoltinQuery(offset: nil, limit: nil, sort: nil, filter: nil, include: [.files, .brands, .categories, .main_image])
        
        Moltin.product.list(withQuery: query) { result in
            switch result {
            case .failure(let error): break
            case .success(let list):
                print(list.products[0].main_image)
            }
}

I’ve added an example in the example project of the repo - ProductListViewController.swift#L44.

Hope this helps, and have a good day.

Thanks,
Craig


#4

This is fantastic Craig. Great work :tada:

I’m excited to see what @davidlintin builds using your new changes and the iOS SDK :ok_hand:


#5

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.