Hiro
Hiro

Reputation: 11

how to extract the url path from my url link in Xcode?

I have a URL link like "http://mobile/testing.php?action=2, when i have this link on my IE browser, it will redirect the link to "http://mobile/myimage.jpeg". It directed to my database server folder name, where my the file I stored called myimage.jpeg.

I am wondering I how can I can put "http://mobile/testing.php?action=2", establish the NSURL connection and let the Xcode learn this path "http://mobile/myimage.jpeg" so that I can extract the image.jpeg and store it into a list where this list can reflect all the files I had in my database.

Anyone can help? would there be any NSURL reference I can use? I did checkup on the pathomponent, but I don know how can I implement it.

Upvotes: 1

Views: 681

Answers (1)

Dave DeLong
Dave DeLong

Reputation: 243156

If you want to know where it actually gets redirected to, then you'll need to use an asynchronous NSURLConnection and implement the -[<NSURLConnectionDelegate> connection:didReceiveResponse:] delegate method.

The NSURLResponse object passed to that method will actually be an NSHTTPURLResponse, which means you can:

  1. Ask it for its -statusCode. If the code is a redirection code, then you know your original request was redirected, at which point you can...
  2. Ask it for its -URL. This is the NSURL to which your original request was redirected. From here, you can get its -lastPathComponent to extract the "myimage.jpeg" bit.

If I've totally misunderstood your question, then you can probably just extract the -path from your starting URL and get the -lastPathComponent from that.

Upvotes: 2

Related Questions