Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for tracks using the soundcloud Sdk iOS

I have read the soundcloud's sdk documentation for iOS and it doesn't seem to say anything about searching for songs, though it talked about listing tracks from an existing soundcloud user. So are there any resources out there or examples ? Thanks a million !

like image 933
ipalibowhyte Avatar asked Apr 13 '14 08:04

ipalibowhyte


People also ask

What can you do with SoundCloud API?

Our API gives you the ability to upload, manage and share tracks. Your app can take an audio file and upload it to a user's SoundCloud account. You can also manage metadata (including tags) or add the track to playlists.

How do I get oauth tokens on SoundCloud?

You don't generate it on yourself, you get it from the SoundCloud API. You have first to authorize with your client_id, client_secret and a redirect_url then in the redirect_url (which the soundcloud server will call it should be some script on your server) you can get the token from the GET parameter "code".

How do I use SoundCloud API on Android?

You need to get your client_id and client_secret by registering your app here. After you have obtained that, you can start using the API. Basically, most of the calls will look like this one: final SoundCloud api = new SoundCloud.


1 Answers

You have to use this format:

https://api.soundcloud.com/me/tracks?q=SEARCHTEXT&format=json

Just remember, if the user enters a space, you have to replace it with %20, you can achieve this by

NSString *search = [originalSearch stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

Then, just request the JSON data like this:

[SCRequest performMethod:SCRequestMethodGET onResource:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.soundcloud.com/me/tracks?q=%@&format=json", search]] usingParameters:nil withAccount:[SCSoundCloud account] sendingProgressHandler:nil responseHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

My final code looks like this:

 NSString *search = [searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

[SCRequest performMethod:SCRequestMethodGET onResource:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.soundcloud.com/me/tracks?q=%@&format=json", search]] usingParameters:nil withAccount:[SCSoundCloud account] sendingProgressHandler:nil responseHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    NSError *jsonError;
    NSJSONSerialization *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];

    if (!jsonError && [jsonResponse isKindOfClass:[NSArray class]]) {

        self.searchQuery = (NSArray *)jsonResponse;
        [self.tableView reloadData];

    }

    else {

        NSLog(@"%@", error.localizedDescription);

    }

}];`

I hope this helped!

like image 97
JomanJi Avatar answered Sep 21 '22 06:09

JomanJi