1

I want to connect to the Facebook Login through api, I can Login and get information but everything is from Facebook:

here is my code how can I write this code return [FBSession.activeSession handleOpenURL:url]; to

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

I should have session.activeSession, but if I right it like this I will have an error

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

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

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

would you please help me to implement this method,

Thanks in advance!

UPDATE : It never use the url and stringByAppendingString:@"connect-fb"

I can login and I can get information but it never use api for example it works with this NSString *url = [BASE_URL stringByAppendingString:@"connect-fb"]; and also if I put what ever I want in stringByAppendingString:@"everything"

Here is my method for login and connect to 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

我想你已经有了你的应用程序的FacebookAppId.

假设它是:123456789012345

您必须输入 YourApp-Info.plist。

在键下:

FacebookAppId作为123456789012345

URLTypes- Item0- UrlSchemes-Item0作为fb123456789012345

那么你的开放网址应该是:

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

编辑2:

不是最终的解决方案,只是向前迈了一步:

我将您的请求创建修改为:

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 回答
0

您确定在您的代码中导入库吗?

#import "FBConnect.h"
#import "Facebook.h"
于 2013-02-21T16:41:06.807 回答