Skip to content

Instantly share code, notes, and snippets.

@casspangell
Last active September 15, 2015 17:50
Show Gist options
  • Save casspangell/712d23b4569efe7b561b to your computer and use it in GitHub Desktop.
Save casspangell/712d23b4569efe7b561b to your computer and use it in GitHub Desktop.
Completion Block with Data Return
.h = - (void)signInAccountWithUserName:(NSString *)userName
password:(NSString *)password
completion:(void (^)(BOOL success))completionBlock;
.m = - (void)signInAccountWithUserName:(NSString *)userName
password:(NSString *)password
completion:(void (^)(BOOL success))completionBlock
{
// Notice that we are passing a BOOL back to the completion block.
if (completionBlock != nil) completionBlock(loginSuccessful);
}
//Call the method:
[self signInAccountWithUserName:@"Bob"
password:@"BobsPassword"
completion:^{
[self doMethod]; //do something like call a method
}];
OR
[self signInAccountWithUserName:@"Bob"
password:@"BobsPassword"
completion:^(BOOL success) {
if (success) {
[self doMethod]; //do something like call a method
} else {
// do something like display an alert
}
}];
//Block that passes in an MKMapView and returns NSData called mapData
.h = - (void)ImageFromMapView:(MKMapView*)mapView completion:(void (^)(NSData* mapData))completionBlock;
.m = - (void)ImageFromMapView:(MKMapView*)mapView completion:(void (^)(NSData* mapData))completionBlock
{
//method does what it needs to do
//self.data holds the NSData that was created inside this method
completionBlock(self.data);
}
Calling it =
[classWithMethod ImageFromMapView:mapView completion:^(NSData *mapData) {
//do something with mapData
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment