DrupalRESTKit is an iOS 7.x library built on the top of AFNetworking. It simplifies developer tasks to consume REST services provided by Drupal 8. Currently it simplifies CRUD operations on 'node' and 'user' entity type. It configures request parameter as per requirements for example while POST request it will set content-type to "application/hal+json". It let you focus on your application rather than managing complex parameters and configuration for REST requests. Currently we are working class realation between drupal entities it will make it more joyful.
#Installation with CocoaPods
platform :ios, '7.0'
pod 'DrupalRESTKit', :git => 'https://github.com/vivekvpandya/DrupalRESTKit.git'
#Requirements iOS 7 or above
#Usage
Import Drupal8RESTSessionManager.h
and you are good to go. Here in my example I am using http basic auth authentication type when required.
Create instance of manager , set accept header.
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager setValue:@"application/hal+json" forHTTPRequestHeader:@"Accept"];
[manager GETNode:self.baseURL
nodeId:@"57"
parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
}
Set Authorization if any required. Build parameters and POST a node. Here if you are posting an article pass bundle type as @"article"
and pass parameters accordingly.
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager.sessionManager.requestSerializer setValue:@"your base 64 basic auth string " forHTTPHeaderField:@"Authorization"];
NSDictionary *parameters= @{
@"uid":@[@{@"target_id":@"1"} ],
@"field_tag":@[@{@"target_id":@"1"}],
@"body":@[@{@"value":@"This is text",
@"format":@"full_html"}],
@"title":@[@{@"value":@"Tip Via Drupal 8 iOS sdk"}]
};
[manager POSTNode:self.baseURL
bundleType:@"tip"
parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"OK POSTED");
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
Same as POST on node.
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager.sessionManager.requestSerializer setValue:@"your base 64 basic auth string" forHTTPHeaderField:@"Authorization"];
NSDictionary *parameters= @{@"uid":@[@{@"target_id":@"1"} ],
@"field_tag":@[@{@"target_id":@"2"}],
@"body":@[@{@"value":@" Hey I have updated this node via drupal 8 ios kit ",
@"format":@"full_html"}],
@"title":@[@{@"value":@"Tip Via Drupal 8 iOS sdk"}]
};
[manager PATCHNode:self.baseURL
bundleType:@"tip"
nodeId:@"61"
parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"UPDATED !");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager setValue:@"your base 64 basic auth string" forHTTPRequestHeader:@"Authorization"];
[manager DELETENode:self.baseURL
nodeId:@"55"
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"ok deleted");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
Here viewName is path for your REST Export.
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager GETView:self.baseURL
viewName:@"vocabulary/foss"
parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
Here targetEntityId is an entity id on which comment will be posted.
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager setValue:@"your base 64 basic auth string" forHTTPRequestHeader:@"Authorization"];
NSDictionary *parameters = @{
@"subject":@[
@{
@"value":@"A comment with Drupal 8 iOS sdk !"
}
],
@"comment_body":@[
@{
@"value":@"<p>Drupal 8 will rock !.</p>\r\n",
@"format":@"basic_html"
}
]
};
[manager POSTComment:self.baseURL
targetEntityId:@"16"
parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"OK Comment POSTED !");
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
To get all comments for particular node create REST export for a view. Set entity nid as contextual filter parameter. Now use GETView
method. Tutorial on Contextual Filter with view
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager.sessionManager.requestSerializer setValue:@"your base 64 basic auth string" forHTTPHeaderField:@"Authorization"];
NSDictionary *parameters = @{
@"subject":@[
@{
@"value":@"A comment update with Drupal 8 iOS sdk !"
}
],
@"comment_body":@[
@{
@"value":@"<p>Drupal 8 will rock !. Drupal 8 SDK will also rock.</p>\r\n",
@"format":@"basic_html"
}
]
};
[manager PATCHCommment:self.baseURL
commentId:@"6"
parameters:parameters
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"OK Comment updated !");
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
Drupal8RESTSessionManager *manager = [[Drupal8RESTSessionManager alloc]init];
[manager.sessionManager.requestSerializer setValue:@"your base 64 basic auth string" forHTTPHeaderField:@"Authorization"];
[manager DELETEComment:self.baseURL
commentId:@"12"
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"OK Comment deleted !");
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error.description);
}];
similarly CRUD for user entity type is also available.
#Credits This SDK has been created as a part of a GSoC 2014 project called Example iOS app for Drupal 8 by Vivek Pandya and Jeff Linwood as mentor.
#Maintainers Vivek Pandya @VivekvPandya
#License DrupalRESTKit is available under MIT license. See LICENSE file for more info.