2016-05-05 8 views
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プロパティが表示されません。それが失敗した場所

これは、次のとおりです。enter image description here

答えて

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]; 
    } 
+0

「APP_DELEGATE」にプロパティを設定したコードの部分を説明できますか?それはどのように機能するのですか? – Erik

+0

それは何もありません。 AppDelegateに値を渡していました。あなたはブロック内にあれば何でもできます –

+0

私はそれが問題だとは思わない。ダウンロードは正常ですが、値を読み取ろうとすると失敗することがよくあります – Erik

関連する問題