Pregunta

I have successfully integrate BOX SDK in my ios app, but facing an issue while uploading a file to box, my issue is i did not able to hide progress indicator/hud after success full upload in success block. I am not much aware of block code. I have used this code for uploading file

-(void)upload{

     BoxFilesRequestBuilder *builder = [[BoxFilesRequestBuilder alloc] init];
     builder.name = @"Logo_Box_Blue_Whitebg_480x480.jpg";
     builder.parentID = folderID;

     NSString *path = [[NSBundle mainBundle] pathForResource:@"Logo_Box_Blue_Whitebg_480x480.jpg" ofType:nil];
     NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:path];
     NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
     long long contentLength = [[fileAttributes objectForKey:NSFileSize] longLongValue];


     [[BoxSDK sharedSDK].filesManager uploadFileWithInputStream:inputStream contentLength:contentLength MIMEType:nil requestBuilder:builder success:fileBlock failure:failureBlock progress:nil];

}

On successful upload this method is called and i want to hide my progress hud in this block, how to do this.

BoxFileBlock fileBlock = ^(BoxFile *file)
{
      // manipulate resulting BoxFile
};

BoxAPIJSONFailureBlock failureBlock = ^(NSURLRequest *request, NSHTTPURLResponse *response,  NSError *error, NSDictionary *JSONDictionary)
{
      // handle failed upload
};
¿Fue útil?

Solución

Have you tried hiding HUD both from success and failure blocks? Either one of them will get called eventually.

In your code above, I can't see any HUD object or property It is also not clear if upload method belongs to the ViewController class with HUD pointer.

Option A

Assuming that upload method belongs to the view controller with HUD.

class declarations in MyAwesomeBOXUploadViewController.m (replace with your view controller class name :) )

@interface MyAwesomeBOXUploadViewController ()
@property (nonatomic, readwrite, strong) MyHUD *uploadHUD;
@end

from success failure blocks MyAwesomeBOXUploadViewController implementation.

// using weak pointer to self to avoid retain loop
__weak MyViewControllerClass *weakSelf = self;
BoxFileBlock fileBlock = ^(BoxFile *file)
{
    [weakSelf.uploadHUD stop];
     // manipulate resulting BoxFile
};

BoxAPIJSONFailureBlock failureBlock = ^(NSURLRequest *request, NSHTTPURLResponse *response,  NSError *error, NSDictionary *JSONDictionary)
{
     [weakSelf.uploadHUD stop];
     // handle failed upload
};

Option B

If upload method is not part of view controller and you don't have HUD pointer, then you need to propagate success and fail callbacks up to the view Controller. To propagate success/failure you have a bunch of options. One might consider using block callbacks or nsnotifications.

BTW, Blocks can be extremely useful and Apple provides great documentation on block programming: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top