0

I am using NSURLConection and its delegate methods to asynchronously get data from an online TTS api. Here is the URL I am trying to load:

http://tts-api.com/tts.mp3?q=hello world

The above URL redirects and give us an MP3 file which I need to download. I have used the following delegate method to download the mp3 file:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    if (connection == connectionToTTSAPI) {
        mp3Manager = [NSFileManager defaultManager];
        localPath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"hello.mp3"];
        mp3Handler = [NSFileHandle fileHandleForWritingAtPath:localPath];
        if (!mp3Handler) {
            [[NSFileManager defaultManager] createFileAtPath:localPath contents:nil attributes:nil];
            mp3Handler = [NSFileHandle fileHandleForWritingAtPath:localPath];
        }
        @try {
            [mp3Handler seekToEndOfFile];
            [mp3Handler writeData:data];
        }
    }

}

Since there are more than one NSURLConnection I am using in my program I have set an if condition to recognise the response is for which connection. But I was not getting the mp3 file, so I set a break point inside the delegate method and discovered that the the connection to TTS API is never getting a response. I think it is because of the redirection. I have seen the use of the following method when i went through some other questions, but I didn't find such a function in NSURLConnectionDelegate, and also as a starter with NSURLConnection I don't know how to use the method for handling redirects. None of the results gave me the clear idea how to use it.

- (NSURLRequest *)connection: (NSURLConnection *)inConnection
             willSendRequest: (NSURLRequest *)inRequest
            redirectResponse: (NSURLResponse *)inRedirectResponse;
MattR
  • 6,908
  • 2
  • 21
  • 30
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113

1 Answers1

0

That delegate method, for iOS, is defined in NSURLConnectionDataDelegate and gives you the opportunity to control how the redirect is handled (you could cancel it, change it or return it unchanged).

In your case you want to let it request the URL as returned by the redirect response so a simple implementation would be:

- (NSURLRequest *)connection: (NSURLConnection *)inConnection
         willSendRequest: (NSURLRequest *)inRequest
        redirectResponse: (NSURLResponse *)inRedirectResponse {

    return inRequest;
}

I believe this should be the default behaviour though, so if the request isn't being processed, the problem is likely elsewhere...

MattR
  • 6,908
  • 2
  • 21
  • 30
  • sorry didn't get you.. where to return this? – Harikrishnan Aug 19 '13 at 06:53
  • Your implementation of the method you NSURLConnectionDelegate method you mentioned... I'll update the answer. – MattR Aug 19 '13 at 06:55
  • no, actually i didn't find that method `- (NSURLRequest *)connection: (NSURLConnection *)inConnection willSendRequest: (NSURLRequest *)inRequest redirectResponse: (NSURLResponse *)inRedirectResponse` in NSURLConnection delegate. In which delegate is that method declared? – Harikrishnan Aug 19 '13 at 06:57
  • It's in `NSURLConnectionDelegate` for OSX - are you coding OSX or iOS? :-) – MattR Aug 19 '13 at 06:59
  • Ok got it fixed.. Looks like it was some problem with Xcode auto filling, ok, if I return inRequest, it will be returned to where? and how can I call the `- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;` method using the redirected URL? – Harikrishnan Aug 19 '13 at 07:03
  • You don't need to call it, it will be called when the redirect response is received... Try implementing it... Updated answer - that method is defined in `NSURLConnectionDataDelegate`. And tagged question with iOS – MattR Aug 19 '13 at 07:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35725/discussion-between-harikrishnan-t-and-mattr) – Harikrishnan Aug 19 '13 at 07:10
  • Can we move this to chat? I am still having trouble. Please join: http://chat.stackoverflow.com/rooms/35725/discussion-between-harikrishnan-t-and-mattr – Harikrishnan Aug 19 '13 at 07:11