2012-07-18 112 views
6

我的iPhone應用程序在牆上發佈Facebook發佈時遇到問題。我安裝了SDK和鏈接框架 登錄工作正常。這裏是代碼:在iPhone應用程序牆上的Facebook sdk發佈

-(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 /錯誤_。 – CBroe 2012-07-18 11:36:04

+1

當你啓動你的Facebook發佈時,是否顯示登錄表單? – 2012-07-18 11:36:05

+2

我在調試中收到一條消息:'錯誤:HTTP狀態碼:400'和'操作無法完成。 (com.facebook.FBIOSSDK錯誤5.)'in'IAlertView' – user1385666 2012-07-18 11:36:14

回答

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 2012-09-23 03:13:21

+1

沒有爲我工作 – 2012-09-26 09:38:17

+0

@Mobilhunterz:你可以與我們分享你的代碼嗎?這對我來說是第一次運行。可能是你錯過了一些東西。與我們分享您的代碼片段,以便我們可以更好地爲您提供幫助。 – Ans 2013-08-16 15:07:48

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"]]; 
相關問題