Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:54:25+00:00 2026-06-17T14:54:25+00:00

I have added FBSdk in my project along with SBJson classes, what files should

  • 0

I have added FBSdk in my project along with SBJson classes, what files should i import in my second view controller to work my code ?

I want to post on Fb using the description under url so for that i need to include this code into my second view controller, but i am stuck in a part i founded that the below code should be added in the second view controller but the “delegate:self” not works, as i am doing this first time so can any one guide me how should i code in my second view controller keeping into account the values from the app delegate (How should i get the values from the app delegate of FB to the second view so that i can add this code in second code plz can any one correct this code)

facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID"]; 

// how should i take this id from the app delegate ?

 NSArray* permissions =  [NSArray arrayWithObjects:@"read_stream", 
 @"publish_stream", nil];
[facebook authorize:permissions delegate:self];

   NSMutableDictionary* params = [[NSMutableDictionary alloc] 
initWithCapacity:1]
 [params setObject:@"Testing" forKey:@"name"];
  [params setObject:@"IMAGE_URL" forKey:@"picture"];
   [params setObject:@"Description" forKey:@"description"];
  [facebook requestWithGraphPath:@"me/feed" 
    andParams:params 
  andHttpMethod:@"POST" 
   andDelegate:self];

Is the above code correct ?
How should i implement it in my second view controller in -(void)fb_share (method).

My App Delegate File is Like this :

#import "AppDelegate.h"
#import "ViewController.h"
#import "ServerRequests.h"
#import "DBManager.h"
#import "SBJson.h"


#include "FBSession.h"
#import "FBRequest.h"
#import "FBGraphUser.h"


NSString *const FBSessionStateChangedNotification = @"com.myapp:FBSessionStateChangedNotification";

@implementation AppDelegate

@synthesize userId;
@synthesize dbSnyced;
@synthesize fbId;
@synthesize loginView;

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}




 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   self.fbId = @"-1";
    self.dbSnyced = NO;
    self.loginView = nil;

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];    

    if (![self openSessionWithAllowLoginUI:NO]) {

        [self showLogin:NO];
    }

    return YES;
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{

    [self checkAccountLogin];
}

- (void) applicationDidEnterBackground:(UIApplication *)application
{
    [self hideLogin:NO];
}

- (void)  applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"applicationWillTerminate");
    [self hideLogin:NO];
    [FBSession.activeSession close];
}

- (void) checkAccountLogin
{
    self.dbSnyced = NO;
    if([ServerRequests serverIsReachableAlertIfNo:YES]) {

        DBManager *db = [[[DBManager alloc] initWithPath:DB_NAME] autorelease];
        NSDictionary *userDic = [[db smartQueryForArrayWithStatement:@"SELECT * FROM table WHERE id=1"] objectAtIndex:0];
        NSString *loginType = [userDic objectForKey:@"login_type"];
        if([loginType isEqualToString:@"none"]) {
            NSLog(@"no login type");
            [self showLogin:NO];
        }
        else {
            self.userId = [[userDic objectForKey:@"user_id"] intValue];
            if([loginType isEqualToString:@"Facebook"]) {
                NSLog(@"Facebook account");

                [FBSession.activeSession handleDidBecomeActive];
            }
            else if([loginType isEqualToString:@"MyApp"]) {
                NSLog(@"MyApp account");
            }
        }
    }
}


#pragma - Fb stuff

/*
 * Callback for session changes.
 */
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
    NSLog(@"sessionStateChanged");
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                [self handleFbUserDetails];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:FBSessionStateChangedNotification object:session];

    if (error) {
        NSLog(@"error: %@", error.localizedDescription);
        if(self.loginView) {
            [self.loginView hideLoading];
        }


    }
}

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    return [FBSession openActiveSessionWithReadPermissions:nil
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];
}


/*
 * If we have a valid session at the time of openURL call, we handle
 * Facebook transitions by passing the url argument to handleOpenURL
 */
- (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];
}

- (BOOL) activeFBSession
{
    return FBSession.activeSession.isOpen;
}

- (void) closeFBSession
{
    NSLog(@"closeFBSession");
    [FBSession.activeSession closeAndClearTokenInformation];
}


- (void) showLogin:(BOOL)animated
{
    if(!self.loginView) {
        self.loginView = [[[LoginViewController alloc] init] autorelease];
        [[self.navigationController topViewController] presentViewController:self.loginView animated:animated completion:nil];
    }
}

- (void) hideLogin:(BOOL)animated
{
    if(self.loginView) {
        [self.viewController gotToTab:0];
        [self.loginView hideLoading];
        [[self.navigationController topViewController] dismissViewControllerAnimated:animated completion:^{
            self.loginView = nil;
            NSLog(@"self.loginView = nil");
        }];
    }
}


