Skip to content

Sample News app (in Swift) using Contentstack SDK.

Notifications You must be signed in to change notification settings

raweng/NewsApp-iOS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Sample News app written in Swift showing use of Contentstack SDK.

Create Content Type - Category and News

In this news application, we will create 2 Content Types viz., Category and News. Download the JSON format of Category and News content type and import it in Contentstack.

Category JSON

News JSON.

To learn more about how to import content type, check out the guide.

Create Category Content Type

Create News Content Type

Clone repository

Open Terminal (for Mac and Linux users) or the command prompt (for Windows users) and paste the below command to clone the project.

$ git clone https://github.com/raweng/NewsApp-iOS.git

Configure project

Grab API Key and Access Token from Contentstack admin interface, Settings > General and Update the config parameters in SDK initialisation step:

let stack:Stack = Contentstack.stackWithAPIKey("API_KEY", accessToken: "ACCESS_TOKEN", environmentName: "ENVIRONMENT_NAME")

Usage

Query News Items

Home page shows list of top news that we have created in Contentstack. Let’s see how to query Contentstack.

var topNewsArticles = [Entry]();
var allNewsByCategoryQuery:Query = stack.contentTypeWithName("news").query()

//filter topnews
allNewsByCategoryQuery.whereKey("topnews", equalTo: NSNumber(bool: true))

allNewsByCategoryQuery.includeReferenceFieldWithKey(["category"])
allNewsByCategoryQuery.orderByAscending("updated_at")

allNewsByCategoryQuery.find { (responseType, result, error) -> Void in
    
    if(error != nil){
        let alertController:UIAlertController = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: "Opps! Some error occured while fetching data.", preferredStyle: UIAlertControllerStyle.Alert)
        
        let cancelAction = UIAlertAction(title: NSLocalizedString("Ok", comment: "Ok"), style: .Cancel) { (action) in
            self.dismissViewControllerAnimated(true, completion: nil)
        }
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true) {
            // ...
        }
    }else {
        
        for entry:Entry in (result.getResult() as! [(Entry)]){
            self.topNewsArticles.append(entry)
        }
    }
}

For more details about Query, refer Contentstack Query Guide

Filter By Category

// self.selectedCategoryUId is a variable containing selected category uid
allNewsByCategoryQuery.whereKey("category", equalTo: [self.selectedCategoryUId])

Filter By Language

//For English language
allNewsByCategoryQuery.language(Language.ENGLISH_UNITED_STATES)

//For Hindi language
//allNewsByCategoryQuery.language(Language.HINDI_INDIA)

About

Sample News app (in Swift) using Contentstack SDK.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published