Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Add support for compiling with Xcode 16 beta 2 #24386

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
delegates.insert(delegate)
}

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation) {
for delegate in delegates {
delegate.webView?(webView, didCommit: navigation)
}
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
func webView(_ webView: WKWebView, didFail navigation: WKNavigation, withError error: Error) {
for delegate in delegates {
delegate.webView?(webView, didFail: navigation, withError: error)
}
}

func webView(
_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
didFailProvisionalNavigation navigation: WKNavigation,
withError error: Error
) {
for delegate in delegates {
delegate.webView?(webView, didFailProvisionalNavigation: navigation, withError: error)
}
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation) {
for delegate in delegates {
delegate.webView?(webView, didFinish: navigation)
}
Expand All @@ -45,23 +45,27 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
}
}

public func webView(
func webView(
_ webView: WKWebView,
respondTo challenge: URLAuthenticationChallenge
) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
let authenticatingDelegates = delegates.filter { wv in
return wv.responds(
to: #selector(WKNavigationDelegate.webView(_:didReceive:completionHandler:))
)
}

guard let firstAuthenticatingDelegate = authenticatingDelegates.first else {
return (.performDefaultHandling, nil)
completionHandler(.performDefaultHandling, nil)
return
}

// Do NOT change to `delegate.webView?(....)` the optional operator makes async-await calls crash the compiler atm!
// It must be force-unwrapped at the time of writing `January 17th, 2023`.
return await firstAuthenticatingDelegate.webView!(webView, respondTo: challenge)
firstAuthenticatingDelegate.webView?(
webView,
didReceive: challenge,
completionHandler: completionHandler
)
}

func webView(
Expand Down Expand Up @@ -103,95 +107,87 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
return .allow
}

@MainActor
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
preferences: WKWebpagePreferences
) async -> (WKNavigationActionPolicy, WKWebpagePreferences) {
preferences: WKWebpagePreferences,
decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void
) {
var res = defaultAllowPolicy(for: navigationAction)
var pref = preferences

for delegate in delegates {
// Needed to resolve ambiguous delegate signatures: https://github.com/apple/swift/issues/45652#issuecomment-1149235081
typealias WKNavigationActionSignature = (WKNavigationDelegate) -> (
(
WKWebView, WKNavigationAction, WKWebpagePreferences,
@escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void
) -> Void
)?

// Needed to detect if async implementations exist as we cannot detect them directly
if delegate.responds(
to: #selector(
WKNavigationDelegate.webView(_:decidePolicyFor:preferences:decisionHandler:)
as WKNavigationActionSignature
)
) {
// Do NOT change to `delegate.webView?(....)` the optional operator makes async-await calls crash the compiler atm!
// It must be force-unwrapped at the time of writing `January 10th, 2023`.
let (policy, preferences) = await delegate.webView!(
webView,
decidePolicyFor: navigationAction,
preferences: preferences
)
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}
let group = DispatchGroup()

pref = preferences
for delegate in delegates {
if !delegate.responds(to: #selector(webView(_:decidePolicyFor:preferences:decisionHandler:)))
{
continue
}
group.enter()
delegate.webView?(
webView,
decidePolicyFor: navigationAction,
preferences: pref,
decisionHandler: { policy, preferences in
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}

pref = preferences

group.leave()
}
)
}

return (res, pref)
group.notify(queue: .main) {
decisionHandler(res, pref)
}
}

@MainActor
func webView(
_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse
) async -> WKNavigationResponsePolicy {
decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void
) {
var res = WKNavigationResponsePolicy.allow
let group = DispatchGroup()
for delegate in delegates {
// Needed to resolve ambiguous delegate signatures: https://github.com/apple/swift/issues/45652#issuecomment-1149235081
typealias WKNavigationResponseSignature = (WKNavigationDelegate) -> (
(WKWebView, WKNavigationResponse, @escaping (WKNavigationResponsePolicy) -> Void) -> Void
)?

// Needed to detect if async implementations exist as we cannot detect them directly
if delegate.responds(
to: #selector(
WKNavigationDelegate.webView(_:decidePolicyFor:decisionHandler:)
as WKNavigationResponseSignature
)
) {
// Do NOT change to `delegate.webView?(....)` the optional operator makes async-await calls crash the compiler atm!
// It must be force-unwrapped at the time of writing `January 10th, 2023`.
let policy = await delegate.webView!(webView, decidePolicyFor: navigationResponse)
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}
if !delegate.responds(to: #selector(webView(_:decidePolicyFor:decisionHandler:))) {
continue
}
group.enter()
delegate.webView?(
webView,
decidePolicyFor: navigationResponse,
decisionHandler: { policy in
if policy == .cancel {
res = policy
}

if policy == .download {
res = policy
}
group.leave()
}
)
}

if res == .allow {
let tab = tabManager?[webView]
tab?.mimeType = navigationResponse.response.mimeType
}

return res
group.notify(queue: .main) {
decisionHandler(res)
}
}

@MainActor
public func webView(
func webView(
_ webView: WKWebView,
navigationAction: WKNavigationAction,
didBecome download: WKDownload
Expand All @@ -204,8 +200,7 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
}
}

@MainActor
public func webView(
func webView(
_ webView: WKWebView,
navigationResponse: WKNavigationResponse,
didBecome download: WKDownload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ import XCTest

sut.insert(delegate1)
sut.insert(delegate2)
_ = await sut.webView(
let e = expectation(description: "decisionHandler")
sut.webView(
anyWebView(),
decidePolicyFor: WKNavigationAction(),
preferences: WKWebpagePreferences()
preferences: WKWebpagePreferences(),
decisionHandler: { _, _ in e.fulfill() }
)
await fulfillment(of: [e])

XCTAssertEqual(delegate1.receivedMessages, [.webViewDecidePolicyWithActionPolicy])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDecidePolicyWithActionPolicy])
Expand All @@ -138,10 +141,13 @@ import XCTest

sut.insert(delegate1)
sut.insert(delegate2)
_ = await sut.webView(
let e = expectation(description: "decisionHandler")
sut.webView(
anyWebView(),
decidePolicyFor: WKNavigationResponse()
decidePolicyFor: WKNavigationResponse(),
decisionHandler: { _ in e.fulfill() }
)
await fulfillment(of: [e])

XCTAssertEqual(delegate1.receivedMessages, [.webViewDecidePolicyWithResponsePolicy])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDecidePolicyWithResponsePolicy])
Expand Down
Loading