The following solution worked for me. Hope it help others as well.
NSString* fullPath = [yourpath stringByExpandingTildeInPath];
NSURL* fileUrl = [NSURL fileURLWithPath:fullPath];
NSURLRequest* fileUrlRequest = [[NSURLRequest alloc] initWithURL:fileUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:.1];
NSError* error = nil;
NSURLResponse* response = nil;
NSData* fileData = [NSURLConnection sendSynchronousRequest:fileUrlRequest returningResponse:&response error:&error];
NSString* mimeType = [response MIMEType];
NSArray *components = [mimeType componentsSeparatedByString:@"/"];
NSString *query = [components lastObject]; //gives the extension
此查询是文件扩展名。您可以根据您的扩展放置 if/else 或 switch 来执行操作。
if ([query isEqualToString:@"mp4"]) {
yourpath = [yourpath stringByAppendingPathExtension:@"mp4"];
}
// likewise for all extensions
NSData* video = [NSData dataWithContentsOfURL:fileUrl options:NSDataReadingUncached error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
[error release];
} else {
NSLog(@"Data has loaded successfully.");
}
BOOL success = [video writeToFile:yourpath atomically:NO];
[fileUrlRequest release];
if (success) {
//open your video the way you want
}