in iPhone App how to post Youtube video link using facebook graph Api to FaceBook Wall post?
You can try this :
-(IBAction)postMeFeedButtonPressed:(id)sender {
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];
[variables setObject:#"#Your Message" forKey:#"message"];
[variables setObject:#"#http://Your Youtube Link" forKey:#"link"];
[variables setObject:#"#This is the bolded copy next to the image" forKey:#"name"];
[variables setObject:#"#This is the plain text copy next to the image. All work and no play makes Jack a dull boy." forKey:#"description"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"me/feed" withPostVars:variables];
NSLog(#"postMeFeedButtonPressed: %#", fb_graph_response.htmlResponse);
//parse our json
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];
[parser release];
//let's save the 'id' Facebook gives us so we can delete it if the user presses the 'delete /me/feed button'
self.feedPostId = (NSString *)[facebook_response objectForKey:#"id"];
NSLog(#"feedPostId, %#", feedPostId);
NSLog(#"Now log into Facebook and look at your profile...");
}
Try this to post youtube video
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType options:#{ACFacebookAppIdKey : facebookAppKey, ACFacebookPermissionsKey : [NSArray arrayWithObjects:#"publish_stream", nil], ACFacebookAudienceKey : ACFacebookAudienceOnlyMe} completion:^(BOOL granted, NSError *error) {
if(granted ) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSDictionary *postDict = #{
#"link": youtubeurl,//URL of youtube video
};
ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/feed"]
parameters:postDict];
[facebookRequest setAccount:facebookAccount];
[facebookRequest performRequestWithHandler:^( NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.view setUserInteractionEnabled:true];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Posted Successfully on Facebook" message:#"" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
});
}];
} else {
NSLog(#"Error %#", error.description);
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Account not found" message:#"Please log in to Facebook account from Settings" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
});
}
}];
Related
I add the function working well on iOS6, but with iOS7, I had to update Facebook SDK because id and setId from the old ones are marked as non-public and are'nt validated.
So, I replaced the old SDK by the new one but, of course, the post image is no more working (as the id is certainly unknown).
I had this :
[params setObject:textViewMessageFacebook.text forKey:#"message"];
[params setObject:UIImagePNGRepresentation(screenshot) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/photos", idAmiS]
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
but don't know how to correct it to have it working...
Thanks
Use SlComposer Controller to post image on facebook.
1) first you add the Social framework and Account framewok into your project and then try below code
writ this code where you want...
{
ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:#{ACFacebookAppIdKey: #"131740927031850", ACFacebookPermissionsKey: #[#"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
ACAccountCredential *fbCredential = [_facebookAccount credential];
accessToken = [fbCredential oauthToken];
NSLog(#"Facebook Access Token: %#", accessToken);
NSLog(#"Success");
[self facebook];
}else{
NSLog(#"Fail");
NSLog(#"Error: %#", error);
}
}];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[tweetSheet setInitialText:#"posting from my own app! :)"];
[tweetSheet addURL:[NSURL URLWithString:#"www.xyz.com"]];
// Here you add the image
[tweetSheet addImage:[UIImage imageNamed:#"yourimage.png"]];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)facebook
{
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
NSLog(#"URl:-%#",meurl);
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Parsed Data:-%#", meDataString);
}];
}
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];
In my iphone app i want to post photos to selected facebook contacts. I'm getting that photos from phone library.
I'm using this code:
fbString=[NSString stringWithFormat:#"%#\n%#",appdelegate.titleString,appdelegate.descriptionString];
NSArray *permissions =[NSArray arrayWithObjects:#"publish_actions",#"publish_stream",#"manage_friendlists", nil];
NSData* imageData = UIImageJPEGRepresentation(self.img, 90);
NSLog(#"imageData %#",imageData);
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:fbString, #"message",imageData,#"source",nil];
NSLog(#"fbid count %d",fbidarray.count);
[[FBSession activeSession] reauthorizeWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error)
{
if (!error) {
for(int j=0;j<fbidarray.count;j++)
{
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/photos",[fbidarray objectAtIndex:j]] parameters:params HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error1)
{
NSLog(#"errors in the view %#",error1);
if(!error1 )
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Success"
message:[NSString stringWithFormat:#" Sucessfully posted "]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}];
}
}
}];
You can try this :
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) {
[self hideHUD];
NSLog(#"Error: %#", [error localizedDescription]);
} else {
// Success
}
}];
[postTest release];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
photoDescription, #"message",
image, #"image",
nil];
[facebook requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
This is what I did to upload a image to Facebook. The image is uploaded successfully to FaceBook 'photos'. But I want to post the image to my FaceBook Feed. So i tried,
[facebook requestWithGraphPath:#"me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
but it still the images posted to the 'photos'. It does not appear in the Feed...
I searched and used different methods for a solution, but I couldn't find anything helpful...
Not sure what your params looks like, but try this..
UIImage *image = ...// some image.
NSData *imageData= UIImagePNGRepresentation(image);
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"some message", #"message", imageData,#"source", nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
UIImage* image=...;
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
NSArray *permissions =
[NSArray arrayWithObjects:#"publish_actions",#"publish_stream", nil];
[[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {}];
}
else
{
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"Photo post test..." forKey:#"message"];
[params setObject:UIImagePNGRepresentation(image) forKey:#"picture"];
//for not allowing multiple hits
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Post Failed"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
else
{
//showing an alert for success
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Post success"
message:#"Shared the photo successfully"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
}
What could this error mean?
[__NSCFNumber length]: unrecognized selector sent to instance 0x6d21350
Here is my code:
NSString *urlString = #"http://api.twitter.com/1/statuses/update.json";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:status forKey:#"status"];
[params setObject:replyToID forKey:#"in_reply_to_status_id"];
[params setObject:#"1" forKey:#"include_entities"];
// Build the request with our parameter
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodPOST];
// Attach the account object to this request
[request setAccount:twitterAccount];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData) {
// inspect the contents of error
NSLog(#"%#", [error localizedDescription]);
self.alert = [[UIAlertView alloc] initWithTitle:#"HTTP error" message:#"I could not connect to the Twitter API." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[self.alert show];
[self.replyDelegate replyRequestSuccessful:NO];
}
else {
/*NSString *responseDataAsString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(responseDataAsString);*/
NSError *error;
NSArray *replyResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
if (!replyResponse) {
NSLog(#"%#", [error localizedDescription]);
self.alert = [[UIAlertView alloc] initWithTitle:#"JSON error" message:#"I could not parse the JSON response from the Twitter API." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[self.alert show];
[self.replyDelegate replyRequestSuccessful:NO];
}
else {
[self.replyDelegate replyRequestSuccessful:YES];
}
}
}];
I tried debuggin, and it dies once it enters the performRequestWithHandler. It goes the else block and dies with the error above.
It means that you are passing an NSNumber where the called code expects an NSString or some other object that has a length method. You can tell Xcode to break on exceptions so that you see where exactly the length method gets called.
Specifically this line of code:
[params setObject:replyToID forKey:#"in_reply_to_status_id"];
the replyToID is not a String or has a method length. If you don't have this, or if you convert the replyToID to a String before setting it in params it should work.