How to post message on Facebook friends wall in iPhone - iphone

How to post message on Facebook friend's wall using NEWFacebook SDK selecting target_id of friend.

NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];
[variables setObject:[NSString stringWithFormat:#"Hi."] forKey:#"message"];
[variables setObject:#"http://icon.png" forKey:#"picture"]; //http://tinyurl.com/45xy4kw
[variables setObject:#"Create post" forKey:#"name"];
[variables setObject:#"Write description." forKey:#"description"];
[_facebook requestWithGraphPath:[NSString stringWithFormat:#"/%#/feed",facebook_user_id]
andParams:variables
andHttpMethod:#"POST"
andDelegate:self];

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"https://developers.facebook.com/docs/reference/dialogs/",#"link",
#"Facebook Dialogs",#"name",
#"Reference Documentation",#"caption",
#"Using Dialogs to interact with users.",#"description",
#"Facebook Dialogs are so easy!",#"message",
nil];
// [facebook requestWithGraphPath:#"me/feed" andParams:params andDelegate:self];
//[facebook requestWithMethodName:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
[facebook requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];

check delegate methodes
-(void)fbDidLogin
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[facebook requestWithGraphPath:#"me/picture" andDelegate:self];
NSLog(#"Login Success with :%# %#",facebook.accessToken,facebook.expirationDate);
}
-(void)fbDidLogout
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"])
{
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
NSLog(#"Logout Success");
}
-(void)request:(FBRequest *)request didLoad:(id)result
{
if(c==0)
{
NSData *data = [NSData dataWithData:(NSData*)result];
UIImage *profilePic = [[[UIImage alloc] initWithData:data] autorelease];
image1.image=profilePic;
[self postWall];
// NSLog(#"response is %#", result);
// NSString *email =[result objectForKey:#"name"];
// NSString *userFbId =[result objectForKey:#"id"];
// NSLog(#"%#",email);
// NSLog(#"%#",userFbId);
c=1;
}
else
{
NSLog(#"%#",result);
NSLog(#"posted!!") ;
}
}
-(void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
NSLog(#"Failed with error: %#", [error localizedDescription]);
}

Related

how to send request multiple friends in fbprofileviewcontroller

My selected fb id's
arryOfFBIds = [[NSMutableArray alloc] init];
for (id<FBGraphUser> user in self.friendPickerController.selection)
{
[arryOfFBIds addObject:user.id ];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: arryOfFBIds forKey: #"SelectedFBIds"];
[defaults synchronize];
NSLog(#"array of fb id:%#",arryOfFBIds);
}
and send request in multiple friends
NSMutableDictionary* params2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:#"Invite to on Ensmbl", #"description", #"http://placebogames.com/", #"link",[arryOfFBIds componentsJoinedByString:#","],#"to", nil];
[FBWebDialogs presentFeedDialogModallyWithSession:[FBSession activeSession] parameters:params2 handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{
NSLog(#"error===>%#,resultURL===>%#",error,resultURL);
} ];
but it is not working .only one friend request has been send .
how to send multiple friend request.

User defaults not saving

I just built my first app and there is only one thing left to do. I am getting data from a url. If that URL fails or is nil then I would like to get the data from the User Defaults.
I have a function that saves the user defaults from the data off the URL
- (void)saveToUserDefaults
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:#"data"];
[defaults synchronize];
}
and here is some more of my code.
NSURL *url = [NSURL URLWithString:#"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
data = [NSData dataWithContentsOfURL:url options:0 error:nil];
[self saveToUserDefaults];
if(url == nil){
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
data = [defaults dataForKey:#"data"];
}
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
I disconnected from the internet and ran my app in a simulator and my app returned empty values.
What can I do to fix this?
Try this for your scenario
// method used to check network is available or not
- (BOOL)isNetworkAvailable
{
BOOL connected;
const char *host = "www.apple.com";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
SCNetworkReachabilityFlags flags;
connected = SCNetworkReachabilityGetFlags(reachability, &flags);
isConnected = NO;
isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
return isConnected;
}
}
now try to check
NSURL *url = [NSURL URLWithString:#"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
data = [NSData dataWithContentsOfURL:url options:0 error:nil];
bool networkStatus = [self isNetworkAvailable];
if(networkStatus){
[self saveToUserDefaults];
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
data = [defaults objectForKey:#"data"];
}
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
try this
- (void)saveToUserDefaults
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:#"data"];
}
NSURL *url = [NSURL URLWithString:#"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
data = [NSData dataWithContentsOfURL:url options:0 error:nil];
[self saveToUserDefaults];
if(url == nil){
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
data = [defaults objectForKey:#"data"];
}
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Try this one.
NSURL * url = [NSURL URLWithString:#"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSOperationQueue * newQueue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:newQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSArray * array = nil;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (error == nil) {
array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[defaults setObject:array forKey:#"data"];
} else {
array = [defaults objectForKey:#"data"];
}
NSLog(#"%#", array);
});
}];

Posting an image to facebook in iPhone

Hi I am using this piece of code to post an image to facebook
UIImage *image_LocalSharePhoto = [[UIImage alloc]initWithData:sharedSingleton.dataResultImage];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"Put Captions On Pics", #"name",
#"Put Captions On Pics", #"caption",
image_LocalSharePhoto,#"picture",
#"I created this fun photo using a FREE app http://bit.ly/CapsPics. #PutCapsOnPics via #PutCapsOnPics", #"description",
nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"532872463436844", #"api_key",
#"Share on Facebook", #"user_message_prompt",
attachmentStr, #"attachment",
nil];
[facebook dialog: #"feed" andParams:params andDelegate:self];
It opens up facebook page but do not show anything to share or post. Can some one help. Thanks!
if you are using facebook's api , I am able to share the images on user's timeline using following code ... Please review the code if it can help ..
include
// aryFetchedImages = Array of images which needs to be posted on Facebook
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
for (int i = 0; i < aryFetchedImages.count ; i++)
{
[params setObject:UIImageJPEGRepresentation([aryFetchedImages objectAtIndex:i], 50.0f) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
#ifdef DEBUG
NSLog(#"Unable to share the photo please try later - Fail %#",error);
#endif
}
else
{
//showing an alert for success
#ifdef DEBUG
NSLog(#"Photo %# uploaded on Facebook Successfully",UIImageJPEGRepresentation([aryFetchedImages objectAtIndex:i], 50.0f));
#endif
}
}];
}
if you are using old facebook Method you can wall post like bellow Methid:-
NSArray* permissions = [NSArray arrayWithObjects:#"publish_stream", #"user_photos", nil];
[facebook authorize:permissions];
now to Post image At your Button Click or your logic like:-
-(IBAction)ActionPostWallPhoto
{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
ImageView.image, #"picture",
nil];
[[objAppdele facebook] requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
}
and if you are using new facebook API then you can use bellow Method:-
NSMutableDictionary *postTest = [[NSMutableDictionary alloc]init];
[postTest setObject:[NSString stringWithFormat#"Your message"] forKey:#"message"];
[postTest setObject:#"" forKey:#"picture"];
[postTest setObject:#"" forKey:#"name"];
[postTest setObject:#"APPID" forKey:#"app_id"];
NSString *userFBID =[NSString stringWithFormat:#"%#",[allInfoDict objectForKey:#"Friends facebook ID "]];
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/feed?access_token=%#",userFBID,[appDelegate.mysession accessToken]]
parameters:postTest
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
NSDictionary * result,
NSError *error) {
if (error) {
NSLog(#"Error: %#", [error localizedDescription]);
} else {
// Success
}
}];
[postTest release];

Facebook integration showing alert address is invalid

I am new in iPhone application development. Now i am developing application for iPhone In this application I need to upload video to Facebook. i am used below code. Now enter Facebook the user name and password. showing error message alert. like safari cannot open the page because the address is invalid.
- (void)viewDidLoad
{
facebook = [[Facebook alloc] initWithAppId:#"Facebook_App_Id"];
//#"com.facebook.samples.VideoUploadTest"];
facebook.sessionDelegate = self;
}
- (IBAction)buttonClicked:(id)sender {
NSArray* permissions = [[NSArray alloc] initWithObjects:
#"publish_stream", nil];
[facebook authorize:permissions delegate:self];
[permissions release];
}
- (void)fbDidLogin {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"mov"];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, #"video.mov",
#"video/quicktime", #"contentType",
#"Video Test Title", #"title",
#"Video Test Description", #"description",
nil];
[facebook requestWithGraphPath:#"me/videos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
referred from this url [http://developers.facebook.com/blog/post/532/]
Refer this for SSO settings.
http://developers.facebook.com/docs/mobile/ios/build/

How to post on Facebook a photo with tags using ASIHTTPRequest

I am posting a photo on Facebook from my iPhone app using the Graph API and ASIFormDataRequest.
Everything works fine (photo is uploaded with the right caption). When I try to add tags, photo is still uploaded but with no tag.
Here is how I prepare the data
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/photos",userID]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addFile:filePath forKey:#"file"];
[request setPostValue:caption forKey:#"message"];
[request setPostValue:fbManager._facebook.accessToken forKey:#"access_token"];
[request setAllowCompressedResponse:NO];
[request setTimeOutSeconds:60];
//Add tags
NSMutableArray *tagArray=[[NSMutableArray alloc] init];
for(FacebookTag *aTag in aJob.tags) {
NSNumber *_cordX=[NSNumber numberWithFloat:((aTag.x/aJob.image.size.width)*100.0)];
NSNumber *_cordY=[NSNumber numberWithFloat:((aTag.y/aJob.image.size.height)*100.0)];
NSString *cordX=[_cordX stringValue];
NSString *cordY=[_cordY stringValue];
NSMutableDictionary* tag = [NSMutableDictionary dictionaryWithObjectsAndKeys:
aTag.userId, #"to",
cordX, #"x",
cordY, #"y",
nil];
[tagArray addObject:tag];
}
NSData *tagData = [NSKeyedArchiver archivedDataWithRootObject:tagArray];
NSString* tagString = [[NSString alloc] initWithData:tagData encoding:NSUTF8StringEncoding];
[request setPostValue:tagString forKey:#"tags"];
Try this code
// Upload photo
...
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
newPhoto, #"picture", nil];
[self.facebook requestWithGraphPath:#"me/photos" andParams:params
andHttpMethod:#"POST" andDelegate:self];
...
// Response received. Photo upload successful, so add tags
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([request.params objectForKey:#"picture"]) {
// Get photo ID from result
NSString* photoId = (NSString*)[result objectForKey:#"id"];
NSMutableDictionary* params = nil;
for (int i = 0; i < [tagsList count]; i++) {
// tagsList contains user IDs to tag
NSString* uid = (NSString*)[tagsList objectAtIndex:i];
params = [NSMutableDictionary dictionaryWithObjectsAndKeys:uid, #"to",
nil];
NSString* graphUrl = [NSString stringWithFormat:#"%#/tags", [photoId stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
[self.facebook requestWithGraphPath:graphUrl
andParams:params andHttpMethod:#"POST" andDelegate:self];
}
}
...
}
It is using FBGraph API