1

一旦用户从 UIActivityViewController 中选择了共享活动,但在显示共享表之前,我想执行上传任务。

具体来说,我需要上传图片的 url 才能在 Activity 中使用。

我已经进行了子类化UIActivityItemProvider,并且认为我可以在该itemForActivityType方法中进行上传,但是上传代码是基于块的,我不知道如何让它等待块完成。这甚至可能吗?这可能是一个简单的编码错误,这是漫长的一天。

当用户按下共享按钮时,我不想上传图像,因为他们可能会取消活动视图,这意味着上传的图像坐在那里没有被使用。

这是我目前拥有的代码,但它在图像上传之前返回 nil 并且在块内它不允许我为错误返回 nil:

- (id) activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{

    [self getShortUrlForUploadedImageWithCompletionHandler:^(NSString *shortUrl, NSError *error) {
        if (!error) {
            if ( [activityType isEqualToString:UIActivityTypeMail] ) {

                NSString *shareString = @"Email content here using shortUrl";

                return shareString;
            } else {
                return @"";
            }

        } else {

            return @"";
        }
    }];

    return nil;
}

-(void)getShortUrlForUploadedImageWithCompletionHandler:(NSString* (^)(NSString *shortUrl, NSError *error))completionHandler
{
    NSData *imageToUpload = UIImageJPEGRepresentation(_image, 75);
    AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kShareURL]];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"image", @"action",
                            @"simple", @"format",
                            nil];
    NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:nil parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
        [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.png" mimeType:@"image/jpeg"];
    }];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *response = [operation responseString];
        NSLog(@"response: %@",response);

        completionHandler(response, nil);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        if([operation.response statusCode] == 403){
            NSLog(@"Upload Failed");
            return;
        }
        NSLog(@"error: %@", [operation error]);

        completionHandler(nil, error);
    }];

    [operation start];
}

- - - - 编辑

我真的可以在这方面提供一些帮助。我目前的工作是在用户单击我的共享按钮时上传图像,在活动选择之前。所以他们可以取消分享,留下一张多余的上传图片,或者他们可以选择不需要上传图片的 Twitter。如果选择了电子邮件,我只需要上传图像,我认为我唯一可以这样做的地方是在 Acticity Provider 子类中。

4

1 回答 1

2

而不是实现- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType,尝试覆盖 UIActivityItemProvider 的- (id)item. 该方法将从后台线程上的 NSOperation 的 main 方法调用。

至于等到网络完成块触发,我建议您考虑使用dispatch_semaphore. 这是一个例子:

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"doing some work");
    sleep(5);
    NSLog(@"done with work");
    dispatch_semaphore_signal(semaphore);
});
double delayInSeconds = 60.0;
dispatch_time_t waitTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
NSLog(@"waiting for background thread to finish");
dispatch_semaphore_wait(semaphore, waitTime);
NSLog(@"background thread finished, or took too long");

确保只在后台线程上使用它,否则你会阻塞主线程。

于 2013-07-30T01:34:45.550 回答