Completion handler
You will want to copy the completion block into the iVar class:
@property (nonatomic, copy) void (^completionHandler)(bool *);
Since you are saving the block, you need the non-class method to accept the block (see how to do this without breaking your singleton). An example of your method might be:
- (void)loginUserWithUsername:(NSString *)username password:(NSString *)password completion:(void(^)(bool *finished))completionBlock {
Then, when your login code finishes its work, you can call the block - here I pass self.error, a bool to the block:
- (void)finishThingsUp { // We are done with all that hard work. Lets call the block! self.completionHandler(self.error); // Clean up the blocks self.completionHandler = nil; }
A good idea
Well, this is a philosophical question, but I will say this: Blocks in Objective-C allow you to write code that performs one task and easily integrate it into many programs. If you decide not to use the completion handler in your login code, you will need a login code:
- Require classes using it to implement the protocol (as in
LoginDelegate ) - Use a different reporting system for your code, such as Monitoring Key Values ββor Notifications
- Hard code only works with one type of calling class
Any of the above approaches are fine, I believe the block-based callback system is the simplest and most flexible. It allows you to simply use your class without worrying about additional infrastructure (setting up notifications, protocol compliance, etc.), but it allows you to reuse it in other classes or programs.
Singelton
Methods starting with + in Objective-C are class methods. You cannot use class methods to control iVars, since who will own this data?
What you can do is to have a class method that always returns the same instance of that class, allowing you to have an object that can own the data, but avoid having more than one of them.
This great answer contains sample code .
Good luck