Facebook iOS SDKでRequestのプログレスを取得する
久々に書くネタができた。
- (void)submitPhoto:(UIImage *)photo comment:(NSString *)comment{ NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: photo, @"picture", comment, @"caption", nil]; [facebook_ requestWithMethodName:@"photos.upload" andParams:params andHttpMethod:@"POST" andDelegate:self]; }
的なリクエストを送ったとき、アップロードプログレスを表示したいですよね。しかしながら、純正のFBRequestDelegateではプログレスを知ることができないようです。
johnmphさんがこの要求を実装してらっしゃいました。
ただ、純正のFBRequestに手を加えていたので、手を加えずに実現してみました。
使い方は、
@interface FacebookPhotoSubmitter : NSObject<FBRequestWithUploadProgressDelegate>{ __strong Facebook *facebook_; }
の様にして、
- (void)request:(FBRequest *)request didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{ NSLog(@"%f", (float)totalBytesWritten / (float)totalBytesExpectedToWrite); }
とすればいいです。
しかし、Categoryって便利だなぁ。
追記
の方が詳しかった.ってか検索能力が低くて引っかからなかっただけだなぁ….FBRequestDelegateをextendsしてるところが違うだけ.