Wednesday 30 January 2013

Facebook Sharing with Facebook SDK 3.0/3.1 iOS 6


Well I put up this code to share using Facebook SDK 3.0/3.1 for iOS 6 , couldn't find it anywhere and finding from the developers site is pretty tough, so hope this helps.

Add the Facebook Framework , and resources bundle , Social framework and Adsupport framework

- (IBAction)facebookShare:(id)sender {

BOOL displayedNativeDialog =
    [FBNativeDialogs
     presentShareDialogModallyFrom:self
     initialText:dealLabel.text
     image:[UIImage imageNamed:@"logo.png"]
     url:[NSURL URLWithString:@"http://www.buzzingaa.com.au"]
     handler:^(FBNativeDialogResult result, NSError *error) {
         if (error) {
             /* handle failure */
         } else {
             if (result == FBNativeDialogResultSucceeded) {
                 /* handle success */
             } else {
                 /* handle user cancel */
             }
         }
     }];
    if (!displayedNativeDialog) {
        /*
         Fallback to web-based Feed Dialog:
         */
    }
    }

And the delegate functions :

#pragma mark FaceBook Methods

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) stateFB
                      error:(NSError *)error
{
    [FBSettings setLoggingBehavior:[NSSet setWithObjects:
                                    FBLoggingBehaviorFBRequests, nil]];
    switch (stateFB) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
//                [self LoggedIn];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }
    
    //    [[NSNotificationCenter defaultCenter]
    //     postNotificationName:FBSessionStateChangedNotification
    //     object:session];
    
    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
    
}

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    return [FBSession openActiveSessionWithReadPermissions:nil
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState stateFB,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:stateFB
                                                                 error:error];
                                         }];
    
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];
}


- (void) closeSession {
    [FBSession.activeSession closeAndClearTokenInformation];
}

- (void)sessionStateChanged:(NSNotification*)notification {
    if (FBSession.activeSession.isOpen) {
        // [buttonFBLogin setTitle:@"Logout" forState:UIControlStateNormal];
        } else {
             // [buttonFBLogin setTitle:@"Login" forState:UIControlStateNormal];
            }
}

No comments:

Post a Comment