iosswiftxcode16

Cannot Extend UIApplication to Conform to Custom Protocol in Xcode 16


I am trying to extend UIApplication to conform to a custom protocol AppSwitcher for handling URL opening. This code worked fine in Xcode 15, but after upgrading to Xcode 16, it is causing errors.

public protocol AppSwitcher {
    func open(
        _ url: URL,
        options: [UIApplication.OpenExternalURLOptionsKey: Any],
        completionHandler completion: ((Bool) -> Void)?
    )
}

extension UIApplication: AppSwitcher {}

enter image description here

n Xcode 16, I can no longer extend UIApplication to conform to my AppSwitcher protocol without encountering errors. It worked perfectly in Xcode 15.


Solution

  • This is a change in the iOS SDK. The open method now has more annotations on its completion handler - @MainActor and @Sendable.

    You should add those to your protocol method too.

    public protocol AppSwitcher {
        @MainActor
        func open(
            _ url: URL,
            options: [UIApplication.OpenExternalURLOptionsKey: Any],
            completionHandler completion: (@MainActor @Sendable (Bool) -> Void)?
        )
    }
    

    Also consider using the async version of the open method in your protocol.

    public protocol AppSwitcher {
        @MainActor
        func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any]) async -> Bool
    }
    

    If this change breaks some other parts of your code, that means you were not using open in a concurrency-safe way in the first place.