Getting user_likes in Facebook iOS SDK - ios

I'm trying to get the current user's likes after they have signed into my iOS app using Facebook. The only things I can get from the user are their profile (gender, locale, etc).
How would I go about requesting permission to get their user_likes, after they have logged in?
Also, would I have to get my app reviewed by the Facebook team? I've read somewhere that I may have to.

First of all you need to provide login permission for user_likes and if others then as follows:
self.LoginView.readPermissions = #[#"basic_info",#"user_location",#"user_birthday",#"user_likes",#"email",#"user_relationships",#"user_interests",#"user_hometown",#"user_about_me",#"user_games_activity",#"user_relationship_details",#"user_religion_politics"];
self.LoginView.delegate=self;
There after you need to call webService using graph Api by simply calling the method ( [self obtainUserData]) which is mentioned as follows:
-(void)obtainUserData
{
NSString *fbAccessToken = [FBSession activeSession].accessTokenData.accessToken;
NSString *fields =#"id,name,first_name,last_name,picture,permissions,installed,music,likes,email,friends,gender,birthday,email,hometown,relationship_status,interested_in,sports";
// feed is use for all action
NSString *path = [NSString stringWithFormat:#"https://graph.facebook.com/me?fields=%#",fields];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#&access_token=%#", path,fbAccessToken]];
NSLog(#"%#",url);
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:req delegate:self];
// NSMutableData *jsondata=[[NSMutableData alloc]init];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData = data;
[conn start];
}
//here receivedData is an object of NSMutable Data
you will obtain your required output in following method:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableDictionary *json=[NSJSONSerialization JSONObjectWithData:_receivedData options:NSJSONReadingMutableContainers error:nil];
NSLog(#"json: %#",json);
}
HopE this Helps!!

Related

Google Contacts API version 3.0 to retrieve all contacts for objective C (iOS) sample request format?

In my application user logs in throgh google+ credentials for which I have implemented google+ framework for authentication/authorization. After Successful login I have to get the contacts list of the logged in user.
for which I am using Google Contacts API version 3.0 url https://www.google.com/m8/feeds/contacts/default/full
After GPP Sign In authentication request..
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error) {
NSLog(#"%#",[NSString stringWithFormat:#"Status: Authentication error: %#", error]);
return;
}
auth.clientID =#"XXXX.apps.googleusercontent.com";
auth.clientSecret =#"xxxxxxxx-z7tmOqQCShgH3ax";
NSString *urlStr = #"https://www.google.com/m8/feeds/contacts/default/full";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"3.0" forHTTPHeaderField:#"GData-Version"];
auth.scope= #"https://www.googleapis.com/auth/contacts.readonly";
[auth authorizeRequest:request
completionHandler:^(NSError *error) {
NSString *output = nil;
if (error) {
output = [error description];
} else {
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (data) {
// API fetch succeeded :Here I am getti
output = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"%#", output);
} else {
// fetch failed
output = [error description];
}
}
}];
}
in the successful response I have written code like this.
But the output is not as desired.
The output string is huge junk value and at the end of it is showing
401. That's an error. There was an
error in your request. That's all we
know.
Could any one please help in resolving the issue.
401 means that you have not correctly authorized. I don't work with that api but it's possible your credentials are incorrect.
you might also be seeing certificate problems for the https? you should set up this call back and see if it provides more info.
How to use NSURLConnection to connect with SSL for an untrusted cert?

Twitter iOS GET statuses/home_timeline using Parse

