Contents |
Sources
Project homepage on GIT — https://github.com/QuickBlox/quickblox-ios-sdk/tree/master/sample-content
Download ZIP - https://github.com/QuickBlox/quickblox-ios-sdk/archive/master.zip
Overview
This sample demonstrates how to work with the QuickBlox Content API.
It allows you to organize a user's photo gallery.
It shows how to:
- Download a user's images
- Upload a new image
Guide: Getting Started with Content API
Getting a QuickBlox account
https://admin.quickblox.com/register
Creating applications on the Admin panel
https://admin.quickblox.com/apps/new
For further reading there is this 5 minute guide.
Connecting QuickBlox to your application
To get the information on how to connect to the QuickBlox.framework, please, refer to the IOS-how-to-connect-Quickblox-framework page.
Adding a Content Management System (CMS) to your application
QuickBlox Content API is:
- Content Management System (CMS) - Allows you to manage the apps contents and settings without having to re-publish it. Using a web interface you can control and make instant changes to the apps.
- Unlimited Data Storage
- Client SDKs that provide access to your content from code
Content Management System (CMS)
CMS allows you to manage the contents and settings of an app without having to re-publish it. You can upload any files through web interface, edit, delete - all typical operations.
Go to Admin panel, Content module. You can see list of your files:
You may not have any files. To upload a new file just use the File Upload section below the table - select a file to upload using the Choose... button, then press the Upload button - the new file will be uploaded.
You can create rich HTML files using the wysiwyg redactor - just press the Add Text/HTML button - a 'New file' page will be opened. You can use the wysiwyg redactor to create rich content.
Next, let's show you how to work with Content API through the iOS SDK:
Upload files
Note: The maximum size of the uploaded file is 100 mb
To upload a file using the iOS SDK use code below:
// your file - this is an image in our case NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"arrow.png"]); [QBRequest TUploadFile:imageData fileName:@"arrow.png" contentType:@"image/png" isPublic:NO successBlock:^(QBResponse *response, QBCBlob *blob) { // File uploaded, do something // if blob.isPublic == YES NSString *url = [blob publicUrl]; } statusBlock:^(QBRequest *request, QBRequestStatus *status) { // handle progress } errorBlock:^(QBResponse *response) { NSLog(@"error: %@", response.error); }];
Read more about isPublic: argument here.
It is also possible to update file's data without creating new file:
NSData *file = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"plus" ofType:@"png"]]; QBCBlob *blob = [QBCBlob blob]; blob.ID = 65268; // ID of exists file blob.name = @"Plus"; [QBRequest TUpdateFileWithData:file file:blob successBlock:^(QBResponse *response) { // File updated // if blob.isPublic == YES NSString *url = [blob publicUrl]; } statusBlock:^(QBRequest *request, QBRequestStatus *status) { // handle progress } errorBlock:^(QBResponse *response) { NSLog(@"error: %@", response.error); }];
Retrieve files
You can use page parameters such as:
- perPage - how many files will be displayed on each page (max 100)
- page - current returned page
To retrieve a list of your own files use code below:
QBGeneralResponsePage *page = [QBGeneralResponsePage responsePageWithCurrentPage:1 perPage:20]; [QBRequest blobsForPage:page successBlock:^(QBResponse *response, QBGeneralResponsePage *page, NSArray *blobs) { // process response } errorBlock:^(QBResponse *response) { // handle errors NSLog("errors=%@", response.error); }];
Download files
You can download a file by ID or UID.
Download file by ID
NSUInteger fileId = 476532; [QBRequest downloadFileWithID:fileId successBlock:^(QBResponse *response, NSData *fileData) { // do something with fileData } statusBlock:^(QBRequest *request, QBRequestStatus *status) { NSLog(@"download progress: %f", status.percentOfCompletion); } errorBlock:^(QBResponse *response) { NSLog(@"error: %@", response.error); }];
Download file by UID
NSString *uid = @"d816966db53640e68b304a3cd4e5c0c100"; [QBRequest downloadFileWithUID:uid successBlock:^(QBResponse *response, NSData *fileData) { // do something with fileData } statusBlock:^(QBRequest *request, QBRequestStatus *status) { NSLog(@"download progress: %f", status.percentOfCompletion); } errorBlock:^(QBResponse *response) { NSLog(@"Response error: %@", response.error); }];
Public/Private urls
There are 2 types of files in Content module:
- Private files - can be accessed only by QuickBlox user with session token
- Public files - can be accessed from anywhere in Internet, don't need to have a session token.
You can manage the type when you upload a file to Content module via isPublic: argument.
To access a private url use the following method:
QBCBlob *file = ...; NSString *privateUrl = [file privateUrl]; // or if you have only file UID NSString *fileUID = @"6221dd49a1bb46cfb61efe62c4526bd800"; NSString *privateUrl = [QBCBlob privateUrlForFileUID:fileUID];
To access a public url use the following method:
QBCBlob *file = ...; NSString *publicUrl = [file publicUrl]; // or if you have only file UID NSString *fileUID = @"6221dd49a1bb46cfb61efe62c4526bd800"; NSString *publicUrl = [QBCBlob publicUrlForFileUID:fileUID];