tags:

views:

1241

answers:

3

Hi There, Im trapping link clicked information in a UIWebView in the shouldStartLoadWithRequest delegate. I am using mainDocumentURL to get the url of the linked clicked. This shows the complete url but I need JUST the name of the page i.e .html, .htm etc requested. I cant see any property that would return this. Do i need to parse and extract this my self or does anyone know if there is a property or method in NSURLRequest that will return this.

Many thanks for your help. tony

+1  A: 

I think you mean the path of the URL. The code below extracts a string, s, whose value is @"/cheese.html".

NSURL* u = [NSURL URLWithString:@"http://www.wibble.com/cheese.html"];
NSString* s = [u path];

Have a look at the documentation for RFC 1808 which tells you how a URL is made up, and then read Apple's documentation for NSURL, which tells you how to extract those parts of the URL.

Jane Sales
A: 

NSURL can extract most of the URL elements. I suspect you mean the path which can be accessed with the path message.

If you really want the extension from the path I suggest you use path and then examine the NSString returned or, convert the whole URL into a string by sending an absoluteString message. Trimming the extension from an NSString can be performed many ways.

Roger Nolan
+1  A: 

If I'm understanding you correctly, you want to convert to an NSString and then use its methods. Starting with your NSURLRequest, you'd do something like this (note, I typed this into a web page and have not compiled it):

NSURL *url = [request URL];
NSString *filename = [[url path] lastPathComponent];

If your URL us something like http://www.foo.com/bar/baz.html, this will give you "baz.html".

Tom Harrington