在目标 c++ 中尝试 IOS facebook SDK

Attempting IOS facebook SDK in objective c++

本文关键字:IOS facebook SDK 目标 c++      更新时间:2023-10-16

我正在尝试为IOS facebook SDK创建一个C++桥。我可以正常登录,但是当我尝试检索用户详细信息时,图形函数似乎从未执行过,尽管我可以在调试器中跳过它们。这是我所拥有的:

void facebook_bridge::performLoginWithFacebook()
{
    // Open a session showing the user the login UI
    // You must ALWAYS ask for basic_info permissions when opening a session
    [FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {
         // Retrieve the app delegate
         AppController* appDelegate = (AppController*)[UIApplication sharedApplication].delegate;
         // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
         [appDelegate sessionStateChanged:session state:state error:error];
     }];
    isLoggedIn = true;
}
cocos2d::CCDictionary* facebook_bridge::getUserDetails()
{
    __block NSDictionary *currentPermissions;
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  // These are the current permissions the user has
                                  currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                  NSLog(@"No error");
                                  // We will store here the missing permissions that we will have to request
                                  // If we have permissions to request
                                                                } else {
                                  NSLog([NSString stringWithFormat:@"error %@", error.description]);
                              }
                          }];
    cocos2d::CCDictionary* dict = cocos2d::CCDictionary::create();
    for(int i = 0; i < currentPermissions.count; i++)  //get a bad access here, currentPermissions is always null
    {
        NSString* val = currentPermissions.allValues[i];
        NSString* k = currentPermissions.allKeys[i];
        std::string value = *new std::string(val.UTF8String);
        std::string key = *new std::string(k.UTF8String);
        cocos2d::CCString *ccVal = cocos2d::CCStringMake(value);
        dict->setObject((cocos2d::CCObject*)ccVal, key);
    }
    dict->retain();
    return dict;
}

我认为 FBRequest 是异步的,所以我尝试将其放在第一个函数中以在同一线程上运行,但无济于事。

有什么帮助吗?(我对目标C很陌生,但有大多数其他语言的经验)

我认为 FBRequest 是异步的,所以我尝试将其放在 第一个在同一线程上运行的函数,无济于事。

我认为您完全错过了"异步"的含义。您不必有线程才能使事情异步 - 您可以在同一线程上异步执行某些内容。(尽管在这种情况下,它可能在另一个线程上执行其主要操作,然后在原始线程上运行完成块。

调用异步 API 时,完成块不会在函数的其余部分之前执行。几乎可以保证。