#if __has_include(<UIKit/UIKit.h>)
NSLog(@"包含");
#else
NSLog(@"不包含");
#endif
开发的时候文件包含可能没有对应的框架那么就可以用这个来判断是否有没有做对应的判断Demo:这个是iOS10的推送
// iOS 10 兼容
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter *uncenter = [UNUserNotificationCenter currentNotificationCenter];
// 监听回调事件
[uncenter setDelegate:self];
//iOS 10 使用以下方法注册,才能得到授权
[uncenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionBadge+UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
//TODO:授权状态改变
NSLog(@"%@" , granted ? @"授权成功" : @"授权失败");
}];
// 获取当前的通知授权状态, UNNotificationSettings
[uncenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%s\nline:%@\n-----\n%@\n\n", __func__, @(__LINE__), settings);
/*
UNAuthorizationStatusNotDetermined : 没有做出选择
UNAuthorizationStatusDenied : 用户未授权
UNAuthorizationStatusAuthorized :用户已授权
*/
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
NSLog(@"未选择");
} else if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
NSLog(@"未授权");
} else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
NSLog(@"已授权");
}
}];
#endif
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
UIUserNotificationType types = UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
UIRemoteNotificationType types = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}
#pragma clang diagnostic pop
对应的宏
#define XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8 __has_include(<UserNotifications/UserNotifications.h>)
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
网友评论