Sample News app written in Swift showing use of Contentstack SDK.
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.
To learn more about how to import content type, check out the guide.
Create Category Content Type
Create News Content Type
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
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")
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
// self.selectedCategoryUId is a variable containing selected category uid
allNewsByCategoryQuery.whereKey("category", equalTo: [self.selectedCategoryUId])
//For English language
allNewsByCategoryQuery.language(Language.ENGLISH_UNITED_STATES)
//For Hindi language
//allNewsByCategoryQuery.language(Language.HINDI_INDIA)