I start to create on-line store iOS app with connection to web site on Woocommerce. I need to insert in TableView merchandise from Woocommerce through API.
Now then I Construct the app, simulator launch solely white empty display screen. Within the closing outcome I need to launch iOS app with connection to web site on Woocommerce, however I create it step-by-step, and first step, now, I need to make display screen with product or classes from this store. Why does my iOS app present a clean display screen when I attempt to show WooCommerce merchandise in a TableView through the API?
Product.swift
struct Product: Codable {
let id: Int
let title: String
let worth: String
let regular_price: String?
let sale_price: String?
let description: String?
let short_description: String?
let pictures: [ProductImage]?
}
struct ProductImage: Codable {
let src: String
}
ViewController.swift
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
non-public let api = WooCommerceAPI()
non-public var merchandise: [Product] = []
override func viewDidLoad() {
tremendous.viewDidLoad()
fetchProducts()
}
func fetchProducts() {
let urlString = "https:///wp-json/wc/v3/merchandise"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { information, response, error in
// 1. Проверка ошибок
if let error = error {
print("Ошибка сети: (error)")
return
}
// 2. Обработка данных
guard let information = information else { return }
do {
// Здесь декодируйте JSON (например, в массив продуктов)
print("Данные получены: (information)")
} catch {
print("Ошибка декодирования: (error)")
}
}.resume()
}
}
WoocommerceAPI.swift
import Basis
class WooCommerceAPI {
non-public let baseURL = "https://wp-json/wc/v3"
non-public let consumerKey = "ck_1255d3fa39b33e661fc91e9fe17dd16fb4367e5"
non-public let consumerSecret = "cs_707ff6ee2e25206a2858c5f288f57dd18acf8358"
func fetchProducts(completion: @escaping ([Product]?) -> Void) {
let endpoint = "(baseURL)/merchandise"
let urlString = "(endpoint)?consumer_key=(consumerKey)&consumer_secret=(consumerSecret)"
guard let url = URL(string: urlString) else {
completion(nil)
return
}
URLSession.shared.dataTask(with: url) { information, response, error in
if let error = error {
print("Ошибка: (error.localizedDescription)")
completion(nil)
return
}
guard let information = information else {
completion(nil)
return
}
do {
let merchandise = strive JSONDecoder().decode([Product].self, from: information)
completion(merchandise)
} catch {
print("Ошибка декодирования: (error.localizedDescription)")
completion(nil)
}
}.resume()
}
}

