Load image for UIImageView from the net with security - iphone

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.

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:passwd#yoursite.com/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
}

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.

Related

Facebook iOS SDK 3.0, implement like action on a url?

I'm trying to implement Like via the facebook open-graph-api with the Facebook iOS SDK 3.0.
Everything seems to work except the FbGraphObject and that's because I have no idea how it should look because this clearly does not work.
What I'm trying to do is to like a url posted as an object. A simple Like with via the open-graph.
The error message I get the the code below is:
The action you're trying to publish is invalid because it does not specify any
reference objects. At least one of the following properties must be specified: object.
The code I use is this:
FBGraphObject *objectToLike = [[FBGraphObject alloc]initWithContentsOfURL:[NSURL URLWithString:facebookLike.titleLabel.text]];
FBRequest *requestLike = [[FBRequest alloc]initForPostWithSession:[FBSession activeSession] graphPath:#"me/og.likes" graphObject:objectToLike];
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
[connection addRequest:requestLike
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
if (!error &&
result) {
DLog(#"NothingWentWrong");
}
DLog(#"MajorError: %#", error);
}
];
[connection start];
UPDATE:
Checked some more info and my guess it to use this method:
https://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBGraphObject/#//api/name/graphObject
To somehow create an object. It's the graphObject method that I probably need to do something with. Any help at all would be appreciated.
I've actually manage to create a simple and quite dirty solution of this.
The solution does not seem optimal but it's currently a working solution.
If anybody has used the explorer tool on facebook on this url:
https://developers.facebook.com/tools/explorer/
You know how the URL will look like when facebook is sharing a like. It has to have the URL and an access-token.
So my solution became just to disregard sending anything from the Facebook SDK and just send a post request to the same URL that I've used in the explorer tool.
There seems to be some referencing to it on the facebooks docs if you look closely and deep, but no one explains exactly how to actually make the connection, so this is my solution:
NSString *urlToLikeFor = facebookLike.titleLabel.text;
NSString *theWholeUrl = [NSString stringWithFormat:#"https://graph.facebook.com/me/og.likes?object=%#&access_token=%#", urlToLikeFor, FBSession.activeSession.accessToken];
NSLog(#"TheWholeUrl: %#", theWholeUrl);
NSURL *facebookUrl = [NSURL URLWithString:theWholeUrl];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:facebookUrl];
[req setHTTPMethod:#"POST"];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(#"responseData: %#", content);
If you look at the code I just take the url and puts two dynamic strings in the url, one with the object-url and one with the access token. I create a URLRequest and make it a POST request, and the response from facebook gets logged so one actually can see if the like go through or not.
There might be some performance improvements that can be done with the actual requests but I will leave it up to you if you see any slowdowns.
I'm still interested in other solutions but this is the one I will use for now.
We don't currently support Like through our Graph API.
What you can look through is something like this :
https://developers.facebook.com/docs/opengraph/actions/builtin/likes/
I’m not sure what initWithContentsOfURL does, but from the name I guess it tries to actually load content from a given URL(?).
You only have to give the URL as a text parameter – a URL is what represents an Open Graph object. Facebook will do the rest, scraping the page behind that URL and reading it’s OG meta tags, etc.
Maybe just this?
FBRequest *requestLike = [[FBRequest alloc]initForPostWithSession:[FBSession activeSession]
graphPath:#"me/og.likes"
graphObject:[NSURL URLWithString:facebookLike.titleLabel.text]];

GET and POST requests from ios5

I'm working on making a client for my REST service on the iPhone. I'm a little lost as to how I go about making the GET and POST requests. I make the url from a NSString, convert it to an NSURL and create the NSURLRequest based off of the url. After that I'm pretty lost. Also, sometimes I care about the response, other times I don't. For example, when making a request for a new id, I care about the response because it's the id I'll use to upload my file later, but when I upload the file I don't care because the server doesn't send a response.


Does anyone have some (hopefully)simple sample code that they could point me to / share?

What I have so far:
-(NSString *) makeGetRequest:(NSString *)url :(Boolean)careAboutResult
{
NSString *results = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSError *reqError;
NSURLResponse *response = nil;
if(careAboutResult == YES)
{
//get the result
}
return results;
}

In the code I'm testing with, the URL is
http://192.168.0.108:8081/TestUploadService/RestfulUpload.svc/id/test123_DOT_png
and I'm saying I do care about the result.

Any help would be greatly appreciated.
#nick its good you have created a NSURLRequest now you just need to create a connection to send this request and receive response, this request is GET request.
To make POST request you will need to use NSMutableURLRequest and set its method name and body content. Here in documentation you will find how you can do this.

Upload image from iphone to facebook without dialog

i want to send image from my iphone to facebook of my account without showing any dialog (in fbconnect there is dialog) . I want a simple way to just test whether it is uploaded in the facebook are not.
I tested with twitter api send message from my iphone and its work fine its few lines of code,
I pasted below
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://username:password#twitter.com/statuses/update.xml"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval: 60.0];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[[NSString stringWithFormat:#"status=%#", #"test message sends from iphone"] dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSLog(#"%#", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
Like wise i want to send text and photo to facebook account ? Is it possible ?
The fbconnect is more no of blocks and lines , can anyone help me ? a simple way to understand it ?
Thanks in advance
With the new OAuth Facebook API and the new facebook-ios-sdk I don't think you can do without the dialog. The whole point is that your app never sees and stores the user credentials. If you then close the app and the login dialog appears again a cookie (set/used by the UIWebView, AFAIK) logs you in without you needing to enter the credentials again.

Twitter profile image upload in objective-c

I want to upload an image to my twitter profile using objective-c. I saw in the twitter API that I need to send a HTML post to http://twitter.com/account/update_profile_image.format and send the picture as a parameter. I am done with the authentication. I am stuck with the uploading. Maybe somebody can help me with sending the picture as a parameter?
You should be using NSURLRequests and NSURLConnection to perform the API requests. If this is true, all you need to do is create an NSMutableURLRequest, set it's URL to the Twitter image upload API URL, set the method to POST.
Then you'll need to create an NSData object to represent your image, which you can do using
NSData *myImageData = [[NSData alloc] initWithData:[myImage CGImage]];
I don't know what the parameter name is for Twitter's upload API, so for arguments sake, lets call it "image". The next thing you need to do is set the image data as the request's body for the "image" parameter, like this
NSString *bodyString = [NSString stringWithFormat:#"image=%#", [[[NSString alloc] initWithData:myImageData encoding:NSStringUTF8Encoding] autorelease]];
[myRequest setBody:bodyString];
Then you can just start your NSURLConnection with the request and it should upload.
If you’ve managed to get started, then this post on CocoaDev should help you set the uploading up. There’s a sample linked at the top too.
I recommend using ASIHTTPRequest
What is ASIHTTPRequest?
ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.
It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The included ASIFormDataRequest subclass makes it easy to submit POST data and files using multipart/form-data.
See this blog post for an example
Somthing like this
// See http://groups.google.com/group/twitter-development-talk/browse_thread/thread/df7102654c3077be/163abfbdcd24b8bf
NSString *postUrl = #"http://api.twitter.com/1/account/update_profile_image.json";
ASIFormDataRequest *req = [[ASIFormDataRequest alloc] initWithURL:[NSURL
URLWithString:postUrl]];
[req addRequestHeader:#"Authorization" value:[oAuth oAuthHeaderForMethod:#"POST"
andUrl:postUrl andParams:nil]];
[req setData:UIImageJPEGRepresentation(imageView.image, 0.8)
withFileName:#"myProfileImage.jpg"
andContentType:#"image/jpeg" forKey:#"image"];
[req startSynchronous];
NSLog(#"Got HTTP status code from Twitter after posting profile image: %d", [req
responseStatusCode]);
NSLog(#"Response string: %#", [req responseString]);
[req release];

Unable to POST data from IPhone using google account authentication

I'm working on an IPhone application that works with a Google App Engine application. I manage to get logged by using a google account and I get the authentication token. I'm also able to GET data from the GAE service (I did it after reading another question written here) but now I need to POST data so I need to send the authentication token in the header of the POST request. I tried several options but none of them worked.
Here is the code I use to put that auth into the header:
NSString* urlStr = [NSString stringWithFormat:#"%#%#", HOST, url];
NSMutableURLRequest* urlPost = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
NSString* authStr = [NSString stringWithFormat:#"GoogleLogin auth=%#", token];
[urlPost addValue:authStr forHTTPHeaderField:#"Authorization"];
but it doesn't work.
Any help?
You need to use [request setHTTPMethod: #"POST"] and [request setHTTPBody: postdata] to properly configure the POST components. See the NSMutableURLRequest docs for more details.
Whenever I'm troubleshooting a problem related to HTTP, the first tool I'll grab is Charles HTTP Proxy. It will show you the entire request and response for closer examination.
If you're authenticating against an App Engine app, you need to obtain and send an authentication cookie, rather than using the GoogleLogin authentication. The source of appengine_rpc.py in the Python SDK demonstrates how.