facebook publish POST API on iphone : Not publishing properly - cocoa-touch

I am trying to use facebook publish API link this as given here http://developers.facebook.com/docs/reference/api/post/
NSDictionary *privacyDict = [[[NSDictionary alloc] initWithObjectsAndKeys:#"SELF",#"value",nil] autorelease];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSString *privacyJSONStr = [jsonWriter stringWithObject:privacyDict];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Test test", #"message",
privacyJSONStr, #"privacy",
#"http://www.facebook.com/", #"link",
#"This name check where it will show", #"name",
#"Caption for the link", #"caption",
#"Longer description of the link", #"description",
nil];
[facebookObj requestWithMethodName:#"facebook.Stream.publish" andParams:params andHttpMethod:#"POST" andDelegate:self];
It posted successfully, but not like that as I expected
there is no caption, name, description , link etc
it shown as
This type of result I can get by this parameter
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Test test", #"message",
privacyJSONStr, #"privacy",
nil];
What is the mistake, and How we can publish all the parameter
Amit Battan

try this .. its works for me
- (IBAction )publishStream{
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Posting From",#"text",#"http://www.google.com/",#"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSLog(#"Action Link String : %#", actionLinksStr);
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"Feed Name", #"name",
#"Feed Caption is here", #"caption",
#"Feed Description is here", #"description",
#"http://www.google.com/", #"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSLog(#"Attachment String : %#", attachmentStr);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
[facebook dialog:#"facebook.Stream.publish"
andParams:params
andDelegate:self];
}

Related

Create nested JSON in Objective-C

I'm trying to create JSON that looks like this:
{
"id": "feed/http://feeds.feedburner.com/design-milk",
"title": "Design Milk",
"categories": [
{
"id": "user/category/test",
"label": "test"
}
]
}
I'm doing it with this method:
NSMutableDictionary *req = [NSMutableDictionary #"feed/http://feeds.feedburner.com/design-milk" forKey:#"id"];
[req #"Design Milk" forKey:#"title"];
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
#"user/category/test", #"id", #"test", #"label",
nil];
[req setObject:tmp forKey:#"categories"];
NSData *postdata = [NSJSONSerialization dataWithJSONObject:req options:0 error:&error];
However, this isn't working. What am I doing wrong here?
The first line of your code isn't going to compile, so you need to fix that.
The value of "categories" in your example output is an array with one element, which happens to be a dictionary. So you need
NSDictionary* oneCategory = [NSDictionary dictionaryWithObjectsAndKeys:...];
NSArray* categories = [NSArray arrayWithObject:oneCategory];
[req setObject:categories forKey:#"categories"];
And you should really use more modern syntax, like
req [#"categories"] = #[oneCategory];
You were missing an array:
NSMutableDictionary *req =[NSMutableDictionary dictionaryWithObjectsAndKeys:#"feed/http://feeds.feedburner.com/design-milk", #"id", nil];
req[#"title"] = #"Design Milk";
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
#"user/category/test", #"id",
#"test", #"label",
nil];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:tmp];
[req setObject:arr forKey:#"categories"];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:req options:0 error:&error];

NSArray crashes app when it has more than 2 objects

so I've been working with NSNotificationCenter in Xcode and I've tried to attach a NSDictionary to my notification using userInfo.
NSArray *objects = [NSArray arrayWithObjects:#"Example Name", #"Example Description", #"Example Date", nil];
NSArray *keys = [NSArray arrayWithObjects:#"name", #"description", #"date", nil];
NSDictionary *dict = [NSDictionary
dictionaryWithObjects:objects
forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notification" object:nil userInfo:dict];
When I try to run the app and post the notification, it crashes at the line:
NSArray *keys = [NSArray arrayWithObjects:#"name", #"description", #"date", nil];
I later found that if the array size exceeded 2 objects, the app would crash.
So if I changed my code to the snippet below, it would work.
NSArray *objects = [NSArray arrayWithObjects:#"Example Name", #"Example Description", nil];
NSArray *keys = [NSArray arrayWithObjects:#"name", #"description", nil];
NSDictionary *dict = [NSDictionary
dictionaryWithObjects:objects
forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notification" object:nil userInfo:dict];
Is there any way I can work around this, or am I doing something terribly wrong?
Does this code compile? Try cleaning and rebuilding the project.

IOS Fb wall post method not working

I am using following code. It takes me to FB Dialog box where I authorize the app and after authorizing app it takes me back to my applicaiton. However It NEVER post anything on the wall.
Any ideas what is wrong?
- (void) login
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.facebook = [[Facebook alloc] initWithAppId:#"MY_APP_ID" andDelegate:self];
NSArray* permissions = [NSArray arrayWithObjects:#"publish_stream", nil];
[appDelegate.facebook authorize:permissions];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#".", #"name",
#".", #"caption",
nil];
/*
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#".", #"name",
#"Build great social apps and get more installs.", #"caption",
#"The Facebook SDK for iOS makes it easier and faster to develop Facebook integrated iOS apps.", #"description",
#"https://developers.facebook.com/ios", #"link",
#"https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png", #"picture",
nil];
*/
// Invoke the dialog
[appDelegate.facebook dialog:#"feed" andParams:params andDelegate:self];
}
You should do it like this:
graphPath = #"me/feed";
[params setObject:#"link" forKey:#"type"];
[params setObject:pict forKey:#"picture"];
[params setObject:self.url forKey:#"link"];
[params setObject:self.message forKey:#"message"];
[params setObject:self.urlName forKey:#"name"];
[params setObject:self.caption forKey:#"caption"];
[params setObject:self.urlDescription forKey:#"description"];
[appDelegate.facebook requestWithGraphPath:graphPath andParams:params andHttpMethod:#"POST" andDelegate:self];

Facebook iPhone SDK - automatically PublishSteam (bypass Post to wall dialog)

Trying to implement facebook wall post on the iphone app I'm working on.
How can I bypass the "Post to Your Wall" dialog box and just submit straight?
Basically I'm just trying to post a url and I don't really want to allow people to see that "Write something" textbox.
Here is the code that I have so far (from the sample app).
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];
[_facebook dialog:#"feed"
andParams:params
andDelegate:self];
Thank you,
Tee
Just use one of the - (FBRequest*)requestWith... instance methods of the Facebook class.
Simple wall post using the old REST API:
NSString *facebookStatusMessage = #"facebookStatusMessage";
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
facebookStatusMessage, #"status",
nil];
FBRequest *request = [_facebook requestWithMethodName:#"status.set" andParams:params andHttpMethod:#"POST" andDelegate:self];
It's better to use the Facebook Graph API with - (FBRequest*)requestWithGraphPath: as the REST API will get deprecated.
Then also implement some of the FBRequestDelegate protocol:
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
}
- (void)request:(FBRequest *)request didLoad:(id)result {
// result may be a dictionary, an array, a string, or a number,
// depending on the format of the API response
}

Insert NSMutableDictionary with NSDictionaries

I tried to insert NSDictionary's in a NSMutableDictionary. There is no error but it won't work, it remains empty.
Here's my code:
NSMutableDictionary *einnahmen = [[NSMutableDictionary alloc] initWithCapacity:20];
NSArray *objects = [NSArray arrayWithObjects:
name,
[NSNumber numberWithInt: x],
[NSNumber numberWithInt: y],
[NSNumber numberWithInt: z],
nil];
NSArray *keys = [NSArray arrayWithObjects:
#"name",
#"startJahr",
#"zins",
#"entnahmefaehig",
nil];
NSDictionary *entry = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[einnahmen setObject:entry forKey:#"name"]; //seems not to work
After [einnahmen setObject:entry the Debugger shows this:
I have solved this problem. The following code was in the wrong init-method:
NSMutableDictionary *einnahmen =
[[NSMutableDictionary alloc] initWithCapacity:20];
The compiler should show a error.