我试图将我的目标C AppDelegate转换为Swift AppDelegate。所以我删除了main.m文件并将所有AppDelegate代码转换为swift。我试图运行该项目,然后发生此错误。
无法找到协议声明 ' UNUserNotificationCenterDelegate'
在他们生成的-Swift.h文件中,这是他们展示的代表
- (void)userNotificationCenter:(UNUserNotificationCenter * _Nonnull)center willPresentNotification:(UNNotification * _Nonnull)notification withCompletionHandler:(void (^ _Nonnull)(UNNotificationPresentationOptions))completionHandler SWIFT_AVAILABILITY(ios,introduced=10.0);
这就是我写的
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
handleNotification(notification.request.content.userInfo)
}
请帮助我了解面临的问题。
我试过,清理,重新启动Xcode并重新启动我的Mac。我使用的是Swift 3.2。
如果我对代表发表评论,则没有错误。
答案 0 :(得分:2)
奇怪的修复但是通过在我的桥接头中导入#import,问题得到了解决。
答案 1 :(得分:1)
这是您需要继续的方式
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
registerForPushNotification()
return true
}
/**Register for push notification*/
func registerForPushNotification() {
let userNotification = UNUserNotificationCenter.current()
userNotification.delegate = self
userNotification.requestAuthorization(options: [.sound, .badge, .alert]) { (status, error) in
if error == nil {
DispatchQueue.main.async { // From iOS 10 onwards we need to call pushNotification registration method in main thread.
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
}
// MARK: - UNUserNotificationCenterDelegate methods
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.sound, .badge, .alert])
}
}
感谢。
答案 2 :(得分:0)
您需要导入以下框架:import UserNotifications