- (void) handleFbUserDetails
{
    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
         if (!error) {
             NSLog(@"fb connected");

             self.fbId = [user objectForKey:@"id"];
             self.userId = -1;             

             ASIFormDataRequest *request = [ServerRequests ASIFormDataRequestForSrcipt:@"facebookUser"];
             [request setPostValue:[user objectForKey:@"id"] forKey:@"fbId"];
             [request setPostValue:user.first_name forKey:@"first_name"];
             [request setPostValue:user.last_name forKey:@"last_name"];
            [request setPostValue:user.location.name forKey:@"location"];
             [request setCompletionBlock:^{
                 NSString *response = [request responseString];
                 NSLog(@"facebookUser response: %@", response);
                 if([response rangeOfString:@"ERROR"].location == NSNotFound) {
                     self.userId = [response intValue];

                     DBManager *db = [[DBManager alloc] initWithPath:DB_NAME];
                     NSString *q = [NSString stringWithFormat:@"UPDATE table SET login_type = 'Facebook', user_id = '%@' WHERE id = 1", response];
                     NSLog(@"%@", q);
                     [db executeStatement:q];
                     [db release];                     
                 }
                 else {                     

                 }
                 [self sessionBecameActive];
             }];
             [request startAsynchronous];
             [self hideLogin:YES];
         }
         else {
             NSLog(@"error: %@", error.localizedDescription);
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook Request Error" message:@"Cannot connect to Facebook." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

             alert.delegate = self;
             [alert show];
             [alert release];
             if(self.loginView) {
                 [self.loginView hideLoading];
             }
         }
         [ServerRequests SyncDataBase];
     }];
}

- (void) signOut
{
    [self closeFBSession];
    DBManager *db = [[DBManager alloc] initWithPath:DB_NAME];
    [db executeStatement:@"UPDATE table SET login_type = 'none' WHERE id = 1"];
    [db release];
    [self showLogin:YES];
}

- (void) sessionBecameActive
{
    [self.viewController sessionBecameActive];
}

@end

Can any one figure out what code should i write in the -(void)fb_share method in second view controller by getting values from the delegate ?
Any Help !


I did this … and getting Error: HTTP status code: 400 … but the user name get’s displayed on alert

     [[FBRequest requestForMe] startWithCompletionHandler:
    ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
        if (!error) {

       //     NSArray* permissions =  [NSArray arrayWithObjects:@"read_stream",
          //                           @"publish_stream", nil];
           // [facebook authorize:permissions delegate:self];


          self.params =  [[NSMutableDictionary alloc] initWithObjectsAndKeys:
             @"https://developers.facebook.com/ios", @"link",
             @"https://developers.facebook.com/attachment/iossdk_logo.png", @"picture",
            @"Facebook SDK for iOS", @"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",
             nil];

            UIAlertView *tmp = [[UIAlertView alloc]
                                initWithTitle:@"Upload to FB?"
                                message:[NSString stringWithFormat:@"Upload to ""%@"" Account?", user.name]
                                delegate:self
                                cancelButtonTitle:nil
                                otherButtonTitles:@"No",@"Yes", nil];

            [FBRequestConnection startWithGraphPath:@"me/feed"
                                          parameters:self.params
                                          HTTPMethod:@"POST"
                                   completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                       if (!error) {
                                           UIAlertView *tmp = [[UIAlertView alloc]
                                                               initWithTitle:@"Success"
                                                               message:@"PostUploaded"
                                                               delegate:self
                                                               cancelButtonTitle:nil
                                                               otherButtonTitles:@"Ok", nil];

                                           [tmp show];
                                           [tmp release];
                                       }
                                   }];

            [tmp show];
            [tmp release];

                   }
    }];

.. what should i do now ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:54:26+00:00Added an answer on June 17, 2026 at 2:54 pm

    declare facebook as a property in .h file of your appDelegate

    then in second view controller import AppDelegate

    in .m file where you want the Facebook object

    get it like

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    Facebook *facebook = appDelegate.facebook;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added multiple app.config (each with a differet name) files to a project,
I have added Two Tabs in my app, to load two view controller using
I have added a navigation controller inside a viewcontroller thats inside another navigation controller...
I have added an external .exe file into my VS2010 setup project and I'd
I have added the sdk files like dropbox website says, but I can't figure
i have added an sqlite libsqlite3.0.dylib into my project's resources as per given in
Have added activityviewcontroller to the main controller -(void)ActionSheet2:(id)sender { UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
I have added a tree view to my form. I want to capture the
I have added Private Frameworks To my project. When I build in DEVICE |
Have added the Xcode Projects for openssl.xcodeproj and sqlcipher.xcodeproj in a current project and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.