FBSDKProfile 类别频繁失败

Posted

技术标签:

【中文标题】FBSDKProfile 类别频繁失败【英文标题】:FBSDKProfile category frequently fails 【发布时间】:2016-05-05 22:13:27 【问题描述】:

我为 ios 版 Facebook SDK V4 提供的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,它会返回:

错误:在“FBSDKProfile *”类型的对象上找不到属性“profileImage” 错误:1 解析表达式错误

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

这是失败的地方:

【问题讨论】:

【参考方案1】:

也许这对你有帮助。

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

【讨论】:

我明白了,您能解释一下将属性设置为APP_DELEGATE 的代码部分吗?它是如何工作的? 没什么。我正在将值传递给 AppDelegate。如果阻塞,你可以在里面做任何你想做的事情 我认为这不是问题。下载正常,但尝试读取值经常失败 我还从 facebook 获取个人资料图片并将该图片保存在 NSUserDefault 中,以使其在 imageView 上一直可见,直到我从 facebook 注销。

以上是关于FBSDKProfile 类别频繁失败的主要内容,如果未能解决你的问题,请参考以下文章

适用于 iOS 的 Facebook SDK v4.0 - 未设置 FBSDKProfile currentProfile

基于类别数据的 Apriori 算法

为啥一个频繁出现的词会被错误分类?

如果网络速度小于 1kb/s 那么为啥 SFTP 连接失败更频繁

频繁登录和退出后谷歌登录失败

Qt、QNetworkAccess Manager下载大文件频繁失败