2012-07-18 21 views
6

मुझे अपने आईफोन एप्लिकेशन में दीवार पर फेसबुक पोस्टिंग लागू करने में समस्या है। मैंने एसडीके स्थापित किया और लिंकवर्क फ्रेमवर्क लॉगिन ठीक काम कर रहा है। यहां कोड है:आईफोन ऐप पर दीवार पर फेसबुक एसडीके पोस्ट

-(IBAction)loginButtonPressed:(id)sender 
{ 
    NSLog(@"loginButtonPressed: called"); 

    AppDelegate *appdel=[[UIApplication sharedApplication] delegate]; 
    appdel.facebookSession=[[FBSession alloc] init]; 
    [appdel.facebookSession openWithCompletionHandler:^(FBSession *session, 
                FBSessionState status, 
                NSError *error) 
    { 
     // 
    }]; 
} 

लेकिन मुझे उपयोगकर्ता की दीवार पर संदेश पोस्ट करने में समस्या है। यहां कोड है:

-(IBAction)likeButtonPressed:(id)sender 
{ 
    NSLog(@"likeButtonPressed: called"); 
    // Post a status update to the user's feedm via the Graph API, and display an alert view 
    // with the results or an error. 

    NSString *message = @"test message"; 
    NSDictionary *params = [NSDictionary dictionaryWithObject:message forKey:@"message"]; 

    // use the "startWith" helper static on FBRequest to both create and start a request, with 
    // a specified completion handler. 
    [FBRequest startWithGraphPath:@"me/feed" 
         parameters:params 
         HTTPMethod:@"POST" 
       completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 

        [self showAlert:message result:result error:error]; 
       }]; 

} 

कृपया मेरी सहायता करें। मेरे कोड में क्या गलत है? या मुझे लॉगिन अनुरोध के लिए कुछ अनुमतियां जोड़नी चाहिए?

+2

आप वास्तविक _problem/Error_ वर्णन करने के लिए भूल गया था। – CBroe

+1

जब आप अपना फेसबुक पोस्ट शुरू करते हैं तो लॉग इन फॉर्म प्रदर्शित होता है? –

+2

मुझे डीबग में एक संदेश मिला है: 'त्रुटि: HTTP स्थिति कोड: 400' और' ऑपरेशन पूरा नहीं हो सका। (com.facebook.FBIOSSDK त्रुटि 5.) 'ÙIAlertView' – user1385666

उत्तर

9

इस कोड को मेरे लिए काम किया प्रयास करें। पहले हम

#import <FBiOSSDK/FacebookSDK.h> 

तो

@property (strong, nonatomic) FBRequestConnection *requestConnection; 

और निश्चित रूप से भूल जाना चाहिए नहीं के संश्लेषण के लिए:

@synthesize requestConnection; 

कोड में ही:

-(IBAction)likeButtonPressed:(id)sender 
{ 
    NSLog(@"likeButtonPressed: called"); 
    // FBSample logic 
    // Check to see whether we have already opened a session. 
    if (FBSession.activeSession.isOpen) 
    { 
     // login is integrated with the send button -- so if open, we send 
     [self postOnWall]; 
    } 
    else 
    { 
     [FBSession sessionOpenWithPermissions:[NSArray arrayWithObjects:@"publish_stream", nil] 
           completionHandler: 
      ^(FBSession *session, 
       FBSessionState status, 
       NSError *error) 
      { 
       // if login fails for any reason, we alert 
       if (error) 
       { 
        NSLog(@" login failed"); 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                    message:error.localizedDescription 
                    delegate:nil 
                  cancelButtonTitle:@"OK" 
                  otherButtonTitles:nil]; 
        [alert show]; 
        // if otherwise we check to see if the session is open, an alternative to 
        // to the FB_ISSESSIONOPENWITHSTATE helper-macro would be to check the isOpen 
        // property of the session object; the macros are useful, however, for more 
        // detailed state checking for FBSession objects 
       } 
       else if (FB_ISSESSIONOPENWITHSTATE(status)) 
       { 
        NSLog(@" sending post on wall request..."); 
        // send our requests if we successfully logged in 
        [self postOnWall]; 
       } 
      }]; 
    }; 
} 

