Find out the extension part of URL saved in NSString - ios

How to find out and save the extension of fields that i am going to save in photo album. i want know it which format.
example:
http://www.example.com/myvideo.mp4,
http://www.example.com/mypicture.png
I need is like .mp4 and .png.If possible give me example code.

Have you looked at the documentation for NSString? You just need to send -pathExtension to your string.
If you're dealing with a string containing a URL, you should first convert it to an NSURL, then extract the path:
NSString *stringURL = #"http://...";
NSURL *url = [NSURL URLWithString:stringURL];
NSString *path = [url path];
NSString *extension = [path pathExtension];

NSURL also have pathExtension (Available in iOS 4.0 and later.)
NSString *extension = [[NSURL URLWithString: #"http://sample.example.com/path/hellowwrod.ext"] pathExtension];

refer a following code.
NSString *path = #"http://www.mysite.com/myvideo.mp4";
NSString *lastPath = [path lastPathComponent];
NSString *fileExtension = [lastPath pathExtension]; // [path pathExtension];
NSLog(#"%#", lastPath); //myvideo.mp4
NSLog(#"%#", fileExtension); // mp4

For Swift:
let stringUrl = " http://www.example.com/mypicture.png"
let url = URL(string: stringUrl)
let path = url?.path
let fileExtension = url?.pathExtension

Related

play .h264 video format - iOS

I have video data,
I need to convert the data into '.h264' format and save into a Temporary file.
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
NSURL* url = [NSURL fileURLWithPath:path];
I have created the temporary file path but, how can I attach the data along with the file and save.
Editted:
Sorry.
I was my mistake. self answering.
NSData *data = [[NSData alloc]initWithBase64EncodedString:val options:0];
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* htmlFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[data writeToFile:htmlFilePath atomically:YES];

convert Nsurl Domain to my country domain

how to convert Nsurl Domain to my country domain
Example :
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
How can get some thing like http://www.google.com.eg
it can detect my zone and change it with my domain zone, if website support multi zones domain.
If there isn't a path in the url just append .eg to the end of the string.
NSString *newCountryString = [[url absoluteString] stringByAppendingString:#".eg"];
NSURL *newCountryUrl = [NSURL URLWithString:newCountryString];
If the url has a path do this:
NSString *host = [url host];
NSString *domain = [[host componentsSeparatedByString:#"."] lastObject];
NSString *newCountryString = [[url absoluteString] stringByReplacingOccurrencesOfString:domain withString:[domain stringByAppendingString:#".eg"]];
NSURL *newCountryUrl = [NSURL URLWithString:newCountryString];

How to get url from video reference url

I have a url of video from photo library :-
/var/mobile/Applications/9BC2EBC4-7A71-4C8B-8BFB-D25D01E4CA83/Documents/IMG_5244.MOV
How can i get convert it to NSData I am tried following but nothing worked:
NSString* fileName = movieAsset.defaultRepresentation.filename;
NSURL* fileUrl = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject]URLByAppendingPathComponent:fileName];
NSString *str = [fileUrl path];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[arrVideoUrls addObject:fileUrl];
Here in your code you have add NSString as an URL and try to convert that NSString URL to NSData, so here first convert that string url to NSURL and then convert it in to NSData like below...
NSString *strVideoURL = #"assets-library://asset/asset.MOV?id=2EBD925F-D275-403E-A6A4-3487134D9B9D&ext=MOV";
NSURL *urlVideo = [NSURL URLWithString:strVideoURL];
NSData *videoData = [NSData dataWithContentsOfURL:urlvideo]];

How to determine the file extension of a file from a URL (Objective-C)

I have a URL of type: http://example.com/files/
And I need to determine what file is there (extension and name of file). Depending on extensions, I'll do something.
How can I determine it?
Try
NSString *ext = [[url lastPathComponent] pathExtension];
See https://developer.apple.com/documentation/foundation/nsstring/1407801-pathextension
You can use this code directly just passing your NSUrl and get File Extension in return.
- (NSString *)findExtensionOfFileInUrl:(NSURL *)url{
NSString *urlString = [url absoluteString];
NSArray *componentsArray = [urlString componentsSeparatedByString:#"."];
NSString *fileExtension = [componentsArray lastObject];
return fileExtension;
}
Try This,
NSURL *urll = [NSURL URLWithString:#"http://example.com/files/firstFile.com"];
NSString *filenameWithExtension = [urll lastPathComponent];
NSRange *range = [filenameWithExtension rangeOfString:#"."];
USing the substringWithRange just substring of those ....
NSString *fileName = [string substringWithRange:NSMakeRange(0, range.location)];
NSString *extension = [string substringWithRange:NSMakeRange(10, string.length - range.location-1)];
NSLog(#"%# %#", fileName, extension);

How do I extract the file name from a path string

For saving a file on the iPad, I need to get the file name substring (after sites/default/files/) in the following path string:
sites/default/files/243347_TroubleShootingSqlserver20052008.pdf
How would I do this?
Set your path to NSURL *targetURL. and then
NSString *filename = [[targetURL path] lastPathComponent];
NSArray* pathURL = [path componentsSeparatedByString: #"/"];
NSString* pdfFileName = [pathURL objectAtIndex: [pathURL length]];

Resources