The messaging mechanism in iOS development is generally subdivided into three broad categories:
-
Local notifications
-
Push Notifications
-
Notification Center
There is no direct connection between the three, Notification Center is to register an observer in the application of all objects broadcast messages, through the notification center can be well coordinated between different objects of message communication.
Attention:
Each application has only one notification hub (Nsnotificationcenter) instance
Both agent and notification hubs coordinate message communication between objects, and notification hubs are functionally similar to proxies, what are the distinctions between the two choices?
The agent corresponds to the relationship of one to the other, and the notification is a a-to-many relationship, so when a change is encountered it causes more
Changes in the functionality of our best choice of notifications.
For example:
-
Change skin effect on QQ Music app
-
Reading software changes font size effect
reasonable use of both in the actual development to prevent the overqualified
A notice of the successful implementation of the three elements must be
-
Notify Publishers
-
Notification Center
-
Notification recipients
A full notification consists of three attributes
-
Name: Names of notifications
-
Object: The publisher of the notification
-
UserInfo: The information content of the notifier to the notifying recipient
Notification recipient Registration notification method to notification center
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject; observer:监听器,即谁要接收这个通知aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入 aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知 anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知
Notification hubs three ways to publish notifications
? - (void)postNotification:(NSNotification *)notification;? - (void)postNotificationName:(NSString *)aName object: (id)anObject;? - (void)postNotificationName:(NSString *)aName object:(id)anObjectuserInfo:(NSDictionary *)aUserInfo;
Dealloc method under Notification
After we register in the Notification Center, the notification center is very sensitive, when the condition is triggered will continue to send the message to the registrant, but when our registrant has been released, this time will cause a wild pointer, triggering the program crashes, so when our object is released, the object should be released to notification Center registration notification.
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
In addition, there are two ways to unregister:
- (void)removeObserver:(id)observer;- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;//第二个多了一个name的参数可以具体到该对象中某个通知
A common practical application of notification hubs
1. Device rotation
UIDeviceOrientationDidChangeNotification
2. Keyboard operation
Remind
The order of normal execution of the notification is: the recipient registers the notification in the Notification Center and the notifier posts the notification in the Notification Center ——— > releases the Notification Center's registration notice. An error in order will occur.
iOS notification hubs