0
iOS用Facebook SDK V4によって提供されたFBSDKProfile
のカテゴリを書き込みました。このカテゴリを使用すると、ユーザプロファイルイメージを取得し、シングルトンインスタンス[FBSDKProfile currentProfile]
を使用してアクセスできます。FBSDKProfileカテゴリが頻繁に失敗する
これは私のカテゴリのヘッダファイルです:
#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
を行う場合には、例外時
は、それが返されます。
- エラー:プロパティ 'profileImage' 'FBSDKProfile *'
- エラーの種類のオブジェクトには見られない:1、エラーの解析式を
[FBSDKProfile currentProfile]
インスタンスの上にポインタを置くと、プロパティリストにprofileImage
プロパティが表示されません。それが失敗した場所
「APP_DELEGATE」にプロパティを設定したコードの部分を説明できますか?それはどのように機能するのですか? – Erik
それは何もありません。 AppDelegateに値を渡していました。あなたはブロック内にあれば何でもできます –
私はそれが問題だとは思わない。ダウンロードは正常ですが、値を読み取ろうとすると失敗することがよくあります – Erik