Created
December 30, 2015 17:05
-
-
Save kosso/06188d84885fbb57e560 to your computer and use it in GitHub Desktop.
scratchpad .. ObjectiveC stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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