7.必要知识
7.1 APNS
目前在8.0以后的推送需要以下几个步骤:
1.使用registerUserNotificationSettings: & registerForRemoteNotifications方法
1
2
3
4
5if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
关于registerForRemoteNotifications方法有以下说明,它会需要实现两个delegate方法,并且方法能够被执行的条件是通过registerUserNotificationSettings:方法成功注册用户的notification,或者enabled for Background App Refresh。
// Calling this will result in either application:didRegisterForRemoteNotificationsWithDeviceToken: or application:didFailToRegisterForRemoteNotificationsWithError: to be called on the application delegate. Note: these callbacks will be made only if the application has successfully registered for user notifications with registerUserNotificationSettings:, or if it is enabled for Background App Refresh
2.实现delegate方法:每个APP都不同,就不写了。
7.2 block tips
- 多使用typedef来定义block,因为简洁、可复用、对于结构相同的block可以通过命名来区分。
- 在定义API时多用handle block来降低代码分散程度。
- 注意在使用block时循环引用对象
1 |
|
1 | #import <Foundation/Foundation.h> |