Skip to content

Instantly share code, notes, and snippets.

@kosso
Created December 30, 2015 17:05
Show Gist options
  • Save kosso/06188d84885fbb57e560 to your computer and use it in GitHub Desktop.
Save kosso/06188d84885fbb57e560 to your computer and use it in GitHub Desktop.
scratchpad .. ObjectiveC stuff
// GET and parsing some remote JSON data inside an iOS module for a Titanium app.
// @Kosso
// Test with amazingtunes.com API of recently played tunes on /AmazingRadio/.
NSString *amazingHistory = @"http://amazingtunes.com/radio/history.json";
-(void)getData:(id)args
{
NSLog(@"[INFO] getData");
NSURLSession *session = [NSURLSession sharedSession];
// get the data
[[session dataTaskWithURL:[NSURL URLWithString:amazingHistory]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
if(!error){
NSLog(@"[INFO] response ok");
NSArray* array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
// prove I can read the JSON... for the sake of it... hey,i'm learning... ;)
/*
NSUInteger entriesCount = [array count];
for (int i = 0; i < entriesCount; i++) {
NSArray* entry = [array objectAtIndex:i];
NSMutableDictionary* tune = [entry valueForKey:@"tune"];
NSMutableDictionary* artist = [tune valueForKey:@"artist"];
NSLog(@"[INFO] tune: %@ - %@", [tune valueForKey:@"title"], [artist valueForKey:@"display_name"]);
}
*/
// send the JSON data back to Titanium in an event
if ([self _hasListeners:@"gotHistory"]) {
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
array, @"data",
self, @"source",
@"gotHistory", @"type",nil];
[self fireEvent:@"gotHistory" withObject:event];
}
} else {
NSLog(@"[INFO] response error");
NSLog(@"[ERROR]: %@", error);
}
}] resume];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment