Facebook sdk贴在墙上的iPhone应用程序 [英] Facebook sdk post on wall on iPhone app

查看:111
本文介绍了Facebook sdk贴在墙上的iPhone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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);
//通过Graph API向用户的feedm发布状态更新,并显示带有结果或错误的警报视图
//。

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

//使用FBRequest上的startWithhelper static来创建和启动请求,
//指定的完成处理程序。
[FBRequest startWithGraphPath:@me / feed
参数:params
HTTPMethod:@POST
completionHandler:^(FBRequestConnection * connection,id result,NSError * error) {

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

}

请帮助我。我的代码怎么了?或者我应该添加一些登录请求的权限?

解决方案

这段代码为我工作。
首先我们必须

  #import< FBiOSSDK / FacebookSDK.h> 

然后

  @property(strong,nonatomic)FBRequestConnection * requestConnection; 

当然不要忘记合成:

  @synthesize requestConnection; 

代码本身:

   - (IBAction)likeButtonPressed:(id)sender 
{
NSLog(@likeButtonPressed:called);
// FBSample logic
//检查我们是否已经打开了一个会话。
if(FBSession.activeSession.isOpen)
{
//登录与发送按钮集成 - 所以如果打开,我们发送
[self postOnWall];
}

{
[FBSession sessionOpenWithPermissions:[NSArray arrayWithObjects:@publish_stream,nil]
completionHandler:
^(FBSession *
FBSessionState状态,
NSError *错误)
{
//如果登录失败的任何原因,我们警告
如果(错误)
{
NSLog(@login failed);
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Error
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@OK
otherButtonTitles :零];
[alert show];
//如果否则我们检查会话是否打开,另一个
//对FB_ISSESSIONOPENWITHSTATE帮助宏将是检查会话对象的isOpen
//属性;这些宏是有用的,但是更多的
// FBSession对象的详细状态检查
}
else if(FB_ISSESSIONOPENWITHSTATE(status))
{
NSLog(@ 发墙贴请求...);
//发送我们的请求,如果我们成功登录
[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];

//创建连接对象
FBRequestConnection * newConnection = [[FBRequestConnection alloc] init];

//创建处理程序块来处理fbid的配置文件请求的结果
FBRequestHandler handler =
^(FBRequestConnection * connection,id result,NSError * error){
//输出请求的结果
[self requestCompleted:connection forFbID:@meresult:result error:error];
};

//创建请求对象,使用fbid作为图形路径
//作为替代的请求* FBRequest类的静态方法可以
//用于获取常见请求,例如/ me和/ me / friends
NSString * messageString = [NSString stringWithFormat:@wk test message%i,[testMessageIndex intValue]];
FBRequest * request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:@me / feed参数:[NSDictionary dictionaryWithObject:messageString forKey:@message] HTTPMethod:@POST];

//如果添加了多个请求,则向连接对象添加请求
//连接对象将以请求的形式组合请求;无论是
//不是请求是批次还是单例,处理程序行为是相同的,
//允许应用程序在单个或多个
/ /请求发生
[newConnection addRequest:request completionHandler:handler];

//如果有一个未完成的连接,只需取消
[self.requestConnection cancel];

//跟踪我们的连接,并启动它
self.requestConnection = newConnection;
[newConnection start];
}

// FBSample logic
//报告任何结果。针对我们所做的每个请求调用一次。
- (void)requestCompleted :( FBRequestConnection *)连接
forFbID:fbID
结果:(id)结果
错误:(NSError *)错误
{
NSLog(@请求完成);

//不是我们正在寻找的完成...
if(self.requestConnection&&
connection!= self.requestConnection)
{
NSLog(@不是我们要找的完成);
返回;
}

//清理这个,为后代
self.requestConnection = nil;

if(error)
{
NSLog(@error);
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@errormessage:error.localizedDescription delegate:nil cancelButtonTitle:@OKotherButtonTitles:nil];
//错误包含有关请求失败的原因的详细信息
[alert show];
}
else
{
NSLog(@ok);
};
}


I have a problem with implementing Facebook posting on wall in my iPhone application. I installed SDK and linked framework login is working fine. here's the code:

-(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)
    {
        //
    }];
}

But I have a problem with posting message on user's wall. Here's the code:

-(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];
                }];

}

Help me please. What's wrong with my code? Or should I add some permissions to login request?

解决方案

this code worked for me. First we must

#import <FBiOSSDK/FacebookSDK.h>

then

@property (strong, nonatomic) FBRequestConnection *requestConnection;

and of course do not forget to synthesize:

@synthesize requestConnection;

the code itself:

-(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");        
    };
}

这篇关于Facebook sdk贴在墙上的iPhone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