Skip to content

Commit

Permalink
GH-2464 protecting from the crashes related to alert controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryBespalov committed Apr 22, 2024
1 parent 5bf8bd2 commit 604eb22
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@
"repositoryURL": "https://github.com/WalletConnect/WalletConnectSwiftV2",
"state": {
"branch": null,
"revision": "58d2b49eeac5cf94432e2647b9107577c156a25c",
"version": "1.9.9"
"revision": "c9ea7a1836e5a58636c3df528a21997187bd54ac",
"version": "1.18.4"
}
},
{
Expand Down
4 changes: 4 additions & 0 deletions Multisig/UI/ClaimToken/EnterCustomAddressViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class EnterCustomAddressViewController: UIViewController {
}

alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = addressField
}

present(alertVC, animated: true, completion: nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class EnterSafeAddressViewController: UIViewController {
self.show(udNameWrapperVC, sender: nil)
}))
}

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = addressField
}

alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ final class SwitchSafesViewController: UITableViewController {
alertController.addAction(addSafe)
alertController.addAction(createSafe)
alertController.addAction(cancel)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: indexPath)
}

self.present(alertController, animated: true)
} else {
let safe = chainSafes[indexPath.section - 1].safes[indexPath.row]
Expand Down Expand Up @@ -165,15 +171,15 @@ final class SwitchSafesViewController: UITableViewController {
var actions = [UIContextualAction]()

let deleteAction = UIContextualAction(style: .destructive, title: "Remove") { [weak self] _, _, completion in
self?.remove(safe: safe)
self?.remove(safe: safe, sourceIndexPath: indexPath)
completion(true)
}
actions.append(deleteAction)

return UISwipeActionsConfiguration(actions: actions)
}

private func remove(safe: Safe) {
private func remove(safe: Safe, sourceIndexPath: IndexPath) {
let title = safe.safeStatus == .deployed ?
"Removing a Safe only removes it from this app. It does not delete the Safe from the blockchain. Funds will not get lost." :
"Are you sure you want to remove this Safe? The transaction fees will not be returned."
Expand All @@ -188,6 +194,12 @@ final class SwitchSafesViewController: UITableViewController {
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(remove)
alertController.addAction(cancel)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: sourceIndexPath)
}

self.present(alertController, animated: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AddressBookListTableViewController: LoadableViewController, UITableViewDel
let alertController = UIAlertController(
title: nil,
message: nil,
preferredStyle: .actionSheet)
preferredStyle: .multiplatformActionSheet)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.barButtonItem = menuButton
Expand Down Expand Up @@ -188,7 +188,7 @@ class AddressBookListTableViewController: LoadableViewController, UITableViewDel
actions.append(editAction)

let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { [weak self] _, _, completion in
self?.remove(entry)
self?.remove(entry, sourceIndexPath: indexPath)
completion(true)
}
actions.append(deleteAction)
Expand Down Expand Up @@ -232,7 +232,7 @@ class AddressBookListTableViewController: LoadableViewController, UITableViewDel
show(ribbonVC, sender: nil)
}

private func remove(_ entry: AddressBookEntry) {
private func remove(_ entry: AddressBookEntry, sourceIndexPath: IndexPath) {
let alertController = UIAlertController(
title: nil,
message: "Removing the entry key only removes it from this app.",
Expand All @@ -245,6 +245,12 @@ class AddressBookListTableViewController: LoadableViewController, UITableViewDel
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(remove)
alertController.addAction(cancel)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: sourceIndexPath)
}

present(alertController, animated: true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class CreateAddressBookEntryViewController: UIViewController {
}))

alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = addressField
}

present(alertVC, animated: true, completion: nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class AddKeyAsOwnerIntroViewController: UIViewController, UIAdaptivePresentation
alertController.addAction(add)
alertController.addAction(replace)
alertController.addAction(cancel)

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = addButton
}

present(alertController, animated: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ extension SelectLedgerDeviceViewController: BluetoothControllerDelegate {
}
alertVC.addAction(cancel)
alertVC.addAction(settings)

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = tableView
}

