构建WebDriverAgentRunner时遇UIUserNotification相关API废弃报错求助
解决WebDriverAgentRunner构建时UIUserNotification相关API废弃报错
核心修改思路
UIUserNotificationTypeAlert、UIUserNotificationSettings、registerUserNotificationSettings:是iOS 10起被废弃的API,需替换为UserNotifications框架的新接口,具体修改步骤如下:
1. 定位目标代码文件
在WebDriverAgent项目中,找到使用上述废弃API的文件(通常为FBApplicationCommands.m或FBSettingsCommands.m)。
2. 导入新框架头文件
在文件顶部添加:
#import <UserNotifications/UserNotifications.h>
3. 替换废弃枚举与代码逻辑
- 将
UIUserNotificationTypeAlert替换为UNAuthorizationOptionAlert(若需同时启用声音、徽章权限,可组合为UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) - 替换原通知注册代码:
原废弃代码示例:
UIUserNotificationType types = UIUserNotificationTypeAlert; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [application registerUserNotificationSettings:settings];
替换为iOS 10+兼容的新代码:
UNAuthorizationOptions options = UNAuthorizationOptionAlert; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) { // 按需处理权限请求结果 }]; // 可选:立即获取当前通知设置状态 [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { // 按需处理设置结果 }];
4. 兼容低版本iOS(可选)
若需支持iOS 10以下系统,添加版本判断:
if (@available(iOS 10.0, *)) { UNAuthorizationOptions options = UNAuthorizationOptionAlert; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {}]; } else { UIUserNotificationType types = UIUserNotificationTypeAlert; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [application registerUserNotificationSettings:settings]; }
5. 重新构建项目
清理Xcode缓存(Cmd+Shift+K),然后重新构建WebDriverAgentRunner。
内容的提问来源于stack exchange,提问作者Oguzhan K
相关产品推荐
相关产品推荐

