Tuesday, September 1, 2015

Objective C BOX API updating File/Folder tags

Objective C BOX API doesn't directly give you an option to update file/folder tags. Where as it gives option to update name and description of the items which are very important compared to tags. Through out this article If I say BOX item, that means whether a BOX file or folder.

In this article I am going to explain, how we can edit BOX API classes to achieve this functionality like updating tags of a file or folder. Before going to that, let's have a look at how we can edit the name of a BOX item (file/folder). Below is the sample code I have used in my application to rename a BOX item.

File Rename:

 BoxFilesRequestBuilder *fileRequestBuilder = [[BoxFilesRequestBuilder allocinitWithQueryStringParameters:@{@"fields" : @"name,type,description,tags,id,size,created_at,created_by,parent"}];

 fileRequestBuilder.name = fileName;    
    [[BoxSDK sharedSDK].filesManager editFileWithID:fileID requestBuilder:fileRequestBuilder success:^(BoxFile *file) {
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {

Folder Rename:

    BoxFoldersRequestBuilder *folderBuilder = [[BoxFoldersRequestBuilder alloc] initWithQueryStringParameters:@{ @"fields" : @"name,type,description,tags,id,size,created_at,created_by,parent"}];
    folderBuilder.name = folderName;
    [[BoxSDK sharedSDK].foldersManager editFolderWithID:folderID requestBuilder:folderBuilder success:^(BoxFolder *folder) {
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {


We use BoxFilesRequestBuilder to request updates on BOX files and BoxFoldersRequestBuilder to request updates on BOX folder items. We give this request to filesManger of BoxSDK for files and foldersManager of BoxSDK for folders to get the update done BOX items.

If you look at the above code for name update, there is a property called 'name' in the BoxFoldersRequestBuilder & BoxFilesRequestBuilder. But there is no property called 'tags'. So, all we need to do here is add property called 'tags' to these classes.

  • Add the following line in BoxItemsRequestBuilder.h file, which is the base class for both files and folders request builder classes.

                   @property (nonatomic, readwrite, strongNSArray *tags;

  • And the below three steps of code in the BoxItemsRequestBuilder.m file.

1. Before init method:

@synthesize tags = _tags;      

2. In initWithQueryStringParameters method:

_tags = nil;   

3. bodyParameters method:        

[self setObjectIfNotNil:self.tags forKey: @"tags" inDictionary:dictionary];  

Once you place this code, we will get options to update tags for BOX items as shown below.

fileRequestBuilder.tags = tagsArr;    
folderBuilder.tags = tagsArr;

Hope this article is useful to iOS developers using BOX API.

Feel free to comment in case of any queries.concerns.