Post to the users Facebook wall from an iOS app - objective-c

I know this has been asked a lot here, but none of the answers seem to work for me :(
I'm trying to post text to the users Facebook wall from my iPhone app. I already have the Facebook delveloper stuff set up, and almost everything is working. Almost.
facebook = [[Facebook alloc] initWithAppId:#"123813234381280"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray* permissions = [[NSArray arrayWithObjects:
#"publish_stream", nil] retain];
[facebook authorize:permissions delegate:self];
}
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"123813234381280", #"app_id",
#"http://test.com/", #"link",
#"Test-Name", #"name",
//#"Reference Documentation", #"caption",
#"Download the app NOW from the App Store", #"description",
#"Test-Message", #"message",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
Now there appears a dialog, but the textfield where it should say "Test-Message" in it is empty. You know, the one where it says what to post on the wall. Everything else works, the description, the link, the name. Everything besides the message.
Can anybody help me?

Facebook is now ignoring the message parameter:
http://developers.facebook.com/blog/post/510/
message : This field will be ignored on July 12, 2011 The message to
prefill the text field that the user will type in. To be compliant
with Facebook Platform Policies, your application may only set this
field if the user manually generated the content earlier in the
workflow. Most applications should not set this.

Related

How can I add 'Follow us on facebook' button to an iOS app?

I've about completed my iOS app but only need to add two buttons:
Follow us on facebook
Follow us on twitter
I assumed I would find a couple of simple examples here, but was surprised to find no answer to this at all (yet).
I'm pretty sure I could spend the next few hours trying to figure it out, but thought I'd reach out for a little time-saving help.
Just looking for the code to go behind a button on a view controller (XCode 4.5.1, iOS 6).
I assume the only variable I might need to supply is the company's facebook account name.
Any suggestions? (Thanks in advance!)
First, the URL schem for Facebook: fb://profile/<yourpageid> (source). A URL with this structure will open the Facebook app, if it is installed.
More on iOS URL schemes.
When your button is tapped, you can check if the Facebook is installed:
-(IBAction)fbButtonTap:(id)sender {
NSURL *fbURL = [[NSURL alloc] initWithString:#"fb://profile/<yourpageid>"];
// check if app is installed
if ( ! [[UIApplication sharedApplication] canOpenURL:fbURL] ) {
// if we get here, we can't open the FB app.
fbURL = ...; // direct URL on FB website to open in safari
}
[[UIApplication sharedApplication] openURL:fbURL];
}
For twitter, you follow the same basic steps. The Twitter URL scheme for iOS is twitter://user?id=12345 or twitter://user?screen_name=yourname (source). Again, if the Twitter app is not installed, you have open the twitter profile in safari.
As for taking direct actions, I do not think you can do that, since there is no inherent knowledge about any other applciation installed on the device. The best I think you can do is direct users to each respective account.
You could use SLRequest have someone follow you. This will only work on iOS 6 though, on iOS 5 Twitter works but not Facebook.
An example of SLRequest for someone to follow you on Twitter, make sure this is called on the background thread:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"Your twitter name" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://api.twitter.com/1/friendships/create.json"] parameters:tempDict];
[followRequest setAccount:twitterAccount];
[followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
NSLog(#"%#", output);
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request failed
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show success
});
}
}];
}
}
}];
And for Facebook you just have to change some of it and look at their API and change the request URL.
this is my code for show people to like the community page of my app.
you can add the _webview where you want to show inside the code.
facebook gives the code for showing the page inside webview.
-(void)likeus
{
_webview =[[UIWebView alloc] initWithFrame:CGRectMake(14,94, 292, 250)];
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Load web view data
NSString *strWebsiteUlr =#"http://www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fenbuyukkim&width=292&height=258&show_faces=true&colorscheme=dark&stream=false&border_color&header=false&appId=433294056715040";
// Load URL
//Create a URL object.
NSURL *url = [NSURL URLWithString:strWebsiteUlr];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[_webview loadRequest:requestObj];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:_webview
action:#selector(removeFromSuperView)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(270.0, 80.0, 30.0, 30.0);
[button setBackgroundImage:[UIImage imageNamed:#"cross.png"] forState:UIControlStateNormal];
[_webview addSubview:button];
}
I am not sure if this is what you are looking for, but I think I found the solution, here at stack overflow on another thread.
Take a look at this thread:
Adding the Facebook Like Button in an iPhone App
Hope that helps you!

How to tag users in a photo using the Facebook iOS SDK?

I can't seem to work out how to tag users in a Facebook photo upload.
The documentation seems to suggest that you use an array, but the following code doesn't parse correctly (causes an application crash)
- (void)uploadImage:(UIImage *)img
withTags:(NSArray *)tags
{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"msgstring", #"message",
img, #"picture",
nil];
if (tags) {
[params setObject:tags
forKey:#"tags"];
}
self.requestType = FBAssistantRequestImageUpload;
[self.facebook requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
It works fine without the tags. The array at the moment contains a single string with the identifier of the friend I wish to tag.
I assume I'm adding the tags incorrectly. I was hoping to avoid having to use the three-step method outlined here: Tag Friends in Facebook Photo Upload, as I believe that requires photos permission, which just posting the photo doesn't need.
here's the code I use to tag friends on photos:
NSMutableArray *tags = [[NSMutableArray alloc] init];
NSString *tag = nil;
if(self.selectedFriends != nil){
for (NSDictionary *user in self.selectedFriends) {
tag = [[NSString alloc] initWithFormat:#"{\"tag_uid\":\"%#\"}",[user objectForKey:#"id"] ];
[tags addObject:tag];
}
NSString *friendIdsSeparation=[tags componentsJoinedByString:#","];
NSString *friendIds = [[NSString alloc] initWithFormat:#"[%#]",friendIdsSeparation ];
[params setObject:friendIds forKey:#"tags"];
}

Facebook graph API : audio share

Can I share audio using facebook graphic API
Can I directly upload audio on facebook server like video?
Can I share audio link on facebook and it will show embedded player on facebook?
I have tried this solution ios Facebook Graph API - post Audio file , but it doesn't work as expected
What I tried is to share audio link http://bit.ly/Ok4ZX6 but show it on facebook like a normal link not with embedded player http://i.stack.imgur.com/Ld67c.png
Edit
Code I have use :
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:4];
[params setObject:text forKey:#"message"];
[params setObject:title forKey:#"name"];
[params setObject:fileUrl forKey:#"source"];
[facebook requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
Also used this code too :
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:4];
NSString *attachment = [NSString stringWithFormat:#"{'media': [{'type': 'mp3', 'src': '%#', 'title': '%#'}], 'messgae': 'Messgae Messgae Messgae', 'name': 'Name Name Name', 'href': '%#'}",amazon_link, title, link];
[params setObject:attachment forKey:#"attachment"];
[params setObject:text forKey:#"message"];
[params setObject:#"stream.publish" forKey:#"method"];
[facebook requestWithParams:params andDelegate:self];
What solution from that link did you try? Post your own code. I also noticed that your audio link doesn't work, atleast not for me.
I think the correct way is to use the "source" field in the graph api, not "link".
This is a good page on the developer reference:
http://developers.facebook.com/docs/reference/api/post/
To upload a video using the graph API you can do this. You also need to be authorized to the publish_stream permission.
NSString * file = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mov"];
NSData * video = [NSData dataWithContentsOfFile:file];
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"title", #"title",
#"description", #"description",
video, #"video.mov",
#"video/quicktime", #"contentType",
nil];
[facebook requestWithGraphPath:#"me/videos"
andParams:dict
andHttpMethod:#"POST"
andDelegate:self];
Soundcloud use og:video tags to show the embedded player. The video tags accept SWF files, so it's possible to load up an interactive audio player widget. Try using the OG Debugger to see the tags.
Currently, this seems like the only strategy to show a player when content is shared.
Try like this.. Its working for me..
NSMutableDictionary *variables=[[NSMutableDictionary alloc]init];
//Posting audio file
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Dookudu" ofType:#"mp3"];
FbGraphFile *file=[[FbGraphFile alloc]initWithData:[NSData dataWithContentsOfFile:filePath]];
[variables setObject:file forKey:#"file"];
FbGraphResponse *response=[fbgraph doGraphPost:#"me/videos" withPostVars:variables];
NSLog(#"response %#",response.htmlResponse);

No results returned when doing Facebook Query

I'm trying to get a list of aid (album ids) using a Facebook Query.
What Works:
Running the query from the browser returns results, but when I run it using Facebook Connect no values are returned.
The problem I'm having is specific the album object, I get results when querying the user table.
The only real difference I can think of is when running from the browser a access_token parameter is included, running from browser without this returns nothing.
Authentication:
I am using OAuth to authenticate as outlined here: Mobile Apps - Getting Started
Do I need to include the access_token in the parameter list when doing a query?
The Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
facebook = [[Facebook alloc] initWithAppId:#"<myAppId>"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil delegate:self];
}
NSArray* permissions = [[NSArray arrayWithObjects:
#"email", #"read_stream", nil] retain];
[facebook authorize:permissions delegate:self];
NSString *fql = [NSString stringWithFormat:#"SELECT aid FROM album WHERE owner=me()"];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:fql, #"query", #"json", #"format", nil];
[facebook requestWithMethodName:#"fql.query" andParams:params andHttpMethod:#"GET" andDelegate:self];
return YES;
}
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0]; }
NSLog(#"didLoad() received result: %#", result);
}
Yes, you absolutely need to include the access_token field. We verify that the access token has access to the user_photos permission before allowing you access to that user's data.

iPhone Facebook sdk, publish to wall

I got the following example form iPhone SDK sample code and probably tried 10 others but i cant seem to be able to post a message to my wall. I dont want a dialog popup to appear for me to enter my message, I want it so when the user click on a post button and the message will auto post to my wall. Im using the new version of Facebook iPhone SDK on iPad.
Id appreciate if someone can spot where im going wrong, at the moment i have no errors or warnings. The permissions i have in place are "email" and "publish_stream".
Thank you in advance.
Ive found App page on facebook if anyone is interested (http://www.facebook.com/settings/?tab=applications).
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:
#"a long run", #"name",
#"The Facebook Running app", #"caption",
#"it is fun", #"description",
#"http://itsti.me/", #"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%#",[NSString stringWithFormat:#"%#/feed", [defaults objectForKey:USERID]]);
[facebook requestWithMethodName:[NSString stringWithFormat:#"%#/feed", [defaults objectForKey:USERID]]
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
If others stumble on this and wish to know how to remove the option so safari doesnt take over you screen, find the following and set safariAuth:NO.
[self authorizeWithFBAppAuth:YES safariAuth:YES];
You will also need to uncomment "return" from the following method so that the webpage doesnt expand out of its holder every time the user clicks between the input box and dismissing the keyboard.
(void)keyboardWillShow:(NSNotification*)notification