present(alertVC, animated: true, completion: nil)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ class OwnerKeyDetailsViewController: UITableViewController, WebConnectionObserve
Tracker.trackEvent(.ownerKeyDetails)
}

@IBAction func removeButtonTouched(_ sender: UIButton) {
removeKey()
}

@objc private func didTapExportButton() {
let exportViewController = ExportViewController()

Expand Down Expand Up @@ -250,11 +246,16 @@ class OwnerKeyDetailsViewController: UITableViewController, WebConnectionObserve
completion?()
}

private func removeKey() {
private func removeKey(sourceIndexPath: IndexPath) {
let alertController = UIAlertController(
title: nil,
message: "Removing the owner key only removes it from this app. It doesn’t delete any Safes from this app or from blockchain. Transactions for Safes controlled by this key will no longer be available for signing in this app.",
preferredStyle: .multiplatformActionSheet)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: sourceIndexPath)
}

let remove = UIAlertAction(title: "Remove", style: .destructive) { _ in
OwnerKeyController.remove(keyInfo: self.keyInfo)
Expand Down Expand Up @@ -304,7 +305,7 @@ class OwnerKeyDetailsViewController: UITableViewController, WebConnectionObserve
indexPath: indexPath)
case Section.Advanced.remove:
return tableView.removeCell(indexPath: indexPath, title: "Remove owner key") { [weak self] in
self?.removeKey()
self?.removeKey(sourceIndexPath: indexPath)
}
default:
return UITableViewCell()
Expand Down Expand Up @@ -349,6 +350,12 @@ class OwnerKeyDetailsViewController: UITableViewController, WebConnectionObserve
case Section.Connected.connected:
if keyInfo.connectedAsDapp {
let alertController = DisconnectionConfirmationController.create(key: keyInfo)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: indexPath)
}

present(alertController, animated: true)
} else {
self.connect(keyInfo: keyInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class OwnerKeysListViewController: LoadableViewController, UITableViewDelegate,

if isConnected {
let alertController = DisconnectionConfirmationController.create(key: keyInfo)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: indexPath)
Expand All @@ -156,7 +157,7 @@ class OwnerKeysListViewController: LoadableViewController, UITableViewDelegate,
}

let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { [weak self] _, _, completion in
self?.remove(key: keyInfo)
self?.remove(key: keyInfo, sourceIndexPath: indexPath)
completion(true)
}
actions.append(deleteAction)
Expand All @@ -172,7 +173,7 @@ class OwnerKeysListViewController: LoadableViewController, UITableViewDelegate,
present(vc, animated: true)
}

