Search code examples
iosiphoneios7ios8ios8.1

Finding video type from NSData


I am loading an video from a URL provided by a third-party. There is no file extension (or filename for that matter) on the URL (as it is an obscured URL). I can take the data from this (in the form of NSData) and load it into a video player and display it fine.

I want to persist this data to a file. However, I don't know what format the data is in (mp4, wav)? I assume it is mp4 (since it's an video from the web) but is there a programmatic way of finding out for sure? I've looked around StackOverflow and at the documentation and haven't been able to find anything. I just wanted to know the file extension whether it is an image or video.


Solution

  • You should check the content type returned by the server:

    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)theResponse
      {
        NSString* content_type = [[(NSHTTPURLResponse*)theResponse allHeaderFields] valueForKey:@"Content-Type"];
    
        //content_type might be image/jpeg, video/mp4 etc.
      }