0

In my iOS app, there is a condition under which I'd like the user to confirm that he wants to receive remote notifications if he hasn't already, then perform the registration (using registerUserNotificationSettings before iOS 10 or requestAuthorizationWithOptions / registerUserNotificationSettings for iOS 10+). The model will then send the device token back to my server when it requests more data.

I'm trying to use a "clean" MVC pattern. Is the following considered a best practice for when the model needs additional information from the user?

  1. The model determines the condition for remote notifications has been satisfied and determines the user hasn't confirmed wanting remote notifications.
  2. The model calls a method on the view controller (as a delegate) to ask the user to confirm wanting remote notifications.
  3. Since the confirmation process happens asynchronously, the view controller then calls a method on the model with the result.
  4. If the user confirmed, the model registers the user and handles the result. This will involve interaction between the model and app delegate, and iOS may display its own confirmation modal to the user which is ok.

An alternative would be for the model to pass a completion handler to the view controller in step 2, which is passed around and finally executed by the view controller when result is obtained. (A little tricky with UIAlertView before iOS 9 but pretty easy with UIAlertController for iOS 9+.)

Thanks for any thoughts!

ScottyB
  • 2,167
  • 1
  • 30
  • 46
  • This is a good question but answers are going to be primarily opinion-based. – Mike Taverne Feb 28 '18 at 17:52
  • the things that you say the model does ("determines the condition for remote notifications...", " model calls a method ..."), they sound like things controller should be doing.. – Milan Nosáľ Feb 28 '18 at 18:22
  • If your step #1 is truly a model function, I would have it react by sending a notification of the state change and letting a listener (controller) initiate the rest of the work. – Phillip Mills Feb 28 '18 at 18:26
  • Appreciate all the responses. To clarify, the model has the data that determines that the user can receive remote notifications, e.g., the user has logged into the service. @PhillipMills, are you saying the model should then ask the view controller for the device token: let the controller go through all the steps of registering the user for notifications with iOS, then set the device token on the model? (I could create a custom setter method on the model which would then kick off the next step of sending the device token to the server.) – ScottyB Feb 28 '18 at 20:00
  • I don't think the model should ask for anything. The controller should be "in control" and update the model as necessary. – Phillip Mills Feb 28 '18 at 21:10
  • Ah, so the model contains the state of whether the user has confirmed notifications. Based on that, the controller goes through the steps of confirming/registering for notifications, then changes the state and sets the token on the model (a real token or empty token). The model (though the custom setter on the token) sends the token to the server in another API call. I like it. Thanks! – ScottyB Feb 28 '18 at 21:42

0 Answers0