#if __has_include(<UIKit/UIKit.h>)
NSLog(@"包含");
#else
NSLog(@"不包含");
#endif
開(kāi)發(fā)的時(shí)候文件包含可能沒(méi)有對(duì)應(yīng)的框架那么就可以用這個(gè)來(lái)判斷是否有沒(méi)有做對(duì)應(yīng)的判斷Demo:這個(gè)是iOS10的推送
// iOS 10 兼容
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
// 使用 UNUserNotificationCenter 來(lái)管理通知
UNUserNotificationCenter *uncenter = [UNUserNotificationCenter currentNotificationCenter];
// 監(jiān)聽(tīng)回調(diào)事件
[uncenter setDelegate:self];
//iOS 10 使用以下方法注冊(cè),才能得到授權(quán)
[uncenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionBadge+UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
//TODO:授權(quán)狀態(tài)改變
NSLog(@"%@" , granted ? @"授權(quán)成功" : @"授權(quán)失敗");
}];
// 獲取當(dāng)前的通知授權(quán)狀態(tài), UNNotificationSettings
[uncenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%s\nline:%@\n-----\n%@\n\n", __func__, @(__LINE__), settings);
/*
UNAuthorizationStatusNotDetermined : 沒(méi)有做出選擇
UNAuthorizationStatusDenied : 用戶(hù)未授權(quán)
UNAuthorizationStatusAuthorized :用戶(hù)已授權(quán)
*/
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
NSLog(@"未選擇");
} else if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
NSLog(@"未授權(quán)");
} else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
NSLog(@"已授權(quán)");
}
}];
#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
對(duì)應(yīng)的宏
#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)