【问题标题】:SLRequest Progress Callback ( iOS 6 )SLRequest 进度回调(iOS 6)
【发布时间】:2012-12-14 08:45:33
【问题描述】:

我正在使用 iOS6 的新社交 API 实现 Facebook 视频上传,一切正常,除了我没有找到知道上传进度的方法......苹果似乎唯一的 API提供的是,如果上传完成或失败,有可能他们是如此--------以至于他们没有提供知道它的方法吗?

我也没有找到关于这个主题的任何问题,不可能只有我对此感兴趣,如果有人知道其他任何事情,请告诉我们。

谢谢!

【问题讨论】:

    标签: ios facebook upload progress social


    【解决方案1】:

    我一直在努力解决这个问题,终于让它工作了。我想我会分享我的 sn-p:

    -(void)sendTwitter {
    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
                                  ACAccountTypeIdentifierTwitter];
    
    [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if (granted == YES) {
            NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
            if ([arrayOfAccounts count] > 0) {
                twitterAccount = [arrayOfAccounts lastObject];
    
                NSString *status = @"Secrets";
                UIImageView *uploadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 640, 480)];
                UIImage *uploadImage = [image resizedImageToFitInSize:uploadImageView.bounds.size scaleIfSmaller:NO];
    
                NSURL *requestURL = [NSURL URLWithString:@"https://upload.twitter.com/1.1/statuses/update_with_media.json"];
    
                SLRequest *postRequest = [SLRequest
                                          requestForServiceType:SLServiceTypeTwitter
                                          requestMethod:SLRequestMethodPOST
                                          URL:requestURL parameters:nil];
                [postRequest addMultipartData:UIImageJPEGRepresentation(uploadImage, 0.6) withName:@"media[]" type:@"multipart/form-data" filename:nil];
                [postRequest addMultipartData:[status dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data" filename:nil];
                [postRequest setAccount:twitterAccount];
    
                NSURLRequest *preparedRequest = [postRequest preparedURLRequest];
    
                AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:preparedRequest];
    
                [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
                    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
                }];
                [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                        NSLog(@"Twitter upload success");
                        [self completedSendingPhotos];
                    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                        NSLog(@"Twitter upload error");
                    }];
    
                [operation start];
            }
        }
    }];
    

    }

    【讨论】:

      【解决方案2】:

      使用preparedURLRequest 获取NSURLRequest 对象。使用NSURLConnection 手动发送请求可以让您提供一个可以处理诸如connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: 之类的委托方法的委托。

      【讨论】:

      • 嗨 Jesper,谢谢,使用这种方法有帮助,但是我必须使用 AFNetworking 来处理 HTTP 操作,为什么在 ---- Apple 不能在 SLRequest 操作中包含进度块?
      • 一个好问题。也许是因为如果您需要那种级别的反馈,您可能还对其他委托方法感兴趣。现在有一条非常简单的路径可以减少代码,还有一条可以通过NSURLRequest 对细节进行大量控制的路径。为了只有一条路径,您必须能够在块 API(或其他东西)中复制所有这些委托方法。但你是对的,简单的路径本身可能会更强大。
      【解决方案3】:

      对于那些感兴趣的人来说,我是如何使用 AFNetworking 的,在拥有一个 SLRequest 对象之后,我使用@Jesper 的方式来获取一个进度监听器。

      NSURLRequest* preparedRequest = [uploadRequest preparedURLRequest];
      
      AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:preparedRequest];
      [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
          NSLog(@"%lld bytes out of %d sent.", totalBytesWritten, fileSize);
          float progress = totalBytesWritten/(float)fileSize;
      }];
      [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSLog(@"Facebook upload success");
          });
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Facebook upload error");
          });
      }];
      
      [operation start];
      

      希望有帮助!

      【讨论】:

        猜你喜欢
        • 2012-09-16
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 2012-09-11
        • 1970-01-01
        相关资源
        最近更新 更多