0

I begin to create online shop iOS app with connection to site on Woocommerce. I want to insert in TableView products from Woocommerce via API.

Now then I Build the app, simulator launch only white empty screen. In the final result I want to launch iOS app with connection to site on Woocommerce, but I create it step by step, and first step, now, I want to make screen with product or categories from this shop. Why does my iOS app show a blank screen when I try to display WooCommerce products in a TableView via the API?

Product.swift

struct Product: Codable {
    let id: Int
    let name: String
    let price: String
    let regular_price: String?
    let sale_price: String?
    let description: String?
    let short_description: String?
    let images: [ProductImage]?
}

struct ProductImage: Codable {
    let src: String
}

ViewController.swift

ViewController.swift

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    private let api = WooCommerceAPI()
    private var products: [Product] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        fetchProducts()
    }
    
    func fetchProducts() {
        let urlString = "https:///wp-json/wc/v3/products"
        guard let url = URL(string: urlString) else { return }
        
        URLSession.shared.dataTask(with: url) { data, response, error in
            // 1. Проверка ошибок
            if let error = error {
                print("Ошибка сети: \(error)")
                return
            }
            
            // 2. Обработка данных
            guard let data = data else { return }
            do {
                // Здесь декодируйте JSON (например, в массив продуктов)
                print("Данные получены: \(data)")
            } catch {
                print("Ошибка декодирования: \(error)")
            }
        }.resume()
    }
}

WoocommerceAPI.swift

import Foundation


class WooCommerceAPI {
    private let baseURL = "https://wp-json/wc/v3"
    private let consumerKey = "ck_1255d3fa39b33e661fc91e9fe17dd16fb4367e5"
    private let consumerSecret = "cs_707ff6ee2e25206a2858c5f288f57dd18acf8358"
    
    func fetchProducts(completion: @escaping ([Product]?) -> Void) {
        let endpoint = "\(baseURL)/products"
        let urlString = "\(endpoint)?consumer_key=\(consumerKey)&consumer_secret=\(consumerSecret)"
        
        guard let url = URL(string: urlString) else {
            completion(nil)
            return
        }
        
        URLSession.shared.dataTask(with: url) { data, response, error in
            if let error = error {
                print("Ошибка: \(error.localizedDescription)")
                completion(nil)
                return
            }
            
            guard let data = data else {
                completion(nil)
                return
            }
            
            do {
                let products = try JSONDecoder().decode([Product].self, from: data)
                completion(products)
            } catch {
                print("Ошибка декодирования: \(error.localizedDescription)")
                completion(nil)
            }
        }.resume()
    }
}
New contributor
dima ulyanoff is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1 - I'd recommend not to put your key & secret on here, 2 - does the woocommerce server respond with a list of products when you simulate the same request via a HTTP client like curl / postman? Commented yesterday

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.