How can I cast an error thrown when trying to start a Live Activity as an ActivityAuthorizationError?

79 Views Asked by At

I have implemented Live Activity in my app but have encountered occasional errors when a user attempts to start a Live Activity. According to Apple's documentation, the error thrown should be an ActivityAuthorizationError but this has not been my experience in testing. I have been able to cast it as an NSError or an Error, but not as an ActivityAuthorizationError or a LocalizedError.

The code I've been using to start the Live Activity is pretty straightforward I think:

do {
    let _ = try Activity.request(attributes: activityAttributes, 
                                 contentState: initialContentState) as Activity<MyActivityAttributes>
   } catch {
    print(error.localizedDescription)
    // I would like to present an alert that provides a failure reason and recovery suggestion.
   }

In my testing I can see the following error message:

Error Domain=com.apple.ActivityKit.ActivityInput Code=1 "(null)" 
    UserInfo={NSUnderlyingError=0x600003b17d20 {Error Domain=com.apple.ActivityKit.ActivityAuthorization Code=4 "(null)"}}

The error shown in the UserInfo seems to be an ActivityAuthorization error, but I can only cast it as an NSError or Error.

I would really like to provide the specific failure reason and recovery suggestion to users who experience a failure.

Has anyone else come across this? Are there suggestions on how to actually get access to the ActivityAuthorizationError type?

Thank you all.

0

There are 0 best solutions below