Classic/Obsolete(?) way
Install SDK
Drag framework folder into XCode project.
Drag bundle into XCode project.
Drag deprecated header folder into XCode project. check copy in needed.
When picking dependency libraries, set them to optional, to allow app running on iOS5.
Set other link flag to -lsqlite3.0
Those steps are fully documented on facebook developer site: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/3.1/
Note: we are using old fashion Facebook sharing feature in app. (FBDelegate vs. FBRequest) Because my client doesn’t like posting story on their wall without a preview. This way app will popup a separated window to display link to share, image, title, description, etc. User needs to type something in the textbox with placeholder ‘say something about this…’
Note: again, preset message feature has been disabled by Facebook as of 2012.
In app, we are adding #include “Facebook.h” to implement this.
Note: you can use sharekit library for this, but I want to figure out what’s happening behind the scene.
In the place you want to popup Facebook share options to user, do this to open a Facebook session.
// openSessionWithAllowLoginUI will be called by the actual sharing screen where users can click button to post message on their wall.
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
NSArray *permission = [[NSArray alloc] initWithObjects:@"user_likes",@"publish_actions", nil];
return [FBSession openActiveSessionWithPublishPermissions:permission
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
}];}
NSString *const FBSessionStateChangedNotification = @"com.bitrixsoft.Login:FBSessionStateChangedNotifiction";
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
NSLog(@"User session found");
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
[[[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
I put this in appDelegate, so it can be shared. I don’t want to call this in appDelegate, otherwise the redirect to facebook to ask permission/login will annoy users.
Add those in appDelegate.h file
extern NSString *const FBSessionStateChangedNotification; - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;
Before calling opensession, sharing window should subscribe statechange notification.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(sessionStateChanged:)
name:FBSessionStateChangedNotification
object:nil];
- (void)sessionStateChanged:(NSNotification *)notification
{
if ((FBSession.activeSession.isOpen)) {
if (nil == self.facebook) {
self.facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
self.facebook.accessToken = FBSession.activeSession.accessToken;
self.facebook.expirationDate = FBSession.activeSession.expirationDate;
// resume sharing routine here, because first time attempt without login will stop after app back from background,
// but this method will get statechange notifcation, so perfect to resume/restart here.
[self shareViaFacebook];
}
}else{
self.facebook = nil;
}
Finally, the actual post action:
- (IBAction)shareOptions:(id)sender
{
BNAppDelegate * appDelegate = (BNAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate openSessionWithAllowLoginUI:YES];
UIActionSheet * options = [[UIActionSheet alloc]
initWithTitle:@"Share"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Twitter", @"Facebook", @"email", nil];
[options showInView:self.view];
}
- (void)shareViaFacebook
{
NSString * message = @"test msg from my test ios app";
// no nil allow in any parameter
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Bitrix News iPhone app", @"name",
_post.title, @"caption",
_post.exerpt, @"description",
@"http://www.frankmao.com", @"link",
@"http://ww1.prweb.com/prfiles/2011/02/01/8151699/gI_60506_bxlogo.jpg", @"picture",
message, @"message", //already obsolete, don't bother, facebook will ignore it anyway
nil];
[self.facebook dialog:@"feed" andParams:params andDelegate:self];
}
Someother gotcha, app identifier must match between app and setting in developers.facebook.com/app, otherwise facebook will complain app is misconfigured when starting the session.
REF: https://developers.facebook.com/docs/reference/dialogs/feed/
https://developers.facebook.com/docs/howtos/feed-dialog-using-ios-sdk/
Thanks for ones marvelous posting! I quite enjoyed reading it,
you’re a great author.I will be sure to bookmark your blog and definitely will come back at some point. I want to encourage yourself to continue your great work, have a nice holiday weekend!