Question

I am using the Evernote API, and I have created a helper class that will contain the Evernote API-specific methods in an effort to keep code as abstracted as possible.

But there is one method that needs to be called from the view controller to show a login form, as well as an alertView if an error occurs. Here is the code:

- (IBAction)loginToEvernote:(id)sender {

EvernoteSession *session = [EvernoteSession sharedSession];
[session authenticateWithViewController:self completionHandler:^(NSError *error) {
    if (error || !session.isAuthenticated) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Could not authenticate"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

    } else {

    }
}];

}

It seems to me the "authenticateWithViewController:completionHandler" method needs to be in a view controller, in order to show the modal view controller that contains the login information.

Again, this is a method from an API, so I cannot alter it.

How can I move this method over to the helper class? At first I thought delegation, but I am already making this view controller the delegate of the helper class in order to send other error messages to it, and making the helper class and viewcontroller delegates of each other seems code-smelly to me, if it's even possible at all.

Was it helpful?

Solution

If the Evernote API requires a view controller, and you want complete abstraction, I would have a method in the helper class like this something like this in the helper class:

- (void)authenticateWithViewController:(UIViewController *)viewController

In this method, you can encapsulate any shared behaviours, calling through to the Evernote session API passing the viewController parameter to the method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top