How to implement the string SCAudioStream? - Sound Cloud - objective-c

I'm working with the sound cloud sdk, I'm trying to use their libraries to play Audio Stream.
I have not been able to find any reference on how to use that class.
I would appreciate any help you could provide.

If you are talking about this libraries I can help you.
First of all you should run this method to get some info about track by its ID:
[api performMethod: #"GET"
onResource: #"tracks"
withParameters: [NSDictionary dictionaryWithObject:trackID forKey:#"ids"]
context: #"trackid"
userInfo: nil];
If you don't know trackID and have only link to track like this : http://soundcloud.com/go-yoshimura/mikasasukasa, then run this method:
[api performMethod: #"GET"
onResource: #"resolve"
withParameters: [NSDictionary dictionaryWithObject:songLink forKey:#"url"]
context: #"songname"
userInfo: nil];
Then you should implement delegate's methods:
- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI
didFinishWithData:(NSData *)data
context:(id)context
userInfo:(id)userInfo;
- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI
didFailWithError:(NSError *)error
context:(id)context
userInfo:(id)userInfo;
In first method you should parse data using JSON Parser (JSONKit or SBJSON). You should get NSDictionary.
streamURL = [(NSDictionary *)jsonData objectForKey:#"stream_url"];
streamable = [[(NSDictionary *)jsonData objectForKey:#"streamable"] boolValue];
If this track is streamable, you can get SCAudioStream this way:
stream = [[SCAudioStream alloc] initWithURL:[NSURL URLWithString:streamURL] authentication:auth];
Auth you can create earlier in your init method this way:
conf = [SCSoundCloudAPIConfiguration configurationForProductionWithClientID: kSCClientID
clientSecret: kSCClientSecret
redirectURL: kSCRedirectURL];
auth = [[SCSoundCloudAPIAuthentication alloc] initWithAuthenticationDelegate:self apiConfiguration:conf];
And now you have SCAudioStream.
//Playing
[stream play];
//Pause
[srteam pause];
//Seeking
[stream seekToMillisecond:ms startPlaying:YES];
That's all :)

Related

IOS App Action extension is not closing

I am facing app extension close issues , please tell me if anyone know what wrong am doing.I am using action extension after preform some action inside extension i need to return response back.
Sample Code
// With Success Case
- (void) completeActionWithItems: (NSString *) response {
NSExtensionItem *extensionItem = [[NSExtensionItem alloc] init];
extensionItem.attachments = #[[[NSItemProvider alloc] response typeIdentifier: (NSString *)kUTTypePlainText]];
[self.extensionContext completeRequestReturningItems: #[extensionItem] completionHandler: nil];
}
// With Error Case
- (void) completeActionWithError: (NSError *) error {
[self.extensionContext cancelRequestWithError: error];
}
With Success Case working fine but some time is not closing,
With Error Case not working above code.
Please let me know what went wrong.Thanks
When you create an action extension, this is the default method which will close the Action Extension View Controller:
- (IBAction)done {
// Return any edited content to the host app.
// This template doesn't do anything, so we just echo the passed in items.
[self.extensionContext completeRequestReturningItems:self.extensionContext.inputItems completionHandler:nil];
}
Since this method is already provided, you should just try calling it from your success method.
// With Success Case
- (void) completeActionWithItems: (NSString *) response {
NSExtensionItem *extensionItem = [[NSExtensionItem alloc] init];
extensionItem.attachments = #[[[NSItemProvider alloc] response typeIdentifier: (NSString *)kUTTypePlainText]];
[self.extensionContext completeRequestReturningItems: #[extensionItem] completionHandler: nil];
// Call to "done" method
[self done];
}

ConnectionKit and SFTP: How to authenticate CK2FileManager

For my Mac OS X Cocoa app, I am trying to
connect to a SFTP server that only accepts username/password credentials
get the contents of a remote directory
upload files
and find it surprisingly complicated.
After trying ConnectionKit (nearly no documentation), NMSSH (crashed once too often with simultaneous uploads), rsync (not supported by the server), sftp (needs key authentication if scripted, doesn't work with username/password), I am now back to ConnectionKit: https://github.com/karelia/ConnectionKit
However, I am struggling with the authentication challenge, as I don’t know what to do with my credential in the delegate method.
I downloaded and compiled ConnectionKit (apparently version 2).
I am trying to use CK2FileManager as the Readme indicates (is this the right approach at all? Or should I use the libssh2_sftp-Cocoa-wrapper instead?… however I had troubles with libssh2 blocking methods in NMSSH before)
I am successfully setting up my connection URL and
my delegates' -didReceiveAuthenticationChallenge is called
But this is where I struggle: I know how to create a NSURLCredential, however, I can’t figure out what to do with it =>
- (void)fileManager:(CK2FileManager *)manager
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *credentials = [NSURLCredential
credentialWithUser:self.username
password:[self getPassword]
persistence:NSURLCredentialPersistenceForSession];
// what to do now?
// [manager useCredential:] doesn’t exist, nor is there a manager.connection?
// ...
}
I already read the header, I searched the archives of this list, but all answers seem to be outdated.
I also searched Google, Bing and StackOverflow and found one promising example from 2011 using CKFTPConnection, which doesn’t seem to be included in the current framework anymore.
Thanks so much for any pointer to the right direction.
tl;dr
I don't know how to respond to ConnectionKit's CK2FileManager authenticationChallenge:
see the comment in the code example
For CK2:
- (void)listDirectoryAtPath:(NSString *)path
{
// path is here #"download"
NSURL *ftpServer = [NSURL URLWithString:#"sftp://companyname.topLevelDomain"];
NSURL *directory = [CK2FileManager URLWithPath:path isDirectory:YES hostURL:ftpServer];
CK2FileManager *fileManager = [[CK2FileManager alloc] init];
fileManager.delegate = self;
[fileManager contentsOfDirectoryAtURL:directory
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
completionHandler:^(NSArray *contents, NSError *error) {
if (!error) {
NSLog(#"%#", contents);
} else {
NSLog(#"ERROR: %#", error.localizedDescription);
}
}];
}
and than you have to implement the following protocol
- (void)fileManager:(CK2FileManager *)manager operation:(CK2FileOperation *)operation
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(CK2AuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodDefault]) {
completionHandler(CK2AuthChallengePerformDefaultHandling, nil);
return;
}
NSString * username = #"<username>";
NSString * pathToPrivateSSHKey = #"</Users/myNameOnLocalMaschine/.ssh/id_rsa>"
NSURLCredential *cred = [NSURLCredential ck2_credentialWithUser:username
publicKeyURL:nil
privateKeyURL:[NSURL fileURLWithPath:pathToPrivateSSHKey]
password:nil
persistence:NSURLCredentialPersistenceForSession];
completionHandler(CK2AuthChallengeUseCredential, cred);
}
That's it.
Call -listDirectoryAtPath: and then you will get in the Completion Handler Block in contents array all the files located on the given path :)
Okay, that was easy and I could have found out that on my own; just for the reference: [[challenge sender] useCredential:credentials forAuthenticationChallenge:challenge];
Sorry to reward myself for my own question, but maybe this code snippet helps filling the missing docs, this is how I connect to my SFTP server with ConnectionKit:
- (void)connectWithCompletionBlock:(void (^)(void))completionBlock {
if(!self.cFileManager) {
self.cFileManager = [[CK2FileManager alloc] init];
self.cFileManager.delegate = self;
}
NSURL *sftpServer = [NSURL URLWithString:[#"sftp://" stringByAppendingString:self.server]];
self.remoteFolder = [CK2FileManager URLWithPath:self.remotePath relativeToURL:sftpServer];
// try to get the contents of the current directory
[self.cFileManager contentsOfDirectoryAtURL:self.remoteFolder
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
completionHandler:^(NSArray *contents, NSError *error)
{
NSLog(#"remote folder contents: \n%#", contents);
// invoke completion block
completionBlock();
}];
}
- (void)fileManager:(CK2FileManager *)manager
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *credentials = [NSURLCredential
credentialWithUser:self.username
password:[self getPassword]
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credentials forAuthenticationChallenge:challenge]
}

UIActivityItemSource Protocole set complex object

I'm using iOS 6 new way to share information : UIActivityViewController. To select the shared data depending on the media (facebook, twitter or mail) my view controller implement the UIActivityItemSource Protocol as follow :
- (IBAction)onShareButton:(UIButton *)sender
{
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[self] applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypeMessage, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMessage, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityViewController animated:YES completion:^{}];
}
#pragma mark - UIActivityItemSource Protocol
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
NSArray *items = #[#"message facebook", [NSURL URLWithString:#"http://www.myUrlFacebook.com"]];
return items;
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
NSArray *items = #[#"message twitter", [NSURL URLWithString:#"http://www.myUrlTwitter.com"]];
return items;
} else if ([activityType isEqualToString:UIActivityTypeMail]) {
NSArray *items = #[#"message mail", [NSURL URLWithString:#"http://www.myUrlMail.com"]];
return items;
}
NSArray *items = #[#"Not a proper Activity", [NSURL URLWithString:#"http://www.myUrlMail.com"]];
return items;
}
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return #"PlaceHolder";
}
When I'm returning a simple NSString for activityViewController:itemForActivityType: the string is well used by my UIActivityViewController, but I can't find a way to use an Array !
According to Apple Documentation it should be possible :
This method returns the actual data object to be acted on by an activity object
Apple documentation
Does anyone ever use this UIActivityItemSource Protocol with Arrays, or is there a use full tutorial to do that ?
Note : I also got this error on the console, it may help ...
Launch Services: Registering unknown app identifier com.apple.mobilemail failed
Launch Services: Unable to find app identifier com.apple.mobilemail
A single object conforming to UIactivityItemSource can only return a single piece of data for activityViewControllerPlaceholderItem:, no NSArrays.
You could overcome this by creating and passing two UIActivityItemSources in the activityItems part of the initial initWithActivityItems:. Each source can pass a placeholder value, but can return something blank on itemForActivityType so you don't actually have to use that particular type of data depending on the activity.
Or just use that cool extension mentioned in the other answer.
After spending a significant amount of time trying to figure this one out, it seems it isn't possible to pass it an NSArray of items. So I extended UIActivityViewController to make it possible.
RDActivityViewController

SoundCloud API and 401 Error for registered application

I have a problem with SoundCloud API in my application.
I have created an app, what get link to the track (e.g. something like this http://api.soundcloud.com/tracks/48760960) and get stream_url, what later can be played using SCAudioStream. There is no auth for users, I use only client id and client secret of my app. App is registered.
I create soundCloudManager this way:
- (id)init
{
if(self = [super init])
{
conf = [SCSoundCloudAPIConfiguration configurationForProductionWithClientID: kSCClientID
clientSecret: kSCClientSecret
redirectURL: kSCRedirectURL];
conf.accessTokenURL = kSCAccessTokenURL;
api = [[SCSoundCloudAPI alloc] initWithDelegate: self
authenticationDelegate: self
apiConfiguration: conf];
//[api checkAuthentication];
}
return self;
}
If uncomment [api checkAuthentication], then logs will says next:
"auth ready with URL:https://soundcloud.com/connect?client_id=&redirect_uri=&response_type=code"
Then I call this method to get data from track:
- (void)runSearchingStreamWithTrackID: (NSString *)trackID
{
[api performMethod: #"GET"
onResource: #"tracks"
withParameters: [NSDictionary dictionaryWithObject:trackID forKey:#"ids"]
context: #"trackid"
userInfo: nil];
}
And then this delegate's method calls:
- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI
didFailWithError:(NSError *)error
context:(id)context
userInfo:(id)userInfo
Text of error:
"HTTP Error: 401".
It means unauthorized. But why? Should I be authorized as soundCloud user to getting info about tracks?
I'm using this for SoundCloudAPI:
https://github.com/soundcloud/cocoa-api-wrapper
Please, help! :(
Today I fixed it. I just add this method to init :
[api authenticateWithUsername:kLogin password:kPwd];
After it this method was called:
- (void)soundCloudAPIDidAuthenticate
So, this test account was authorized.
Then I call this method:
- (void)runSearchingStreamWithTrackID: (NSString *)trackID
{
[api performMethod: #"GET"
onResource: #"tracks"
withParameters: [NSDictionary dictionaryWithObject:trackID forKey:#"ids"]
context: #"trackid"
userInfo: nil];
}
And no one of these methods will be called:
- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI
didFailWithError:(NSError *)error
context:(id)context
userInfo:(id)userInfo;
- (void)soundCloudAPI:(SCSoundCloudAPI *)soundCloudAPI
didFinishWithData:(NSData *)data
context:(id)context
userInfo:(id)userInfo;
- (void)soundCloudAPIDidAuthenticate;
- (void)soundCloudAPIDidResetAuthentication;
- (void)soundCloudAPIDidFailToGetAccessTokenWithError:(NSError *)error;
- (void)soundCloudAPIPreparedAuthorizationURL:(NSURL *)authorizationURL;
But there is log:
-[NXOAuth2PostBodyStream open] Stream has been reopened after close
And this method was called:
[NXOAuth2Client oauthConnection:connection didFailWithError:error];
error: HTTP 401
What do I wrong?
I understood the problem. Just download the SC API from the official site and install the latest SC API. Then do something like this:
- (void)searchStreamURLByTrackID: (NSString *)trackID
{
NSString *string = [NSString stringWithFormat:#"https://api.soundcloud.com/tracks/%#.json?client_id=%#", trackID, kSCClientID];
[SCRequest performMethod: SCRequestMethodGET
onResource: [NSURL URLWithString:string]
usingParameters: nil
withAccount: nil
sendingProgressHandler: nil
responseHandler:^(NSURLResponse *response, NSData *data, NSError *error){
NSString *resultString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"FOUND:\n%#", resultString);
}
];
}
After that obtain the stream url from the JSON in the resultString.
Finally, you have to write this code:
NSString *streamURL = [(NSDictionary *)jsonData objectForKey:#"stream_url"];
NSString *realURL = [streamURL stringByAppendingFormat:#".json?client_id=%#", kSCClientID];
AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:realURL]];
Enjoy!
Did you check https://github.com/soundcloud/CocoaSoundCloudAPI ?
This is the more current version of the API wrapper (if you don't need iOS 3.0 support).
It also comes with a tutorial video: http://vimeo.com/28715664
If you want to stay with this library you should try to figure out where the log message ("auth ready with ...") appears. Did you implement any of the AuthorizationDelegate methods?
See https://github.com/soundcloud/cocoa-api-wrapper/blob/master/Usage.md#the-authentication-delegate-the-easy-way

Help with making NSURLConnection in a custom class

Hey all, I'm trying to use Yahoo's PlaceFinder to do reverse geocoding for an app I'm making. Problem is I need to use the NSURLConnection to call to my database. So I decided to make a custom class that is initialized with the user's latitude and longitude, and only store a string variable containing the state the user is in.
Update the following code now works fine....
Here is the .h
#import <Foundation/Foundation.h>
#import "CJSONDeserializer.h"
#interface StateFinder : NSObject
{
NSString *userState;
NSURLConnection *connection;
}
-(id)initwithLatitude:(NSString *)latitude andLongitude:(NSString *)longitude;
#property (nonatomic, retain) NSString *userState;
#property (nonatomic, retain) NSURLConnection *connection;
#end
and the .m
#import "StateFinder.h"
#implementation StateFinder
#synthesize userState;
#synthesize connection;
-(id)initwithLatitude:(NSString *)latitude andLongitude:(NSString *)longitude
{
if(self = [super init])
{
NSString *lat = latitude;
NSString *lon = longitude;
NSString *stateURLFinder = [NSString stringWithFormat:#"http://where.yahooapis.com/geocode?q=%#,+%#&gflags=R&flags=J&appid=zqoGxo7k", lat, lon];
//NSLog(stateURLFinder);
NSURL *stateURL = [NSURL URLWithString:stateURLFinder];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: stateURL];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];
}
return self;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(#"didReceiveResponse");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"didFinishLoading");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Store incoming data into a string
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(jsonString);
// Yes, this is incomplete, but I was waiting for the method to fire before going
// any further. This will at least show me the JSON data being returned from yahoo
// in string format so I can output it to the console via NSLog
}
- (void)dealloc
{
[userState release];
[connection release];
[super dealloc];
}
#end
This is the current code I'm using and it works fine. All I did was include the connectionDidFinishLoading and didFailWithError methods to the original code. With regards to the connection being released before it was made, I used the code above as is without the previously mentioned methods and neither didReceiveData/didReceiveResponse would hit. It wasn't until those 2 methods were included that the methods began getting called. Not sure how, not sure why, but that was the only change among all of those suggested that worked. Big thanks to #Jiva DeVoe , #XJones , #jlehr and #Aby for all the tips/hints/suggestions!
Actually, I advise that you definitely do not use sendSynchronousRequest. You should always try to use asynchronous networking unless the app you're developing is a command line app without a run loop.
I suspect your problem may be that you're releasing the connection immediately, so it's never getting a chance to run. You should add a member variable and keep it around until you've received a response or whatever.
Bonus tip:
Guessing you're probably doing this for either iOS or the Mac, and writing GUI apps. These apps have run loops. When you use synchronous networking like the prior answer suggests, you prevent that runloop from executing. This means that if the request takes longer than a few seconds, your iOS app will be killed by the OS, and your Mac app will appear non-responsive. Neither of these are good results.
You're sending a release message to the connection before it's had a chance to run; don't do that. With regards to JSON, the string you said was returned from the server is JSON. Were you expecting something else?
Get rid of the line:
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
Once you've initialized the connection with the request it runs automatically. You have other problems as well. Your implementation of the NSURLConnectionDelegate protocol is wrong. You need to incrementally add data received in connection:didReceiveResponse to an NSMutableData object. You can convert it to JSON in `connectionDidFinishLoading:'.
As for the memory leak, release the connection in connectionDidFinishLoading: or connection:didFailWithError: You are guaranteed to receive one but not both of them.
Everything you need to know is here
[EDIT: added NSURLConnection code sample]
// Attach this to the touchUpInside event of a UIButton
// Note that all objects will be autoreleased
// Note that you can comment out any or all of the NSURLConnection delegate methods
// and the request will execute
- (IBAction)initiateRequest
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:#"http://www.google.com"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)date
{
NSLog(#"connection:didReceiveData");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(#"connection:didReceiveResponse:");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"connectionDidFinishLoading:");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"connection:didFailWithError:");
}
Thanks to XJones for the suggestion. I wasn't including the two methods didFinishLoading or didFailWithError once inserted into my code, both didReceiveResponse and didReceiveData both began to hit. Thank you all for the tips, advice and suggestions and hopefully this helps someone else out down the road.