NightFuryLxD
NightFuryLxD

Reputation: 845

IOS warning: instance method 'userNotificationCenter(_:willPresent:withCompletionHandler:)' nearly matches optional requirement

In an iOS project, I have implemented the following delegate method to handle Notifications when app is in foregorund:

func userNotificationCenter (_ pNotificationCenter : UNUserNotificationCenter, willPresent pNotification : UNNotification, withCompletionHandler pCompletionHandler : @escaping (UNNotificationPresentationOptions) -> Void) -> Void
{
    // When app is in foreground, notification is directly sent to 
    // the app by invoking this delegate method. 
}

Usually, this warning means a simple spelling error which causes the compiler to get confused with the method specification in protocol. But I've verified all that from the documentation.

Out of curiosity, I wanted to check what Xcode suggests... and it's the following delegate method.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (Int) -> Void) {
}

Notice the Int parameter in the escaping closure. But this is wrong, right? The Apple documentation (linked above) explicitly states the escaping closure's parameter is UNNotificationPresentationOptions. And UNNotificationPresentationOptions is not an enum (it's a struct), so the closure cannot take Int, yet Xcode suggested it.

Xcode also gave the following suggestion:

Make 'userNotificationCenter(_:willPresent:withCompletionHandler:)' private to silence this warning

but why? I have implemented the right delegate method. How can I fix this warning?

Edit1: Here's a google drive link which has a tiny version of the Xcode project I'm working on. After downloading the zip file, please follow the steps to setup and reproduce the warning:

  1. Download the zip file
  2. Unzip the file and place its contents (2 folders - SampleBuilds & SampleWorkspace) in /tmp folder of your Mac (this is to prevent errors due to difference in paths if placed in home folder).
  3. Now, navigate to SampleBuilds->SampleWorkspace->TWInt->IOS_Simulator-arm64
  4. Open SampleProject.xcodeproj
  5. Clean (Cmd + Shift + K) and Build (Cmd + B) the SampleApp target. (When building multiple times, the warning may not appear. Always clean and build)

Upvotes: 0

Views: 408

Answers (1)

Gerasim
Gerasim

Reputation: 21

completionHandler(Int(UNNotificationPresentationOptions.banner.rawValue))

Upvotes: 0

Related Questions