Android vs iOS Development: APIs parallels (Part 1) Intent vs NSNotification
In Square1 we get requests for a new mobile app almost every week and the requirement is always to have it supporting both Android and iPhone. Not being too keen on using cross platform frameworks we always develop our apps using native Java or Objective-c APIs. Having implemented the same requirements on both Android and iPhone several times, I have noticed how the two platforms have taken similar approaches when it comes to solving common problems. In this series of posts I am going to show how to achieve the same result on both platforms focusing on the similarities of the two SDKs.
Intents and NSNotifications
When developing an app it is a common requirement to notify an event to various components at the same time. Let's think for example of when a user logs in or logs out or when a paid feature is purchased. It is common in this case to have to update several sections of the app to reflect the new state.
The way I normally handle this scenario is by sending an Intent with the LocalBroadcastManager on Android and by sending an NSNotification with the NSNotificationCenter on iOS.
There are 4 main aspects when dealing with both Intents and NSNotifications:
Register to receive events
Create and Send the event
Receive and Handle the event
The first step is to define a string identifier for the event and register to receive notifications. Let's call this event USER_LOGGEDIN_STATE_CHANGED.
On Android the code will look like:
public class MyActivity extends Activity { public static final String USER_LOGGEDIN_STATE_CHANGED = "USER_LOGGEDIN_STATE_CHANGED"; public void register(){ LocalBroadcastManager mngr = LocalBroadcastManager.getInstance(this); mngr.registerReceiver( mMessageReceiver, new IntentFilter(USER_LOGGEDIN_STATE_CHANGED)); } }
On iOS the code will look like:
@implementation MyViewController -(void) register { center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(messageReceiver:) name:USER_LOGGEDIN_STATE_CHANGED object:nil]; } @end
Once the registration is completed events can be created and sent from anywhere in the application.
On Android create an Intent, add in any extra data and broadcast:
Intent intent = new Intent(USER_LOGGEDIN_STATE_CHANGED); intent.putExtra("state", "User is logged in"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
On iOS send an NSotification with any extra data added to the userInfo dictionary:
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"User is logged in" forKey:@"state"]; [[NSNotificationCenter defaultCenter] postNotificationName: @"USER_LOGGEDIN_STATE_CHANGED" object:nil userInfo:userInfo];
Receiving the notification is the part that differs the most in between Android and iPhone. On Android when calling
manager = LocalBroadcastManager.getInstance(this); manager.registerReceiver(mMessageReceiver)
we have passed mMessageReceiver, an instance of a class that extends the BroadCastReceiver class. Sending a broadcast will call in the BroadCastReceiver method
onReceive(Context context, Intent intent)
passing in the Intent that was broadcasted.
[[NSNotificationCenter defaultCenter] addObserver:self
we have passed in an instance of a class that will receive the notification ( in the example is self) and a @selector for the method of that class that will be called when the Notification is sent . Sending a Notification will call in the method
[self messageReceiver:(NSNotification*)notification
To keep your implementation clean and save resources It is good practice to deregister when there is no longer a need to receive the notifications. This is achieved in a similar way on both Android and iOS.
manager = LocalBroadcastManager.getInstance(this); manager.unregisterReceiver(mMessageReceiver);
*center = [NSNotificationCenter defaultCenter]; [center removeObserver:self];
Well there is a lot more that could be said on this subject but this covers the basics on Intents and NSNotifications!
- Roberto Prato @pratorob
Square1 are a creative online and mobile software development company based in Dublin, Ireland.