I’m kinda new with ios dev and I am trying to disable a button if the user is already authorized with an access token. I just don’t know if there is a clean way of doing it. Right now I have a check in my viewWillAppear that calls the api client object isAuthorized: that returns a bool and sets the button’s enabled to yes/no. In the isAuthorized I make a test http request if a access token exists but since I have to wait for the completion block the timing is off and button is disabled when it shouldn’t be. Should I setup an NSNotification or should I just make the test http request in the viewWillAppear instead of in the client? Or is there a better solution?
Thanks
First, good that your view controller just talks to a “client” object and that the client object talks to the server. That’s the correct layout. Now how to design the UI portion:
Your client object needs an internal sense of “is my authorization current?” That may mean it’s checked it once, or it’s checked in in the last X minutes. Depends on your situation.
When you call
isAuthorized, if the auth isn’t current, you immediately returnNOand then make your request.When the request comes in, you call
setAuthorized:, which will post KVO notifications.Your view controller KVO observes
isAuthorizedand updates the button as needed. (Alternately you could use a delegate method or a notification.)If you want to distinguish “not authorized” from “I don’t know,” just create an 3-option enum and a method
authorizationStateinstead ofisAuthorized.