|
1 |
| -# Contentstack SDK for iOS |
2 |
| -Built.io Contentstack is a headless CMS with an API-first approach that puts content at the centre. It is designed to simplify the process of publication by separating code from content. For more information see the [website](https://contentstackdocs.built.io/developer). |
3 |
| - |
4 |
| -#### Installation Options |
5 |
| -##### Manual |
6 |
| - |
7 |
| -1. Download the [Latest iOS SDK release](https://github.com/raweng/BuiltIOContentstack-iOS/releases) and extract the zip file to your local disk. |
8 |
| - |
9 |
| -2. Drag and drop Contentstack.framework into your project folder in Xcode. |
10 |
| - |
11 |
| - A window will appear, prompting you to choose one of the options for adding files. Click the ‘Destination’ checkbox to copy items into the destination group’s folder. This will add the SDK to your project. |
12 |
| - |
13 |
| -3. In the project editor, select your app under `TARGETS`. Under the `General` tab, open `Linked Frameworks and Libraries` and add the following libraries: |
14 |
| - - CoreGraphics.framework |
15 |
| - - MobileCoreServices.framework |
16 |
| - - Security.framework |
17 |
| - - SystemConfiguration.framework |
18 |
| - |
19 |
| -4. In your target app, click on the `Build Settings` tab and add the `-ObjC` flag to `Other Linker Flags`. |
20 |
| - |
21 |
| -##### **[CocoaPods](https://cocoapods.org)** |
22 |
| - |
23 |
| -Add the following line to your Podfile: |
24 |
| -```sh |
25 |
| -pod 'Contentstack' |
26 |
| -``` |
27 |
| -Run `pod install`, and you should now have the latest Contentstack release. |
28 |
| - |
29 |
| -#### Import Header/Module |
30 |
| -You can import header file in Objective-C project as: |
31 |
| -```sh |
32 |
| -#import <Contentstack/Contentstack.h> |
33 |
| -``` |
34 |
| -You can also import as a Module: |
35 |
| - |
36 |
| -```sh |
37 |
| -//Objc |
38 |
| -@import Contentstack |
39 |
| - |
40 |
| -//Swift |
41 |
| -import Contentstack |
42 |
| -``` |
43 |
| -#### Other Links |
44 |
| -- [QuickStart](https://contentstackdocs.built.io/developer/ios/quickstart) |
45 |
| -- [Developer Guide](https://contentstackdocs.built.io/developer/guides) |
46 |
| -- [API Docs](https://contentstackdocs.built.io/ios/api) |
47 |
| - |
48 |
| -## License |
49 |
| -``` |
50 |
| -Copyright (c) 2017, Contentstack.io. |
51 |
| -All rights reserved. |
52 |
| -``` |
| 1 | +[](https://www.contentstack.com/) |
| 2 | + |
| 3 | +## iOS SDK for Contentstack |
| 4 | + |
| 5 | +Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. [Read More](https://www.contentstack.com/). |
| 6 | + |
| 7 | +Contentstack provides iOS SDK to build application on top of iOS. Given below is the detailed guide and helpful resources to get started with our iOS SDK. |
| 8 | + |
| 9 | + |
| 10 | +### Prerequisite |
| 11 | + |
| 12 | + |
| 13 | +Latest Xcode and Mac OS X |
| 14 | + |
| 15 | + |
| 16 | +### Setup and Installation |
| 17 | + |
| 18 | + |
| 19 | +To use this SDK on iOS platform, you will have to install the SDK according to the steps given below. |
| 20 | + |
| 21 | + |
| 22 | +##### Manual |
| 23 | + |
| 24 | + |
| 25 | +1. Download the [Latest iOS SDK release](https://github.com/raweng/BuiltIOContentstack-iOS/releases) and extract the zip file to your local disk. |
| 26 | +2. Drag and drop Contentstack.framework into your project folder in Xcode. A window will appear, prompting you to choose one of the options for adding files. Click the ‘Destination’ checkbox to copy items into the destination group’s folder. This will add the SDK to your project. |
| 27 | +3. In the project editor, select your app under TARGETS. Under the General tab, open Linked Frameworks and Libraries and add the following libraries: |
| 28 | +- CoreGraphics.framework |
| 29 | +- MobileCoreServices.framework |
| 30 | +- Security.framework |
| 31 | +- SystemConfiguration.framework |
| 32 | + |
| 33 | +4. In your target app, click on the Build Settings tab and add the -ObjC flag to Other Linker Flags. |
| 34 | + |
| 35 | +##### CocoaPods |
| 36 | + |
| 37 | +1. Add the following line to your Podfile: |
| 38 | +2. pod 'Contentstack' |
| 39 | +3. Run pod install, and you should now have the latest Contentstack release. |
| 40 | + |
| 41 | +##### Import Header/Module |
| 42 | + |
| 43 | +You can import header file in Objective-C project as: |
| 44 | +```sh |
| 45 | +#import <Contentstack/Contentstack.h> |
| 46 | + |
| 47 | +You can also import as a Module: |
| 48 | + |
| 49 | +//Objc |
| 50 | + |
| 51 | +@import Contentstack |
| 52 | + |
| 53 | +//Swift |
| 54 | + |
| 55 | +import Contentstack |
| 56 | +``` |
| 57 | +### Key Concepts for using Contentstack |
| 58 | + |
| 59 | +#### Stack |
| 60 | + |
| 61 | +A stack is like a container that holds the content of your app. Learn more about [Stacks](https://www.contentstack.com/docs/guide/stack). |
| 62 | + |
| 63 | +#### Content Type |
| 64 | + |
| 65 | +Content type lets you define the structure or blueprint of a page or a section of your digital property. It is a form-like page that gives Content Managers an interface to input and upload content. [Read more](https://www.contentstack.com/docs/guide/content-types). |
| 66 | + |
| 67 | +#### Entry |
| 68 | + |
| 69 | +An entry is the actual piece of content created using one of the defined content types. Learn more about [Entries](https://www.contentstack.com/docs/guide/content-management#working-with-entries). |
| 70 | + |
| 71 | + |
| 72 | +#### Asset |
| 73 | + |
| 74 | + |
| 75 | +Assets refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded to Contentstack. These files can be used in multiple entries. Read more about [Assets](https://www.contentstack.com/docs/guide/content-management#working-with-assets). |
| 76 | + |
| 77 | + |
| 78 | +#### Environment |
| 79 | + |
| 80 | +A publishing environment corresponds to one or more deployment servers or a content delivery destination where the entries need to be published. Learn how to work with [Environments](https://www.contentstack.com/docs/guide/environments). |
| 81 | + |
| 82 | +### Contentstack iOS SDK: 5-minute Quickstart |
| 83 | + |
| 84 | +#### Initializing your SDK |
| 85 | + |
| 86 | +To start using the SDK in your application, you will need to initialize the stack by providing the required keys and values associated with them: |
| 87 | +```sh |
| 88 | +//Objc |
| 89 | + |
| 90 | +Stack *stack = [Contentstack stackWithAPIKey: API_KEY accessToken: ACCESS_TOKEN environmentName: ENVIRONMENT]; |
| 91 | +//Swift |
| 92 | + |
| 93 | +let stack:Stack = Contentstack.stackWithAPIKey(API_KEY, accessToken: ACCESS_TOKEN, environmentName: ENVIRONMENT) |
| 94 | +``` |
| 95 | +To get the api credentials mentioned above, you need to log into your Contentstack account and then in your top panel navigation, go to Settings -> Stack to view both your API Key and your Access Token |
| 96 | + |
| 97 | +The stack object that is returned is a Contentstack client object, which can be used to initialize different modules and make queries against our [Content Delivery API](https://contentstack.com/docs/apis/content-delivery-api/). The initialization process for each module is explained in the following section. |
| 98 | + |
| 99 | + |
| 100 | +#### Querying content from your stack |
| 101 | + |
| 102 | +To fetch all entries of of a content type, use the query given below: |
| 103 | +```sh |
| 104 | +//Obj-C |
| 105 | + |
| 106 | +ContentType *contentTypeObject = [stack contentTypeWithName:@"my_content_type"]; |
| 107 | + |
| 108 | +Query *queryObject = [contentTypeObj query]; |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +//Swift |
| 113 | + |
| 114 | +var contentTypeObject:ContentType = stack.contentTypeWithName("my_content_type") |
| 115 | + |
| 116 | +var queryObject:Query = contentTypeObj.query() |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +To fetch a specific entry from a content type, use the following query: |
| 121 | +```sh |
| 122 | +//Obj-C |
| 123 | + |
| 124 | +ContentType * contentTypeObject = [stack contentTypeWithName:@"my_content_type"]; |
| 125 | + |
| 126 | +Entry *entryObject = [contentTypeObject entryWithUID:@"ENTRY_UID"]; |
| 127 | + |
| 128 | +//Swift |
| 129 | + |
| 130 | +var contentTypeObject:ContentType = stack.contentTypeWithName("my_content_type") |
| 131 | + |
| 132 | +var entryObject:Entry = contentTypeObject.entryWithUID("ENTRY_UID") |
| 133 | +``` |
| 134 | +### Advanced Queries |
| 135 | + |
| 136 | +You can query for content types, entries, assets and more using our iOS API Reference. |
| 137 | + |
| 138 | +[iOS API Reference Doc](https://www.contentstack.com/docs/platforms/ios/api-reference/) |
| 139 | + |
| 140 | +### Working with Images |
| 141 | + |
| 142 | +We have introduced Image Delivery APIs that let you retrieve images and then manipulate and optimize them for your digital properties. It lets you perform a host of other actions such as crop, trim, resize, rotate, overlay, and so on. |
| 143 | + |
| 144 | +For example, if you want to crop an image (with width as 300 and height as 400), you simply need to append query parameters at the end of the image URL, such as, https://images.contentstack.io/v3/assets/blteae40eb499811073/bltc5064f36b5855343/59e0c41ac0eddd140d5a8e3e/download?crop=300,400. There are several more parameters that you can use for your images. |
| 145 | + |
| 146 | +[Read Image Delivery API documentation](https://www.contentstack.com/docs/apis/image-delivery-api/). |
| 147 | + |
| 148 | +You can use the Image Delivery API functions in this SDK as well. Here are a few examples of its usage in the SDK. |
| 149 | + |
| 150 | +```sh |
| 151 | +//Obj-C |
| 152 | +/* set the image quality to 100 */ |
| 153 | +NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:100], @"quality", nil]; |
| 154 | +NSString *transformedUrl = [stack imageTransformWithUrl:imageURL andParams:params]; |
| 155 | + |
| 156 | +/* resize the image by specifying width and height */ |
| 157 | +NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:100], @"width", [NSNumber numberWithInt:100], @"height", nil]; |
| 158 | +NSString *transformedUrl = [stack imageTransformWithUrl:imageURL andParams:params]; |
| 159 | + |
| 160 | +/* enable auto optimization for the image */ |
| 161 | +NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:@"webp", @"auto", nil]; |
| 162 | +NSString *transformedUrl = [stack imageTransformWithUrl:imageURL andParams:params]; |
| 163 | + |
| 164 | + |
| 165 | +//Swift |
| 166 | +/* set the image quality to 100 */ |
| 167 | +let params:[String : AnyObject?] = [ |
| 168 | +"quality":100 as AnyObject |
| 169 | +]; |
| 170 | +let transformedUrl:String = stack.imageTransformation(withUrl: imageURL, andParams: params); |
| 171 | + |
| 172 | +/* resize the image by specifying width and height */ |
| 173 | +let params:[String : AnyObject?] = [ |
| 174 | +"width":100 as AnyObject, |
| 175 | +"height":100 as AnyObject, |
| 176 | +]; |
| 177 | +let transformedUrl:String = stack.imageTransformation(withUrl: imageURL, andParams: params); |
| 178 | + |
| 179 | +let params:[String : AnyObject?] = [ |
| 180 | +"auto":"webp" as AnyObject |
| 181 | +]; |
| 182 | +let transformedUrl:String = stack.imageTransformation(withUrl: imageURL, andParams: params); |
| 183 | + |
| 184 | +``` |
| 185 | + |
| 186 | +### Helpful Links |
| 187 | + |
| 188 | +- [Contentstack Website](https://www.contentstack.com) |
| 189 | +- [Official Documentation](http://contentstack.com/docs) |
| 190 | +- [Content Delivery API Docs](https://contentstack.com/docs/apis/content-delivery-api/) |
| 191 | + |
| 192 | +## License |
| 193 | +``` |
| 194 | +Copyright (c) 2017, Contentstack.io. |
| 195 | +All rights reserved. |
| 196 | +``` |
| 197 | + |
| 198 | + |
0 commit comments