Finding extension of an URL - objective-c

i have a string named url i.e
NSString *url = #"http://sound17.mp3pk.com/indian/barfi/%5BSongs.PK%5D%20
Barfi%20-%2001%20-%20Barfi!.mp3";
Now I want that It should search from last upto the .(dot)
i.e it should search
mp3 in string as it is coming after .(dot) and want to store that mp3 in the temporary variable.
how can i use lastindex(".") or something else to store in temporary variable.

Simple as that:
NSString *extension = [url pathExtension];

Related

Screen Scraping Error while using NSURL - Objective c

I am currently trying to write a program that reads the source code of http://finance.yahoo.com/q?s=aapl and returns the value associated with the average 3 month volume (Avg Vol 3m). I am using NSURL as follows:
NSString *Ticker = #"aaple";
NSString *website = #"http://finance.yahoo.com/q?s=";
NSString *newwebsite = [website stringByAppendingString: Ticker];
NSURL *myURL = [NSURL URLWithString: newwebsite];
NSString *myHomePage = [NSString stringWithContentsOfURL: myURL encoding: NSASCIIStringEncoding error: NULL];
NSLog (#"%#", myHomePage);
However when I print out the source code I notice that the string does not contain the value of the average 3 month volume nor does it contain the reference to it. Similarly I notice that the output of the code is different from the source code of the same website when viewed through a browser (view->developer->view source on chrome). Am I using the correct classes/methods? What can I do so that my program's output is the same as that viewed through my browser?
Btw I am aware that once I have the proper source code I can extract the needed strings using rangeofString, substringToIndex, subStringFromIndex, etc. The focus of this question is how to obtain the correct source code and not what to do to it once I have it.

How to Use An Escape URL String With Formatting in Objective-C

I need to gather data from a website based on the user's input.
searchString is the user inputted value, such as "search this string".
NSString *withoutSpaces = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Here, I need to replace spaces with %20
Next, I need to put the new string without spaces (replaced with %20) into another string.
NSString *unescapedSearchString = [NSString stringWithFormat:
#"website.com/query?=%22%#%22", withoutSpaces];
The site I need is not really "website.com", but that's just an example. I also need the %22 to remain at the beginning and end.
As you can see, I need the %# to format the new withoutSpaces user input into the website URL.
I did a search and found examples but I could not find any with formatting such as in my case using %#.
What's the best way to "escape" the characters and keep my formatted string? Currently, when I try to access data from the website, it comes back as null. However, when I try a string without the %# formatting and an actual value, I successfully retrieve the data from a website.
Any help is greatly appreciated.
You should do things this way:
NSString *searchString = ... // the raw search string with spaces and all
NSString *quoted = [NSString stringWithFormat:#"\"%#\"", searchString];
NSString *escaped = [quoted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"website.com?query=%#&value=all", escaped];
BTW - the URL seems a little off. There should be a variable name before the = and after the ?.

ios - how do I concatinate strings to create a url?

I am trying to make a url by first collecting the parameters, and then in one statement creating the actual url. Here is what I am trying to do:
NSString *urlString = #"http://www.some_login_url.com?email=%#&password=%#";
NSString *email = self.email.text;
NSString *password = self.password.text;
NSString *url_to_send = [NSString stringWithFormat:#"%#%#", urlString , email , password];
So what I wanted to do was replace the # symbols with the values in the variables, but instead the second variable just got appended to the end of the string.
How would I change the last line so I could put the right parameters in their correct spots?
Thanks!!
I think you meant:
NSString *url_to_send = [NSString stringWithFormat:urlString , email , password];
But you might want to look for ways to construct a URL that do proper escaping of arguments, etc. For example, see this question.
The urlString is your format, so you want:
NSString *url_to_send = [NSString stringWithFormat:urlString , email , password];

stringByReplacingOccurrence does not replace string

I need to parse a FILE URL in my application, and replace the %20 for a SPACE. I am using stringByReplacingOccurance:
NSString *strippedContent = [finalFilePath stringByReplacingOccurrencesOfString:#"%20" withString:#" "];
But when I display strippedContent in an NSLog, all of the %20 strings are still there. Here is an example of the file name I hope to parse:
.../Documents/Inbox/Test%20Doc%20From%20Another%20App.txt
It seems as if NSFileManager cannot find the document when it has the %20 in it.
The file path is being passed from another application through the "Open In..." dialogue. Is there any way to remove the %20 with stringByReplacingOccurrence or when the URL is imported?
NSString provides a method that performs the conversion that you need:
NSString *strippedContent = [finalFilePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Yes you should use:
NSString * strippedContent = [finalFilePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Multiple Substrings in String

I´m working on an app that displays 6 pictures from a website. These pictures change over time so I extracted the sourcecode of said website and managed to pull the first of the 6 pictures with this code:
NSError *error = nil;
NSString *deviantStringPopular;
deviantStringPopular = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://browse.deviantart.com/?order=24"]
encoding:NSISOLatin1StringEncoding
error:&error];
NSString *popularContent;
NSRange popularURLRange1 = [deviantStringPopular rangeOfString:#"super_img=\""];
NSRange popularURLRange2 = [deviantStringPopular rangeOfString:#"\" super_w"];
int lengt = popularURLRange2.location - popularURLRange1.location - popularURLRange1.length;
int location = popularURLRange1.location + popularURLRange1.length;
NSRange endRange;
endRange.location = location;
endRange.length = lengt;
popularContent = [deviantStringPopular substringWithRange:endRange];
NSLog(#"%#", popularContent);
The problem is, the other 5 images' URLs are between the same substrings as the first one.
So is it possible that the first image's URL is ignored once the it´s loaded successfully and the second one loads and is stored under a different variable and so on?
thanks in advance
To get 6 URLs from the string and load them into different variables, you could try using a loop. With each iteration, after the substring is found and stored into a variable, make a range from the beginning of the string up to the end of the substring you found. You can use stringByReplacingCharactersInRange to erase the part of the string already searched so that the next time you search the string for the substring, it will find the next URL.
An easier solution may be to use RegexKit, if you know how to use RegEx's. Then each URL could just be a capture group.