如何查看 Facebook iOS 上传的进度?

发布于 2024-12-12 18:04:04 字数 101 浏览 0 评论 0原文

我正在使用 Facebook iOS SDK 并使用 Graph API 将视频上传到 Facebook。

上传工作非常正常,但我可以跟踪上传进度,以便在进度栏中反映进度。

I'm using the Facebook iOS SDK and using the Graph API to upload videos to Facebook.

The uploading is working perfectly fine, but can I keep track of the progress of the upload so I can reflect the progress in a progress bar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-12-19 18:04:04

这是一个老问题,但您想要做的事情可以使用最新的 Facebook iOS SDK v3.9 来实现。 (2013 年 10 月 27 日)

本质上,FBRequestConnection 公开了一个属性 urlRequest (NSMutableURLRequest),您可以使用它来发送任何其他第三方网络框架甚至 Apple 提供的数据。

https://developers.facebook.com/docs/reference/ios/current/class/ FBRequestConnection#urlRequest

这是我如何使用 AFNetworking 1.x 获取进度回调的示例。

准备请求正文

NSDictionary *parameters = @{ @"video.mov": videoData,
                              @"title": @"Upload Title",
                              @"description": @"Upload Description" };

创建 FBRequest

FBRequest *request = [FBRequest requestWithGraphPath:@"me/videos" 
                                          parameters:parameters
                                          HTTPMethod:@"POST"];

生成 FBRequestConnection(取消并提取 URLRequest)

FBRequestConnection *requestConnection = [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
}];
[requestConnection cancel];

NSMutableURLRequest *urlRequest = requestConnection.urlRequest;

使用 AFNetworking HTTPRequestOperation

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  // Do your success callback.
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  // Do your failure callback.
}];

设置进度回调

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
  NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

开始操作

[[APIClient sharedInstance] enqueueHTTPRequestOperation:operation];
// APIClient is a singleton class for AFHTTPClient subclass

This is an old question but what you're trying to do is possible with latest Facebook iOS SDK v3.9. (27 Oct 2013)

Essentially, FBRequestConnection exposes a property urlRequest (NSMutableURLRequest) that you can use to send out the data any other third party networking frameworks or even the ones Apple provided.

https://developers.facebook.com/docs/reference/ios/current/class/FBRequestConnection#urlRequest

Here's an example how I get progress callbacks using AFNetworking 1.x.

Prepare Request Body

NSDictionary *parameters = @{ @"video.mov": videoData,
                              @"title": @"Upload Title",
                              @"description": @"Upload Description" };

Create FBRequest

FBRequest *request = [FBRequest requestWithGraphPath:@"me/videos" 
                                          parameters:parameters
                                          HTTPMethod:@"POST"];

Generate FBRequestConnection (Cancel & Extract URLRequest)

FBRequestConnection *requestConnection = [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
}];
[requestConnection cancel];

NSMutableURLRequest *urlRequest = requestConnection.urlRequest;

Use AFNetworking HTTPRequestOperation

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  // Do your success callback.
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  // Do your failure callback.
}];

Set Progress Callback

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
  NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

Start the operation

[[APIClient sharedInstance] enqueueHTTPRequestOperation:operation];
// APIClient is a singleton class for AFHTTPClient subclass
七七 2024-12-19 18:04:04

在查看 NSURLConnection 后,我终于找到了一种方法。这意味着在 FBRequest.h 和 FBRequest.m 文件中添加以下代码来创建新的委托。

在 FBRequest.m 文件的底部有 NSURLConnectionDelegate 的所有方法。在此添加此代码:

- (void)connection:connection
   didSendBodyData:(NSInteger)bytesWritten
 totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
    float percentComplete = ((float)totalBytesWritten/(float)totalBytesExpectedToWrite);

    if ([_delegate respondsToSelector:@selector(request:uploadPercentComplete:)])
    {
        [_delegate request:self uploadPercentComplete:percentComplete];
    }
}

现在将其放入 FBRequest.h 类中以创建一个新的 FBRequest 委托:

/**
 * Called a data packet is sent
 *
 * The result object is a float of the percent of data sent
 */
- (void)request:(FBRequest *)request uploadPercentComplete:(float)per;

这位于 FBRequest.h 文件的底部:

@protocol FBRequestDelegate <NSObject>

@optional

现在您所要做的就是在代码中的任何位置调用这个新委托,例如您可以使用任何其他 FBRequest 委托,它会给您一个从 0.0 到 1.0(0% 到 100%)的浮动值。

奇怪的是 Facebook API 没有这个(以及上传取消,我在这里找到了如何做如何使用 Facebook iOS SDK 取消正在进行的视频上传?),因为它并不那么棘手。

享受!

I've finally found a way of doing this after looking around in NSURLConnection. It means adding the following code inside of the FBRequest.h and FBRequest.m files to create a new delegate.

At the bottom of the FBRequest.m file there are all of the methods for NSURLConnectionDelegate. Add this code here:

- (void)connection:connection
   didSendBodyData:(NSInteger)bytesWritten
 totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
    float percentComplete = ((float)totalBytesWritten/(float)totalBytesExpectedToWrite);

    if ([_delegate respondsToSelector:@selector(request:uploadPercentComplete:)])
    {
        [_delegate request:self uploadPercentComplete:percentComplete];
    }
}

Now put this in the FBRequest.h class to create a new FBRequest delegate:

/**
 * Called a data packet is sent
 *
 * The result object is a float of the percent of data sent
 */
- (void)request:(FBRequest *)request uploadPercentComplete:(float)per;

This goes at the bottom of the FBRequest.h file after:

@protocol FBRequestDelegate <NSObject>

@optional

Now all you have to do is call this new delegate anywhere in your code like you would any other FBRequest delegate and it will give you a float from 0.0 to 1.0 (0% to 100%).

Strange that the Facebook API doesn't have this (along with upload cancel which I found out how to do here How to cancel a video upload in progress using the Facebook iOS SDK?) as it's not that tricky.

Enjoy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文