Swift WordPress Posts Fetch Page

I’m a beginner for programming and swift. I’m developing an app for my small business. It’s almost finished but i’m completely stucked with blog page. I just want to fetch data from my website which is wordpress, and put my posts in to my tableview. I was searching but couldn’t find a proper answer. Please at least me tell me where to begin with

Thank you

Related posts

2 comments

  1. You need to use awesome new API of WordPress called WP REST API, which will give you all data you need in JSON format. Have a look at this url to get started:
    http://v2.wp-api.org/

    Use this official plugin in your WordPress site:
    https://wordpress.org/plugins/rest-api/

    Update: You do not need to install above plugin as it has been merged into WordPress core from v4.4 to onwards.

  2. At first, you should define models for parsing the JSON response, for example, as below.

    import Foundation
    
    struct Title: Decodable {
    
        let rendered: String
    
    }
    
    struct Content: Decodable {
    
        let rendered: String
    
    }
    
    struct WPFeaturedMedia: Decodable {
    
        let sourceURLString: String
    
        var url: URL? {
            .init(string: sourceURLString)
        }
    
        enum CodingKeys: String, CodingKey {
            case sourceURLString = "source_url"
        }
    
    }
    
    struct Embedded: Decodable {
    
        let medias: [WPFeaturedMedia]?
    
        enum CodingKeys: String, CodingKey {
            case medias = "wp:featuredmedia"
        }
    
    }
    
    struct PostHeader: Decodable, Identifiable {
    
        let id: Int
    
        let date: Date
        let title: Title
        let embedded: Embedded
    
        enum CodingKeys: String, CodingKey {
            case date = "date"
            case title = "title"
            case embedded = "_embedded"
            case id = "id"
        }
    
    }
    
    struct Post: Decodable, Identifiable {
    
        let id: Int
    
        let date: Date
        let title: Title
        let content: Content
        let embedded: Embedded
    
        enum CodingKeys: String, CodingKey {
            case date = "date"
            case title = "title"
            case content = "content"
            case embedded = "_embedded"
            case id = "id"
        }
    
    }
    
    

    You can easily fetch posts using Combine.

    import Combine
    import SwiftUI
    
    final class ViewModel: ObservableObject {
    
            @Published var postHeaders: [PostHeader] = []
            @Published var error: Error?
    
            private var cancellable: AnyCancellable?
    
            init() {
                let url = URL(string: "https://PUT_YOUR_HOST_HERE/wp-json/wp/v2/posts?_fields=id,date,title,_links,_embedded&_embed”)!
    
                cancellable = URLSession.shared.dataTaskPublisher(for: url)
                    .map { $0.data }
                    .decode(type: [PostHeader].self, decoder: JSONDecoder())
                    .receive(on: DispatchQueue.main)
                    .sink(
                        receiveCompletion: { completion in
                            switch completion {
                            case .failure(let error):
                                self.error = error
                            case .finished:
                                break
                            }
                        },
                        receiveValue: { posts in
                            self.postHeaders = posts
                        }
                    )
            }
    }
    
    

    Full sources you can find here: https://github.com/fuzzzlove/SwiftUIWordpressClient

Comments are closed.