How to cancel a video upload in progress using the

2019-06-06 07:42发布

问题:

I'm using the Facebook iOS SDK to post videos from my app to the user's facebook.

I'm trying to allow the user to cancel the upload after the upload has started.

These are the methods that starts the upload:

- (void)startUpload
{   
    NSArray* permissions = [[NSArray alloc] initWithObjects:
                            @"publish_stream", nil];
    [facebook authorize:permissions];
    [permissions release];
}

- (void)fbDidLogin
{
    NSString *filePath = [videoNSURL path];

    NSData *videoData = [NSData dataWithContentsOfFile:filePath];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   videoData, @"video.mov",
                                   @"video/quicktime", @"contentType",
                                   videoTitle, @"title",
                                   videoDescription, @"description",
                                   nil];
    [facebook requestWithGraphPath:@"me/videos"
                         andParams:params
                     andHttpMethod:@"POST"
                       andDelegate:self];
}

I cannot find any methods inside Facebook.h that comes with the Facebook iOS SDK that allows me to cancel the upload.

Even if I do a [facebook release], it will still not cancel the FBSession which is trying to upload the video. Which means I'll get a exc_bad_access when the upload is complete when FBSession tries to inform the facebook object that the upload is complete.

回答1:

I added the following methods:

FBRequest

(void) cancel {
   [_connection cancel];
   [_connection release], _connection = nil;
}

Facebook

(void)cancelPendingRequest {
  [_request cancel];
  [_request release], _request = nil;
}

cancelPendingRequest will allow me to cancel a video upload in progress.