private func remove(key: KeyInfo) {
private func remove(key: KeyInfo, sourceIndexPath: IndexPath) {
let alertController = UIAlertController(
title: nil,
message: "Removing the owner key only removes it from this app. It doesn’t delete any Safes from this app or from blockchain. Transactions for Safes controlled by this key will no longer be available for signing in this app.",
Expand All @@ -184,6 +185,12 @@ class OwnerKeysListViewController: LoadableViewController, UITableViewDelegate,
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(remove)
alertController.addAction(cancel)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: sourceIndexPath)
}

present(alertController, animated: true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class ReceiveAddOwnerLinkViewController: UIViewController {
alertController.addAction(add)
alertController.addAction(replace)
alertController.addAction(cancel)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = continueButton
}

present(alertController, animated: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class CreatePasscodeFlow: UIFlow {
}

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) { [unowned self] in

if let popoverPresentationController = biometryAlert.popoverPresentationController {
popoverPresentationController.sourceView = self.navigationController.view
}
self.navigationController.present(biometryAlert, animated: true)
}
}
Expand Down Expand Up @@ -168,6 +172,10 @@ class CreatePasscodeFlow: UIFlow {
let biometryAlert = factory.enableBiometryAlert { [unowned self] in
stop(success: true)
}

if let popoverPresentationController = biometryAlert.popoverPresentationController {
popoverPresentationController.sourceView = presenter.view
}

presenter.present(biometryAlert, animated: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ class EnterPasscodeViewController: PasscodeViewController {
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(remove)
alertController.addAction(cancel)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = button
}

present(alertController, animated: true)
}

Expand Down
23 changes: 15 additions & 8 deletions Multisig/UI/Settings/Passcode/SecuritySettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ class SecuritySettingsViewController: UITableViewController {
}
}

private func toggleBiometrics(_ lockMethod: LockMethod) {
private func toggleBiometrics(_ lockMethod: LockMethod, sourceIndexPath: IndexPath) {
if AppConfiguration.FeatureToggles.securityCenter {
toggleBiometricsSecurityLock(newLockMethod: lockMethod)
} else {
toggleBiometricsKeychainStorage()
toggleBiometricsKeychainStorage(sourceIndexPath: sourceIndexPath)
}
}

private func toggleBiometricsKeychainStorage() {
private func toggleBiometricsKeychainStorage(sourceIndexPath: IndexPath) {
withPasscodeAuthentication(for: "Login with biometrics") { [unowned self] success, nav, finish in
let completion = { [unowned self] in
finish()
Expand All @@ -200,7 +200,7 @@ class SecuritySettingsViewController: UITableViewController {
} else if success {
App.shared.auth.activateBiometrics { [unowned self] result in
if hasFailedBecauseBiometryNotEnabled(result) {
showBiometrySettings(presenter: nav!, completion: completion)
showBiometrySettings(presenter: nav!, sourceIndexPath: sourceIndexPath, completion: completion)
} else {
completion()
}
Expand Down Expand Up @@ -262,7 +262,9 @@ class SecuritySettingsViewController: UITableViewController {
return FAILED_FOR_OTHER_REASON
}

private func showBiometrySettings(presenter: UIViewController, completion: @escaping () -> Void) {
private func showBiometrySettings(presenter: UIViewController,
sourceIndexPath: IndexPath,
completion: @escaping () -> Void) {
let alertVC = UIAlertController(title: nil,
message: "To activate biometry, navigate to Settings.",
preferredStyle: .alert)
Expand All @@ -281,6 +283,11 @@ class SecuritySettingsViewController: UITableViewController {
}
alertVC.addAction(cancel)
alertVC.addAction(settings)

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: sourceIndexPath)
}

presenter.present(alertVC, animated: true, completion: nil)
}
Expand Down Expand Up @@ -354,22 +361,22 @@ class SecuritySettingsViewController: UITableViewController {
state: lock == .passcode ? .on : .off
) { [unowned self] action in
if lock != .passcode {
toggleBiometrics(.passcode)
toggleBiometrics(.passcode, sourceIndexPath: indexPath)
}
},
UIAction(
title: detailText(for: .lockMethod, lock: .userPresence, biometry: biometryType)!,
state: lock == .userPresence ? .on : .off
) { [unowned self] action in
if lock != .userPresence {
toggleBiometrics(.userPresence)
toggleBiometrics(.userPresence, sourceIndexPath: indexPath)
}
}]
if AppConfiguration.FeatureToggles.securityCenter {
children.append(UIAction(title: detailText(for: .lockMethod, lock: .passcodeAndUserPresence, biometry: biometryType)!,
state: lock == .passcodeAndUserPresence ? .on : .off
) { [unowned self] action in
toggleBiometrics(.passcodeAndUserPresence)
toggleBiometrics(.passcodeAndUserPresence, sourceIndexPath: indexPath)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ class SafeSettingsViewController: LoadableViewController, UITableViewDelegate, U
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(remove)
alertController.addAction(cancel)

if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = tableView
popoverPresentationController.sourceRect = tableView.rectForRow(at: indexPath)
}

self.present(alertController, animated: true)
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class TransactionViewController: UIViewController {
}

private func didTapAddressField() {
let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: .multiplatformActionSheet)

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = addressField
Expand Down Expand Up @@ -154,6 +154,10 @@ class TransactionViewController: UIViewController {
}))

alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

if let popoverPresentationController = alertVC.popoverPresentationController {
popoverPresentationController.sourceView = addressField
}

present(alertVC, animated: true, completion: nil)
}
Expand Down

0 comments on commit 604eb22

Please sign in to comment.