views:

65

answers:

2

Hi,

I'm trying to load an image into an UIImageView from the internet. This is no problem, but what I want is the image be secured with user credentials. So if you go to the direct link of the image, you get an login popup (secured directory).

How can I pass those user credentials so I can load the image from the net with user credentials?

The problem is that the image may not be accessed directly, but only via the iPhone app. If you have a better idea to maximize the security, let me know ;-)

Thanks in advance.

A: 

If you are using basic server authentication (which I think you are based on the "popup dialog"), I would recommend using ASIHTTPRequest http://allseeing-i.com/ASIHTTPRequest/How-to-use

It makes it very easy to download and make web requests. You can download the image and give the username and password by doing:

NSURL *url = [NSURL URLWithString:@"http://user:[email protected]/secret/"];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url];
[req startSynchronous];
NSError *err = [req error];
if (!err) {
    NSData *response = [req responseData];  
    UIImage* image = [UIImage imageWithData:response];
    // do whatever you want with the image here, like put it into a UIImageView
}
christophercotton
Thank you for your quick reply. Isn't that a little bit harmfull? Let say if I spoof the network, and the iPhone makes the request, the password is clearly visible isn't it?
CyberK
If you use Basic Authentication yes, it will be sent in the clear. You can use Message Digest authentication which will do a basic encoding and not send a cleartext password. The best is to use SSL + MessageDigest if you can. For how to setup the message digest on the server, it is server dependent you can google for it though. here is a link about apache: http://httpd.apache.org/docs/2.2/howto/auth.html and http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html
christophercotton
the ASIHTTPRequest will handle both digest and basic, so the code in the iphone is the same. You just need to configure the server.
christophercotton
Thank you Christoper, I'll give it a try...
CyberK
A: 

I'm assuming you have access to the server side. Create a simple PHP script that takes in an HTTP POST request with the username and password, validates it and if it's valid, echoes back a JSON/XML/string with the link to the image or else echoes back a null value or something.

Call a POST request from your iphone using urlWithString and when you get a response, check to see if it's null, or if it has a link.

Handle the result accordingly (if null, tell the user that the login credentials are incorrect).

If not null, it's the image - display it. Hope this helps.

Siddharth Iyer