skip to Main Content

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.

  • Has anyone else experienced this in Xcode 16?
  • Is this a change in Swift 5.9 or the iOS SDK?
  • What’s the reason behind this change, and are there any workarounds to achieve the same result?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. FYI if you want to compile code in Xcode15 after updating on

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

    you can use #if compiler(>=x.x.x)

    for xcode 15.1 compiler is 5.9.2

    /Applications/Xcode
    15.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
    –version swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5) Target:
    arm64-apple-macosx14.0

    for xcode 16.1 compiler is 6.0.2

    /Applications/Xcode
    16.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
    –version swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4)

    #if compiler(>=6.0.0)
    func open(
        _ url: URL,
        options: [UIApplication.OpenExternalURLOptionsKey: Any],
        completionHandler completion: (@MainActor @Sendable (Bool) -> Void)?
    )
    #else
    func open(
        _ url: URL,
        options: [UIApplication.OpenExternalURLOptionsKey: Any],
        completionHandler: ((Bool) -> Void)?
    )
    #endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search