- (void)postOnWall 
{ 
    NSNumber *testMessageIndex=[[NSNumber alloc] init]; 
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"testMessageIndex"]==nil) 
    { 
     testMessageIndex=[NSNumber numberWithInt:100]; 
    } 
    else 
    { 
     testMessageIndex=[[NSUserDefaults standardUserDefaults] objectForKey:@"testMessageIndex"]; 
    }; 
    testMessageIndex=[NSNumber numberWithInt:[testMessageIndex intValue]+1]; 
    [[NSUserDefaults standardUserDefaults] setObject:testMessageIndex forKey:@"testMessageIndex"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 

    // create the connection object 
    FBRequestConnection *newConnection = [[FBRequestConnection alloc] init]; 

    // create a handler block to handle the results of the request for fbid's profile 
    FBRequestHandler handler = 
    ^(FBRequestConnection *connection, id result, NSError *error) { 
     // output the results of the request 
     [self requestCompleted:connection forFbID:@"me" result:result error:error]; 
    }; 

    // create the request object, using the fbid as the graph path 
    // as an alternative the request* static methods of the FBRequest class could 
    // be used to fetch common requests, such as /me and /me/friends 
    NSString *messageString=[NSString stringWithFormat:@"wk test message %i", [testMessageIndex intValue]]; 
    FBRequest *request=[[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:@"me/feed" parameters:[NSDictionary dictionaryWithObject:messageString forKey:@"message"] HTTPMethod:@"POST"]; 

    // add the request to the connection object, if more than one request is added 
    // the connection object will compose the requests as a batch request; whether or 
    // not the request is a batch or a singleton, the handler behavior is the same, 
    // allowing the application to be dynamic in regards to whether a single or multiple 
    // requests are occuring 
    [newConnection addRequest:request completionHandler:handler]; 

    // if there's an outstanding connection, just cancel 
    [self.requestConnection cancel]; 

    // keep track of our connection, and start it 
    self.requestConnection = newConnection;  
    [newConnection start]; 
} 

// FBSample logic 
// Report any results. Invoked once for each request we make. 
- (void)requestCompleted:(FBRequestConnection *)connection 
       forFbID:fbID 
        result:(id)result 
        error:(NSError *)error 
{ 
    NSLog(@"request completed"); 

    // not the completion we were looking for... 
    if (self.requestConnection && 
     connection != self.requestConnection) 
    { 
     NSLog(@" not the completion we are looking for"); 
     return; 
    } 

    // clean this up, for posterity 
    self.requestConnection = nil; 

    if (error) 
    { 
     NSLog(@" error"); 
     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     // error contains details about why the request failed 
     [alert show]; 
    } 
    else 
    { 
     NSLog(@" ok");   
    }; 
} 
+1

पॉप अप करें यह एक अपेक्षित उत्तर था, अच्छी तरह से लिखा गया था। लेकिन, FBRequestConnection किसी भी UI को FBDialog की तरह प्रदान नहीं करता है। इसलिए, यदि हम FaceBookFramework को संभालने के लिए चाहते हैं तो इसका विकल्प क्या होना चाहिए। – andyPaul

+1

मेरे लिए काम नहीं किया –

+0

@Mobilhunterz: क्या आप अपना कोड हमारे साथ साझा कर सकते हैं? यह मेरे लिए पहले रन के लिए काम किया। हो सकता है कि आप कुछ खो रहे हों। अपने कोड स्निपेट को हमारे साथ साझा करें ताकि हम आपकी मदद कर सकें। – Ans

3

इस कोड

 NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 
NSArray* facebookCookies = [cookies cookiesForURL: 
          [NSURL URLWithString:@"http://login.facebook.com"]]; 

for (NSHTTPCookie* cookie in facebookCookies) { 
    [cookies deleteCookie:cookie]; 
} 
NSString *FBBody = [NSString stringWithFormat:@"your message you want to post"]; 
UIImage *img=[UIImage imageNamed:[NSString stringWithFormat:image naemif you want to post]]; 
FBFeedPost *post = [[FBFeedPost alloc] initWithPhoto:img name:FBBody]; 
[post publishPostWithDelegate:self]; 
[[UIAppDelegate indicator] startAnimating]; 
IFNNotificationDisplay *display = [[IFNNotificationDisplay alloc] init]; 
display.type = NotificationDisplayTypeLoading; 
display.tag = NOTIFICATION_DISPLAY_TAG; 
2

आप अनुमतियां सेट करना चाहिए :"स्थिति_ अद्यतन करें"।

इस तरह:

FBLoginView *loginview = [[FBLoginView alloc] initWithPermissions:[NSArray arrayWithObject:@"status_update"]]; 

या

FBSession *fbSession = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObject:@"status_update"]]; 
संबंधित मुद्दे