Retrive a friend's profile image after loging in - facebook

I'm looking for idea on how to use the Facebook connection. I am able to used the Facebook connect on button click, to access the Facebook. The method I wrote:
-(void)login
{
NSMutableDictionary *params=[NSMutableDictionary dictionary];
[params setValue:apiString forKey:#"client_id"];
[_facebook setSessionDelegate:self];
[_facebook dialog:#"oauth" andParams:params andDelegate:self];
}
-(void)fbDidLogin
{enter code here
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: #"4", #"uids", #"name", #"fields", nil];
[_facebook requestWithMethodName: #"users.getInfo" andParams: params andHttpMethod: #"GET" andDelegate: self];
[_facebook requestWithGraphPath:#"me/friends" andDelegate:self];
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"received response");
}
- (void)request:(FBRequest *)request didLoad:(id)result// This method i am using to access all friends name and id after loging.
{
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
NSLog(#"%#" ,result);
}
Now my requirement is that with a friend's name I need to access their profile imag.
How can I get a friend's profile image?
I am using the JSON to access Facebook connect.

"/userid/picture" see also https://developers.facebook.com/docs/reference/api/user/

Related

How to attach data to Facebook app request on the iPhone?

I'm working on 'deep linking' a Facebook request into my iOS app. A Facebook app is setup and seems to work as I can send a request to a friend, the request badge appears in the friend's Facebook, and clicking on the request launches my app (all on the iPhone).
However, so far I could not pass any data with the request, which I want to use when my app launched from the Facebook app with the request.
I use the following call:
-(void) fbRequestActionWithMessage: (NSString *) message andLink: (NSString *) link
{
NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
#"data1", #"key1",
#"data2", #"key2",
nil];
NSString *requestDataString = [requestData JSONRepresentation];
NSMutableDictionary* params = [NSMutableDictionary
dictionaryWithObjectsAndKeys:
message, #"message",
#"Check this out", #"notification_text",
link, #"link",
requestDataString, #"data",
nil];
[facebook dialog:#"apprequests" andParams:params andDelegate:self];
}
Neither the "data" nor the "link" values in the params dictionary seem to have any effect. Ideally, when my app is launched from this request, I would get back the "data" or the "link" values. Can this be done?
I could not find any Facebook docs about the structure of params dictionary - is there a list of supported keys and their effect?
The "link" parameter and "notification_text" is not supported in iOS but you should be able to pass in data and get it back.
Example, passing in data:
FBSBJSON *jsonWriter = [FBSBJSON new];
NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
#"data1", #"key1",
#"data2", #"key2",
nil];
NSString *requestDataString = [jsonWriter stringWithObject:requestData];
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
message, #"message",
requestDataString, #"data",
nil];
[facebook dialog:#"apprequests"
andParams:params
andDelegate:self];
Example, reading it back:
....
#property (nonatomic, retain) NSURL *openedURL;
....
#synthesize openedURL = _openedURL;
....
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
self.openedURL = url;
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSession.activeSession handleDidBecomeActive];
if (FBSession.activeSession.isOpen) {
[self checkIncomingNotification];
}
}
- (void) notificationGet:(NSString *)requestid {
[FBRequestConnection startWithGraphPath:requestid
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
NSString *title;
NSString *message;
if ([result objectForKey:#"data"]) {
// Process data in request
FBSBJSON *jsonParser = [FBSBJSON new];
NSDictionary *requestData =
[jsonParser
objectWithString:[result objectForKey:#"data"]];
[NSString stringWithFormat:#"Badge: %#, Karma: %#",
NSString *data1 = [requestData objectForKey:#"key1"];
NSString *data2 = [requestData objectForKey:#"key2"];
}
}
}];
}
- (void) checkIncomingNotification {
if (self.openedURL) {
NSString *query = [self.openedURL fragment];
if (!query) {
query = [self.openedURL query];
}
NSDictionary *params = [self parseURLParams:query];
// Check target URL exists
NSString *targetURLString = [params valueForKey:#"target_url"];
if (targetURLString) {
NSURL *targetURL = [NSURL URLWithString:targetURLString];
NSDictionary *targetParams = [self parseURLParams:[targetURL query]];
NSString *ref = [targetParams valueForKey:#"ref"];
// Check for the ref parameter to check if this is one of
// our incoming news feed link, otherwise it can be an
// an attribution link
if ([ref isEqualToString:#"notif"]) {
// Get the request id
NSString *requestIDParam = [targetParams
objectForKey:#"request_ids"];
NSArray *requestIDs = [requestIDParam
componentsSeparatedByString:#","];
// Get the request data from a Graph API call to the
// request id endpoint
[self notificationGet:[requestIDs objectAtIndex:0]];
}
}
// Clean out to avoid duplicate calls
self.openedURL = nil;
}
}
You can find more details on this, using the latest SDK v3.1:
https://developers.facebook.com/docs/howtos/send-requests-using-ios-sdk/

Getting Facebook username for iOS

I'm using the following to get the Facebook username:
[facebook requestWithGraphPath:#"me" andDelegate:facebook];
from my understanding I then have to use something like the following request to
- (void)request:(FBRequest *)request didLoad:(id)result {
NSLog(#"Result: %#", result);
NSDictionary *userInfo = (NSDictionary *)result;
userName = [userInfo objectForKey:#"name"];
fb_id = [userInfo objectForKey:#"id"];
}
my question is how do I call this request method from another method?
thanks for any help
check my function below to fetch user data id,pic and name
- (void)fetchUserDetails {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"SELECT uid, name, pic FROM user WHERE uid=me()", #"query",nil];
[fb requestWithMethodName:#"fql.query"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
and in the delegate method you will log the name like this
- (void)request:(FBRequest *)request didLoad:(id)result
{
if ([result isKindOfClass:[NSArray class]] && [result count]>0) {
result = [result objectAtIndex:0];
}
if ([result objectForKey:#"name"])
NSString *userName = [result objectForKey:#"name"];
NSLog(#"%#",userName);
}

How to post images in facebook in iphone app?

could anyone pls help me to post fotos on facebook using objective c for iphone app.i tried it but its getting terminated when i check with iphone.its working properly in simulator.following is my code i used to post in fb.i use graph api for developing my app.
-(void)fbGraphCallback:(id)sender {
if ((fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0)) {
NSLog(#"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Facebook...I require you to be logged in & approve access before you can do anything useful....");
//restart the authentication process.....
[fbGraph authenticateUserWithCallbackObject:self
andSelector:#selector(fbGraphCallback:)
andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
[self.view addSubview:viewshare];
}
else {
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];
//imgPicture is my image view name
NSLog(#"the Data::%#\n",imgPicture.image);
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:imgPicture.image];
[variables setObject:graph_file forKey:#"file"];
[variables setObject:[NSString stringWithFormat:#"%#", txtComment.text] forKey:#"message"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"me/photos" withPostVars:variables];
NSLog(#"postPictureButtonPressed: %#", fb_graph_response.htmlResponse);
NSLog(#"Now log into Facebook and look at your profile & photo albums...");
txtComment.text=#" ";
[txtComment setHidden:YES];
[lblcmt setHidden:YES];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:#"Successfully posted...Now log into Facebook & look at your profile" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
[fbGraph release];
}
First I check if facebook(the facebook object) has a valid session:
if (![facebook_ isSessionValid]) {
permissions_ = [[NSArray arrayWithObjects:#"read_stream", #"publish_stream", #"offline_access",nil] retain];
[facebook_ authorize:permissions_];
}
When I can guaranty that i'm logged into facebook I post the image like this:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
image, #"picture",
message, #"message",
nil];
[facebook_ requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
Finally I check this methods in for being sure if the image post was succesful or not:
-(void)request:(FBRequest *)request didFailWithError:(NSError *)error {
//Error
}
-(void)request:(FBRequest *)request didLoad:(id)result {
//Succes
}

why fbdidlogin didn't call?

- (id)init {
if (self == [super init]) {
facebook = [[Facebook alloc] initWithAppId:kAppId];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
permissions = [[NSArray arrayWithObjects:
#"read_stream", #"user_birthday",
#"publish_stream", nil] retain];
[facebook authorize:permissions delegate:self];
}
[self login];
}
return self;
}
- (void)login {
if (![_session isConnected]) {
[self postToWall];
}
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
}
- (void)fbdidLogin {
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:#"FBAccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:#"FBExpirationDate"];
// User has logged in to Facebook, now get their userId from Facebook
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
-(void)postToWall
{
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary *actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Always Running",#"text",#"http://itsti.me/",#"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSDictionary *attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"Your Happiness!", #"name",
#"asda", #"caption",
#"asdf", #"description",
nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
[facebook dialog:#"stream.publish" andParams:params andDelegate:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
This is my methods for calling facebook dialog boxes.But every time when i click to score for publishing;The permission box came first.I need to give permission after that the publish dialog box comes.I want to give permission ones and i want it to save it;after saving it i don't wanna see the permission box ever again.How can i do it?What is wrong with my code?
edit:
My access token and expiration date is null i guess thats because of this.
A few things to check, you call the method login handling method "fbdidLogin" instead of fbDidLogin. This is case sensitive.
Make sure in your view controller header file that you added FBSessionDelegate as one of the protocols you support. That could be a reason fbDidLogin is not called. You could always add an NSLog to verify that the fbDidLogin method is reached.
You need to implement Facebook's Single-Sign-On feature.
http://developers.facebook.com/docs/guides/mobile/#ios
This link has the step-by-step guide for implementing SSO, if you follow it carefully your App will request for permission once, then it will never request permission again (until the user remove your App)
Note that, you will need to go to www.facebook.com and use the "Developer Tools" to create a new Facebook App, you will be the admin of the app, and you will have your AppID, you will need this too.

how to login some other user on facebook in your app?

i am publishing some story on the user's facebook wall in my app. its working fine but i wanna know if some other user wanna login in my app with other facebook account how can he do that. i mean how can i logout an existing user and allow other user to login. my code in AppDelegate is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
facebook = [[Facebook alloc] initWithAppId:#"app_id"];
NSArray* permissions = [[NSArray arrayWithObjects:
#"publish_stream", nil] retain];
[facebook authorize:permissions delegate:self];
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
and my code in DetailViewController is:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
[sTitle stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""], #"name",
[sSummary stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""], #"description",
[sLink stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""], #"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"XXX", #"api_key",
#"Share on Facebook", #"user_message_prompt",
attachmentStr, #"attachment",
nil];
[appDelegate.facebook dialog:#"stream.publish" andParams:params andDelegate:self];
once app is installed it asks for username and password for facebook. and then it publishes stories to only that user. what about if i wanna change user in my app. where should i write
[facebook logout:self];
You'll need to provide the options for login/ logout. This post will help you how to do it.