IOS10正式版宣布以後,網上各類適配XCode8和IOS10的文章滿天飛。但關於IOS10適配長途推送的文章卻不多。iOS10關於推送的修正照樣異常年夜的,新增了UserNotifications Framework,明天就聯合本身的項目,說一說現實適配的情形。
1、Capabilities中翻開Push Notifications 開關
在XCode7中這裡的開關不打卡,推送也是可以正常應用的,然則在XCode8中,這裡的開關必需要翻開,否則會報錯:
Error Domain=NSCocoaErrorDomain Code=3000 "未找到運用法式的“aps-environment”的受權字符串" UserInfo={NSLocalizedDescription=未找到運用法式的“aps-environment”的受權字符串}
翻開後會生成entitlements文件,在這裡可以設置APS Environment
2、推送的注冊
起首引入UserNotifications Framework,
import <UserNotifications/UserNotifications.h>
iOS10修正了注冊推送的辦法,這裡我們又要對分歧版天職別停止設置了。在application didFinishLaunchingWithOptions辦法中修正之前的推送設置(我只完成了iOS8以上的設置)
if (IOS_VERSION >= 10.0) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error) { DLog(@"request authorization succeeded!"); } }]; } else { if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { //IOS8,創立UIUserNotificationSettings,並設置新聞的顯示類類型 UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:notiSettings]; } }
3、UNUserNotificationCenterDelegate署理完成
在iOS10中處置推送新聞須要完成UNUserNotificationCenterDelegate的兩個辦法:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
個中第一個辦法為App在前台的時刻收到推送履行的回調辦法,第二個為App在後台的時刻,點擊推送信息,進入App後履行的 回調辦法。
之前處置推送,信息是在userInfo參數中,而新辦法中注解上看並沒有這個參數,其實我們一樣可以獲得到userInfo,以下:
/// App在前台時刻回調 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; [self handleRemoteNotificationForcegroundWithUserInfo:userInfo]; } /// App在後台時刻點擊推送挪用 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; [self handleRemoteNotificationBackgroundWithUserInfo:userInfo]; }
完成下面三個步調的設置,關於iOS10的推送設置根本就適配了。要想自界說Notification Content或許完成其他NotificationAction請參考其他文章。這裡只是做了對iOS10的適配。
本文已被整頓到了《iOS推送教程》,迎接年夜家進修浏覽。
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐本站。
【iOS10 適配長途推送功效完成代碼】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!