This will be my first iPhone app and I am running into difficulties that I thought I'd be able to find a tutorial on, but alas.... nothing on the inter webs that I can find.
I'm trying to get an initial 20 or so tweets from a signed in user (signed in using Parse's Twitter authentication) and I can't seem to get it to work. I put a breakpoint in when I get an error and this is what it shows:
I'm contemplating abandoning Parse all together and doing my own authentication using https://github.com/nst/STTwitter but wanted to quickly see if there was a way to simply do what I am trying to. The code is question is at this github: https://github.com/johndangerstorey/twiz and outlined below as found in my MyLoginViewController.m file:
NSString *bodyString = [NSString stringWithFormat:#"https://api.twitter.com/1.1/statuses/home_timeline.json?screen_name=johnDANGRstorey"];
// Explicitly percent-escape the '!' character.
bodyString = [bodyString stringByReplacingOccurrencesOfString:#"!" withString:#"%21"];
NSURL *url = [NSURL URLWithString:bodyString];
NSMutableURLRequest *tweetRequest = [NSMutableURLRequest requestWithURL:url];
tweetRequest.HTTPMethod = #"GET";
tweetRequest.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[[PFTwitterUtils twitter] signRequest:tweetRequest];
NSURLResponse *response = nil;
NSError *error = nil;
// Post status synchronously.
NSData *data = [NSURLConnection sendSynchronousRequest:tweetRequest
returningResponse:&response
error:&error];
// Handle response.
if (!error) {
NSLog(#"Response: %#", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else {
NSLog(#"Error: %#", error);
}
Thanks for your reply and help.
I figured it out, GET requests don't require .body or .method requests so I just removed
tweetRequest.HTTPMethod = #"GET";
tweetRequest.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
and was golden.

iOS Facebook get user's profile image randomly opens profile homepage instead of permission dialog

We're trying to get the user's profile image URL. We are experiencing random behavior. Sometimes the permission dialog is opened in the browser as expected but sometimes the browser opens with the users homepage. any ideas why?
NSArray *permissions = [[NSArray alloc] initWithObjects:#"email",#"publish_actions", nil];
FBSession* session;
session = [[FBSession alloc] initWithAppID:#"xxx" permissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone urlSchemeSuffix:nil tokenCacheStrategy:nil];
[FBSession setActiveSession:session];
// get image
NSString *path = [[NSString alloc] initWithFormat:#"%#/picture?redirect=false&width=170&height=170",fuid];
[FBRequestConnection startWithGraphPath:path completionHandler:^]
This is how I would collect the User Profile picture, although there are plenty of ways to do this:
I would use a notification (or any other method) to notify once the user is signed in, then call a method to collect the Facebook Profile picture. Once they are logged in you can pass their facebook Id to a value. (kUserFacebookIDKey)
NSLog(#"Downloading user's profile picture");
// Download user's profile picture
NSURL *profilePictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large", self.kUserFacebookIDKey];
NSURLRequest *profilePictureURLRequest = [NSURLRequest requestWithURL:profilePictureURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; // Facebook profile picture cache policy: Expires in 2 weeks
[NSURLConnection connectionWithRequest:profilePictureURLRequest delegate:self];
Note: You would need to update your header to <NSURLConnectionDataDelegate> and add a property for NSMutableData *_data;
Then handle the response:
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[TESTUtility processFacebookProfilePictureData:_data];
}
Then you can do what you wish with the file. Typically I cache the file and then check if the cached file matches the new and so on...

Need alternative to Facebook Open Graph og.likes action for Facebook page

While attempting to publish a like connection between a Facebook user and a Facebook page (using Facebook's Open Graph og.likes action type) within an iOS app I received the following error:
{"error":{"message":"(#100) Like actions are not yet supported against objects of this type.","type":"OAuthException","code":100}}
After doing a little research I found that open graph like actions can not be published against Facebook pages. My understanding is that my only other option would be to embed the standard like button with a UIWebView.
My question:
Is there anything else I can do to create a similar or meaningful open graph connection between a Facebook user and a Facebook page without using a UIWebView?
Below is some of my code, I successfully used it to publish a like against a non-Facebook page to verify the issue wasn't something else:
if(isFacebookServiceAuthorized)
{
NSLog(#"Facebook service is authorized");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [defaults valueForKey:#"kSHKFacebookAccessToken"];
id userInfo = [defaults valueForKey:#"kSHKFacebookUserInfo"];
NSString *userID = [userInfo objectForKey:#"id"];
NSLog(#"Access Token: %# and userid: %#", accessToken, userID);
NSString *devID = FACEBOOK_DEV_USERID
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/og.likes", devID]];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setURL:url];
[request1 setHTTPMethod:#"POST"];
NSString *body=[NSString stringWithFormat:#"access_token=%#&object=OBJECT_TO_LIKE", accessToken];
[request1 setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"string..:%#",string);
}

IOS how to POST GET DELETE PUT Rest Api

I am building my App conected to a Rest API and until now I only made a GET request with the following code :
//Start login process
NSString *emailstring = email.text;
NSString *passstring = pass.text;
// Create the URL from a string.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.myserver.com/Rest/API/users?format=json&email=%#&password=%#",emailstring,passstring]];
NSLog(#"%#",url);
// Create a request object using the URL.
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// Prepare for the response back from the server
NSHTTPURLResponse *response = nil;
NSError *error = nil;
// Send a synchronous request to the server (i.e. sit and wait for the response)
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"Reponse from web:%#", response);
// Check if an error occurred
if (error != nil) {
NSLog(#"%#", [error localizedDescription]);
// Do something to handle/advise user.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login error"
message:#""
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
else {
// Convert the response data to a string.
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// View the data returned - should be ready for parsing.
NSLog(#"%#", responseString);
// Add data to a Plist file for next time
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"login.plist"];
NSArray *values = [[NSArray alloc] initWithObjects:emailstring,passstring,#"IDtest",nil];
[values writeToFile:path atomically:YES];
[values release];
[self dismissModalViewControllerAnimated:YES];
}
This code work fine just for a GET request. I saw it is there a lot of framework (e.g RestKit, ....). But I am getting a bit lost with other request ! So what is the best solution to make POST DELETE PUT request for an IOS App ?
It's similar code, but using the class NSMutableRequest. You can set the httpbody and other parameters to communicate with the server.
check the documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html
to post something, just put setHTTPMethod:#"POST"and assign the data to post using setHTTPBody:

Resources