I was toying around with AWS (Amazon Web Services) and SimpleDB on iOS a couple weeks ago and I struggled finding a very simple example of how to go about it. There is API documentation and several abstracted libraries out there but I wanted something really really basic. I was surprised to not find anything at all. I eventually figured it out so I thought I'd put this out there for anyone who might want to get a quick start.
So here goes. This is not the best way to code but it is the simplest. Don't forget to replace the access key and secret key below. Code has been tested in xcode5 and ios7 only.
//
// ViewController.m
// AWSTest
//
// make sure AWSRuntime.framework and AWSSimpleDB.framework have been added to the project
#import "ViewController.h"
#import <AWSSimpleDB/AWSSimpleDB.h>
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// set your domain name
NSString *domainName = [[NSString alloc]initWithFormat:@"TestStore"];
// A. create a simpledb client - everything happens through this client
AmazonSimpleDBClient *sdbClient = [[AmazonSimpleDBClient alloc] initWithAccessKey:@"AAAAAAAAAAAAAAAAAAAA" withSecretKey:@"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"];
// A.1. get a list of domaoins and check if we have the ones we need
SimpleDBListDomainsRequest *request = [[SimpleDBListDomainsRequest alloc]init];
SimpleDBListDomainsResponse *response = [[SimpleDBListDomainsResponse alloc]init];
response = [sdbClient listDomains:request];
if (response.exception==nil) {
NSLog(@"client object obtained");
} else {
NSLog(@"error creating client object: %@", response.exception);
}
BOOL domainExists;
for (NSString *name in response.domainNames) {
if ([name isEqualToString:domainName]) {
domainExists=YES;
}
}
// A.2. if not then create the domain we need
if (domainExists==NO) {
SimpleDBCreateDomainRequest *createRequest = [[SimpleDBCreateDomainRequest alloc]initWithDomainName:domainName];
SimpleDBCreateDomainResponse *createReponse = [sdbClient createDomain:createRequest];
if (createReponse.exception==nil) {
NSLog(@"domain created by name: %@", domainName);
} else {
NSLog(@"error creating domain %@", createReponse.exception);
}
} else {
NSLog(@"domains exist: %@", response.domainNames);
}
// B. now lets add attributes...
// B.1. create two simple attributes, the last parameter is to replace the value of the attribute if it already exists
SimpleDBReplaceableAttribute *attrib1 = [[SimpleDBReplaceableAttribute alloc]initWithName:@"name" andValue:@"John Doe" andReplace:YES];
SimpleDBReplaceableAttribute *attrib2 = [[SimpleDBReplaceableAttribute alloc]initWithName:@"neighborhood" andValue:@"Midtown South" andReplace:YES];
// B.2. add the attributes to an array
NSMutableArray *attribList = [[NSMutableArray alloc] initWithObjects:attrib1, attrib2, nil];
// B.3. create a request object
SimpleDBPutAttributesRequest *putRequest = [[SimpleDBPutAttributesRequest alloc] initWithDomainName:domainName andItemName:@"1001" andAttributes:attribList];
// B.4. call put attributes with response object created earlier
SimpleDBPutAttributesResponse *putResponse = [sdbClient putAttributes:putRequest];
// B.5. check response to make sure we did good
if (putResponse.exception==nil) {
NSLog(@"Attributes updated");
} else {
NSLog(@"error updating attributes: %@", putResponse.exception);
}
// C. and now list them to make sure what we added actually got stored - go with consistent so that saves happen
// C.1. create request object - get all attributes so we won't bother to add attributes to the request
SimpleDBGetAttributesRequest *getRequest = [[SimpleDBGetAttributesRequest alloc]initWithDomainName:domainName andItemName:@"1001"];
// C.2. call get attributes with response object
SimpleDBGetAttributesResponse *getResponse = [sdbClient getAttributes:getRequest];
// C.3. list data provided by the response object
if (getResponse.exception==nil) {
NSLog(@"%i Attribute values found", [getResponse.attributes count]);
for (SimpleDBAttribute *attrib in getResponse.attributes) {
NSLog(@"%@=%@", attrib.name, attrib.value);
}
} else {
NSLog(@"error getting attributes: %@", getResponse.exception);
}
}
@end