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] - Fix PassKit Multi-Threading #24493

Closed
wants to merge 1 commit into from
Closed
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 @@ -62,17 +62,23 @@ extension BrowserViewController: WKDownloadDelegate {
textEncodingName: downloadInfo.response.textEncodingName
)

if let passbookHelper = OpenPassBookHelper(
request: nil,
response: response,
canShowInWebView: false,
forceDownload: false,
browserViewController: self
) {
passbookHelper.open()
guard
let passbookHelper = OpenPassBookHelper(
request: nil,
response: response,
canShowInWebView: false,
forceDownload: false,
browserViewController: self
)
else {
Task.detached {
try FileManager.default.removeItem(at: downloadInfo.fileURL)
}
return
}

Task {
Task.detached {
await passbookHelper.open()
try FileManager.default.removeItem(at: downloadInfo.fileURL)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,9 @@ extension BrowserViewController: WKNavigationDelegate {
browserViewController: self
) {
// Open our helper and cancel this response from the webview.
passbookHelper.open()
Task.detached {
await passbookHelper.open()
}
return .cancel
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,39 @@ class OpenPassBookHelper: NSObject {
super.init()
}

func open() {
func open() async {
precondition(!Thread.isMainThread)

guard let passData = try? Data(contentsOf: url) else { return }
do {
let pass = try PKPass(data: passData)
let passLibrary = PKPassLibrary()
if passLibrary.containsPass(pass) {
UIApplication.shared.open(pass.passURL!, options: [:])
await MainActor.run {
UIApplication.shared.open(pass.passURL!, options: [:])
}
} else {
if let addController = PKAddPassesViewController(pass: pass) {
browserViewController.present(addController, animated: true, completion: nil)
await MainActor.run {
if let addController = PKAddPassesViewController(pass: pass) {
browserViewController.present(addController, animated: true, completion: nil)
}
}
}
} catch {
// display an error
let alertController = UIAlertController(
title: Strings.unableToAddPassErrorTitle,
message: Strings.unableToAddPassErrorMessage,
preferredStyle: .alert
)
alertController.addAction(
UIAlertAction(title: Strings.unableToAddPassErrorDismiss, style: .cancel) { (action) in
// Do nothing.
}
)
browserViewController.present(alertController, animated: true, completion: nil)
return
await MainActor.run {
// display an error
let alertController = UIAlertController(
title: Strings.unableToAddPassErrorTitle,
message: Strings.unableToAddPassErrorMessage,
preferredStyle: .alert
)
alertController.addAction(
UIAlertAction(title: Strings.unableToAddPassErrorDismiss, style: .cancel) { (action) in
// Do nothing.
}
)
browserViewController.present(alertController, animated: true, completion: nil)
}
}
}
}
Loading