2

I'm studying UserNotifications framework and have the following method to respond to user's actions on a notification.

func userNotificationCenter (_ pNotificationCenter: UNUserNotificationCenter, didReceive pResponse: UNNotificationResponse, withCompletionHandler pCompletionHandler: @escaping () -> Void) -> Void {
    ...
}

This works well in iOS and iPadOS, but the documentation says this delegate method is not applicable for tvOS - which leads me to believe that notification banners cannot be displayed in tvOS - which is ok.

But the problem is, how do I make this compile for tvOS? I'm getting the following error

error: 'UNNotificationResponse' is unavailable in tvOS
...
error: cannot override 'userNotificationCenter' which has been marked unavailable

I tried using the @available attribute, which would include this method only for iOS and iPadOS 10.0 and above.

@available(iOS 10.0, *)

But this doesn't work. I still get the same error. Macros can work, but what's wrong in the way I've used @available?

How do I selectively disable this function for tvOS using @available?

Update1: Even if I wrap the empty function as shown below, I still get the same error.

#if os(tvOS)
func userNotificationCenter (_ pNotificationCenter: UNUserNotificationCenter, didReceive pResponse: UNNotificationResponse, withCompletionHandler pCompletionHandler: @escaping () -> Void) -> Void {

}    
#endif

Update2: Silly mistake in update 1. If I need the above code to be available only for iOS and iPadOS, I should have used

#if os(iOS)

instead of

#if os(tvOS)
NightFuryLxD
  • 847
  • 5
  • 15
  • Did you try to compile conditionally like [described here](https://stackoverflow.com/questions/35462375/compiler-condition-for-target-membership-running-in-swift/35464314#35464314) This should do... – Wolfgang Wilke Apr 20 '23 at 09:24
  • @WolfgangWilke, I tried `#if os(tvOS)`. Didn't work. Same error. This is similar to macros, right? I thought it would work... – NightFuryLxD Apr 20 '23 at 10:54
  • Did you exclude the function and the references to it compeltly??? – Wolfgang Wilke Apr 20 '23 at 12:39
  • @WolfgangWilke yes. The function doesn't contain any code (exactly as depicted in question). I wrapped the empty function with `#if os(tvOS)` and `#endif`. – NightFuryLxD Apr 20 '23 at 13:28
  • @WolfgangWilke, I have pasted the updated code in the question (under update section). – NightFuryLxD Apr 20 '23 at 13:32

2 Answers2

1

For disable selectively the feature on tvOS using the property @available, adding unavailable on tvOS, unavailable to the @available property.

@available(iOS 10.0, *, unavailableIn: .tvOS)
Grenoblois
  • 503
  • 1
  • 5
  • 19
  • 1
    Nope. That didn't work. This syntax seems to be wrong. I've got syntax errors like `error: expected declaration` and `error:expected version number` in addition to the errors reported in question. – NightFuryLxD Apr 20 '23 at 10:51
1

You declare the function if you have os(tvOS) but you want the function declared only if you have it not. In the conditional compilation you have not a direct negation. So a solution would be:

#if os(tvOS)
    
#warning("Not implemented in tvOS") // or if you want #error(...)    
        
#else
        
func userNotificationCenter (_ pNotificationCenter: UNUserNotificationCenter, didReceive pResponse: UNNotificationResponse, withCompletionHandler pCompletionHandler: @escaping () -> Void) -> Void {
....

}

So the function is only declared if you don't have tvOS.

Here you find some more examples on conditional compilation but you find a lot more in the web...

Wolfgang Wilke
  • 490
  • 3
  • 5
  • 13
  • Thanks. This definitely works. But I was hoping for a solution using @available (as mentioned in question)? Because I want some methods to be available for certain versions of the OS. #if will exclude the entire method for every version of the OS. – NightFuryLxD Apr 21 '23 at 08:51
  • 1
    After reading [this post](https://forums.swift.org/t/if-vs-available-vs-if-available/40266) in swift forums about #if, @available and #available, I see that in this case, #if must be used... since any version of tvOS should not see the userNotificationCenter(_:didReceive:withCompletionHandler:) delegate method. This is the right answer. Thank you. – NightFuryLxD May 04 '23 at 15:12