FBSDKProfile类别经常失败

时间:2016-05-05 22:13:28

标签: ios objective-c singleton runtime facebook-sdk-4.0

我为Facebook SDK V4 for iOS提供了FBSDKProfile的类别。此类别使我能够获取用户配置文件图像并使用[FBSDKProfile currentProfile]单例实例访问它。

这是我的类别标题文件:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <objc/runtime.h>

static char const * const kProfileImageKey = "profile_image";

@interface FBSDKProfile (ProfileImage)

+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler;

@property (nonatomic, strong) UIImage *profileImage;

@end

以下是实施文件:

#import "FBSDKProfile+ProfileImage.h"

@implementation FBSDKProfile (ProfileImage)

+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler {
    FBSDKProfile *currentProfile = [FBSDKProfile currentProfile];
    NSString *userId = currentProfile.userID;
    if (![userId isEqualToString:@""] && userId != Nil)
    {
        [self downloadFacebookProfileImageWithId:userId completionBlock:^(BOOL succeeded, UIImage *profileImage) {
            currentProfile.profileImage = profileImage;
            [[NSNotificationCenter defaultCenter] postNotificationName:FBSDKProfileDidFetchProfileImageNotification object:nil];
            if (handler) { handler(succeeded); }
        }];
    } else
    {
        /* no user id */
        if (handler) { handler(NO); }
    }
}

+(void)downloadFacebookProfileImageWithId:(NSString *)profileId completionBlock:(void (^)(BOOL succeeded, UIImage *profileImage))completionBlock
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", profileId]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if (!error)
                               {
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   completionBlock(YES, image);
                               } else{
                                   completionBlock(NO, nil);
                               }
                           }];
}

#pragma mark - custom getter/setter methods

-(void)setProfileImage:(UIImage *)profileImage {
objc_setAssociatedObject(self, kProfileImageKey, profileImage, OBJC_ASSOCIATION_ASSIGN);
}

-(UIImage *)profileImage {
    return objc_getAssociatedObject(self, kProfileImageKey);
}

@end

问题

此解决方案的工作方式与大多数时间一样,但确实会失败。据我所知,我认为这与图像的存储有关。

如果发生异常,如果我po [FBSDKProfile currentProfile].profileImage,则返回:

  • 错误:属性&#39; profileImage&#39;没有找到类型为&#39; FBSDKProfile *&#39;
  • 的对象
  • 错误:解析表达式时出错1个

如果我将指针悬停在[FBSDKProfile currentProfile]实例上,则它不会在属性列表中显示profileImage属性。

这是失败的地方:enter image description here

1 个答案:

答案 0 :(得分:0)

可能这可以帮到你。

  -(void)getFacebookProfileInfos:(NSString*)token{

    FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:@{@"fields":@"id, name, picture.type(large),email"}];

   FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];

   [connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
    {
       if(result)
       {
         APP_DELEGATE.socialEmail=result[@"email"];
         APP_DELEGATE.socialName= result[@"name"];
         APP_DELEGATE.socialImage= result[@"picture"][@"data"][@"url"];
         APP_DELEGATE.socialAcessToken=token;


         HomeVC *obj = SB_IDENTIFIER(@"home");
         SB_PUSH(obj);
       }
       else
       {
         NSLog(@"%@", [error localizedDescription]);
       }
    }];
   [connection start];
   }