How to retrieve the substring with in the string - iphone

I am new to iphone.I have small doubt that is I have a path of my audiofile which is placed in the directory in resources folder that path is
/Users/Chary/Library/Application Support/iPhone Simulator/5.0/Applications/B02404E5-52DC-49B6-8DBB-C9946E4331AF/BiblePlayer.app/raj/1.mp3
My question is how to retrieve the string "1.mp3" from that entire path?

NSString *fullPath = #"…";
NSString *fileName = [fullPath lastPathComponent];

NSString *allString = #"/Users/Chary/Library/Application Support/iPhone Simulator/5.0/Applications/B02404E5-52DC-49B6-8DBB-C9946E4331AF/BiblePlayer.app/raj/1.mp3"
int slashPosition = [allString rangeOfString:#"/" options:NSBackwardsSearch].location;
NSString *function = [allString substringFromIndexslashPosition + 1];
This will get you the path of the file after the last slash

try it:-
NSString *yourPath = #"/Users/Chary/Library/Application Support/iPhone Simulator/5.0/Applications/B02404E5-52DC-49B6-8DBB-C9946E4331AF/BiblePlayer.app/raj/1.mp3";
NSString *fileName = [yourPath lastPathComponent];
It may help u thank :)

Related

Concatenating Objective-C NSStrings

I'm a total newbie at Objective-C, so bear with me. This is how I'm concatenating my URL:
id url = [NSURL URLWithString:#"http://blahblah.com/gradient.jpg"];
id image = [[NSImage alloc] initWithContentsOfURL:url];
id tiff = [image TIFFRepresentation];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: #"Desktop"];
NSString *fileToWrite = #"/test.tiff";
NSString *fullPath = [docsDir stringByAppendingString:fileToWrite];
[tiff writeToFile:fullPath atomically:YES];
It works, but it seems sloppy. Is this the ideal way of doing concatenating NSStrings?
stringByAppendingString: or stringWithFormat: pretty much is the way.
You can append multiple path components at once. E.g.:
NSString* fullPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Desktop/test.tiff"];
You can also specify the entire path in a single string:
NSString* fullPath = [#"~/Desktop/test.tiff" stringByExpandingTildeInPath];
Have you looked into NSMutableString ?
A common convention is to use [NSString stringWithFormat:...] however it does not perform path appending (stringByAppendingPathComponent).

How Can I Get Remote FileName on Host When download it via ASIHttpRequest

How Can I Get Remote FileName on Host When download it via ASIHttpRequest??
for example, some url like this:
http://www.filedropper.com/processing/filedownload.php?id=test_22
test_22 is my filename, but original string is #"test_22.gif" , i can not analysis the filename extension from url.
i am trying to get responseString for the real filename when ASIHttpRequest call it delegate method :
- (void)request:(ASIHTTPRequest *)request
didReceiveResponseHeaders:(NSDictionary *)responseHeaders
but i got nothing useful.
any idea?
i hope, you can get file name through the following code..see the link to find file type.after finding append it..
NSString *url = #"http://www.filedropper.com/processing/filedownload.php?id=test_22";
NSArray *parts = [url componentsSeparatedByString:#"="];
NSString *filename = [parts objectAtIndex:[parts count]-1]; // or NSString *filename = [parts lastObject];

filemanager in obj c

in my iPhone app I'm using file manager to check size of file,code is working fine on simulator, but when I run the same app on iphone it is saying file size is zero even though file size is greater than zero, I am using following code:
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
documentsDirectory1 = [documentsDirectory1 stringByAppendingPathComponent:#"XML"];
NSString * szDestPath1 = [documentsDirectory1 stringByAppendingPathComponent:#"Extras"];
NSString *URL2 = [szDestPath1 stringByAppendingPathComponent:#"config.xml"];
NSLog(#"%#",URL2);
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL2 error:&attributesError];
int _fileSize = [fileAttributes fileSize];
NSLog(#"_fileSize:%U",_fileSize); //0 if empty
How can I solve this, can anyone help me? thanx in advance.
Are you sure that the file exists?
Usually if a piece of code that involves files works fine on the simulator but not on the device it is because of wrong filenames. Most simulators run on a case-insensitive filesystem, but the device has a case-sensitive filesystem.
So please check again if you use the correct file path.
You could add something like this to make sure that the file exists:
if (![[NSFileManager defaultManager] fileExistsAtPath:URL2]) {
NSLog(#"File does not exist");
}

CFURLRef of documents directory

I am creating an app that needs access to the documents directory. I am currently using the following to return the URL of a file pdfName from the main bundle. Is there a similar way of getting the documents directory?
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)pdfName, NULL, NULL);
Edit: this is my full code, but it isn't working - any ideas?
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *myFilePath = [documentsDirectory stringByAppendingPathComponent:pdfName];
CFURLRef pdfURL = (CFURLRef)[NSURL fileURLWithPath:myFilePath];
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
I had the same problem. The app crashed when creating the CFURLRef. This is how i solved it (given you already have an NSString with the complete path to the file in documents directory):
CFURLRef pdfURL = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:myFilePath];
pdf = CGPDFDocumentCreateWithURL(pdfURL);
CFRelease(pdfURL);
Looks like the only difference in my code is that I alloc and init the NSURL.
This might help you: ADC Link

how to delete a substring and get another part from that string

I have some string that have a type like this
NSString *string1 = #"/Users/mine/Library/Application Support/iPhone Simulator/3.2/Applications/02221798-1B7A-46B4-928F-F5BE37E177B5/Documents/Project/Project 1/Folder 1/2.php";
if i just want to get the "02221798-1B7A-46B4-928F-F5BE37E177B5"
i have implement a code like this :
NSString *subString1;
int count = [string1 length];
NSLog(#"count : %d", count);
subString1 = [string1 substringFromIndex:121];
but it left "02221798-1B7A-46B4-928F-F5BE37E177B5/Documents/Project/Project 1/Folder 1/2.php"
how can i do to fix it??
To get the name of your app's parent directory you should try this instead:
NSString *appUUID = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] lastPathComponent];
This will work both on a device and Simulator.
If you need to get the full path to the Documents folder inside your app sandbox:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
If you need to append a path component to a path string:
NSString *projectPath = [documentsPath stringByAppendingPathComponent: #"Project"];
Please, take a look at NSString documentation, section "Tasks", "Working with Paths". It is a must-read.
you can use stringWithRange method of NSString.
subString1 = [string1 stringWithRange:NSMakeRange(startPos,yourLengthToSelect)];
or you can use componentSeparetedByString and break string using #"/". it will return array of string. get your desired string using array index.