1

APIを介してFacebookログインに接続したいのですが、ログインして情報を取得できますが、すべてFacebookからのものです:

これが私のコードです このコードをどのように書くことができますか return [FBSession.activeSession handleOpenURL:url]; に

return [[[HttpRequest sharedHttpRequest] session] handleOpenURL:url];

session.activeSession が必要ですが、このように正しくするとエラーが発生します

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation
{

//return [FBSession.activeSession handleOpenURL:url];

// return [[[HttpRequest sharedHttpRequest] session] handleOpenURL:url];
}

この方法を実装するのを手伝ってくれませんか?

前もって感謝します!

更新: URL と stringByAppendingString:@"connect-fb" を使用することはありません

ログインして情報を取得できますが、API を使用することはありません。たとえば、この NSString *url = [BASE_URL stringByAppendingString:@"connect-fb"]; で動作します。また、必要なものを stringByAppendingString:@"everything" に入れると、

ログインしてAPIに接続するための私の方法は次のとおりです。

 -(void) getApiUserInfos
{

if (FBSession.activeSession.isOpen) {
    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,

       NSError *error) {

         if (!error) {

             NSString *url = [BASE_URL stringByAppendingString:@"connect-fb"];
             NSLog(@"%@",url);
             NSString *post = [NSString 
 stringWithFormat:@"facebookId=%@&firstName=%@&lastName=%@&email=%@",
                               self.facebookId = user.id,
                               self.firstName= user.first_name,
                               self.lastName= user.last_name,
                               self.email=user.email];

             NSLog(@"post : %@",post);
             post = [post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

             NSData *postData = [NSData dataWithBytes: [post UTF8String] length: [post 
 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

             NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL 
 URLWithString:url]

                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                                timeoutInterval:600];

             [request setHTTPMethod:@"POST"];
             [request setHTTPBody:postData];

             [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue 
 alloc] init]
                                    completionHandler:
              ^(NSURLResponse *response, NSData *data, NSError *error) {


                  return;

                  if(error)
                  {
                      NSLog(@"Errorr");
                  }

                  ///write for if error
                    NSString *ret = [NSString stringWithUTF8String:[data bytes]];


                  NSLog(@"api yser infos RETSTRING : %@",ret);


              }];                 
         }
     }];
   }
4

2 に答える 2

0

コードにライブラリをインポートしてもよろしいですか?

#import "FBConnect.h"
#import "Facebook.h"
于 2013-02-21T16:41:06.807 に答える
0

アプリのFacebookAppId.

それは次のとおりです。123456789012345

YourApp-Info.plist に入力する必要があります。

キーの下:

FacebookAppIdなので123456789012345

URLTypes- Item0- UrlSchemes-Item0としてfb123456789012345

次に、開いている URL は次のようになります。

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{

    return [FBSession.activeSession handleOpenURL:url];
}

編集:

ユーザーデータを取得する別の方法を試すことができます。私にとって完璧に機能します。

- (void)fetchUserData
{
    NSLog (@"fetching user data");


    if (FBSession.activeSession.isOpen)
    {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,
           NSError *error)
        {
            if (!error)
            {
                NSLog (@"FB:          id: %@", user.id);
                NSLog (@"FB:    username: %@", user.username);
                NSLog (@"FB:        link: %@", user.link);
                NSLog (@"FB:        name: %@", user.name);
                NSLog (@"FB:  first_name: %@", user.first_name);
                NSLog (@"FB:   last_name: %@", user.last_name);
                NSLog (@"FB: middle_name: %@", user.middle_name);
                NSLog (@"FB:    birthday: %@", user.birthday);
                NSLog (@"FB:       email: %@",[user objectForKey:@"email"]);
                NSLog (@"FB:      gender: %@",[user objectForKey:@"gender"]);
            }
            else
            {
                NSLog (@"fetchUserData: error");
            }
        }];
    }
}

EDIT2:

最終的な解決策ではなく、一歩前進しただけです。

リクエストの作成を次のように変更しました:

NSString *post = [NSString stringWithFormat:@"facebookId=%@&firstName=%@&lastName=%@&email=%@",
                                  self.facebookId = user.id,
                                  self.firstName = user.first_name,
                                  self.lastName = user.last_name,
                                  self.email = [user objectForKey:@"email"]]; //this line changed

そして完了ハンドラーへ:

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[[NSOperationQueue alloc] init]
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error)
{
    //return; - ditch this

    if(error)
    {
        NSLog(@"Error: %@", [error localizedDescription]); // returns "unsuported URL"
    }
    else
    {
         NSString *ret = [NSString stringWithUTF8String:[data bytes]];

         NSLog(@"api yser infos RETSTRING : %@",ret);
    }            
}];
于 2013-02-21T16:53:58.403 に答える