Automatically switch back to previous app (if iOS has detected one) after A) WalletConnect session approval B) WalletConnect session rejection C) WalletConnect signing

pull/4992/head
Hwee-Boon Yar 2 years ago
parent 4d8e301ece
commit ffce20f3ed
  1. 2
      AlphaWallet.xcodeproj/project.pbxproj
  2. 35
      AlphaWallet/WalletConnect/Coordinator/WalletConnectCoordinator.swift
  3. 1
      Podfile
  4. 7
      Podfile.lock
  5. 27
      modules/AlphaWalletGoBack/AlphaWalletGoBack.podspec
  6. 16
      modules/AlphaWalletGoBack/AlphaWalletGoBack/AlphaWalletGoBack.h
  7. 7
      modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.h
  8. 24
      modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.m
  9. 21
      modules/AlphaWalletGoBack/LICENSE

@ -6440,6 +6440,7 @@
"${BUILT_PRODUCTS_DIR}/AlphaWalletAddress/AlphaWalletAddress.framework",
"${BUILT_PRODUCTS_DIR}/AlphaWalletCore/AlphaWalletCore.framework",
"${BUILT_PRODUCTS_DIR}/AlphaWalletENS/AlphaWalletENS.framework",
"${BUILT_PRODUCTS_DIR}/AlphaWalletGoBack/AlphaWalletGoBack.framework",
"${BUILT_PRODUCTS_DIR}/AlphaWalletOpenSea/AlphaWalletOpenSea.framework",
"${BUILT_PRODUCTS_DIR}/Apollo/Apollo.framework",
"${BUILT_PRODUCTS_DIR}/BigInt/BigInt.framework",
@ -6491,6 +6492,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/AlphaWalletAddress.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/AlphaWalletCore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/AlphaWalletENS.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/AlphaWalletGoBack.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/AlphaWalletOpenSea.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Apollo.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/BigInt.framework",

@ -6,6 +6,7 @@
//
import UIKit
import AlphaWalletGoBack
import WalletConnectSwift
import PromiseKit
import Result
@ -317,6 +318,8 @@ extension WalletConnectCoordinator: WalletConnectServerDelegate {
}
}.done { response in
try? server.respond(response, request: request)
}.ensure {
JumpBackToPreviousApp.goBack(forWalletConnectAction: action)
}.catch { error in
if error is WalletConnectCoordinator.RequestCanceledDueToWatchWalletError {
self.navigationController.displayError(error: error)
@ -461,10 +464,11 @@ extension WalletConnectCoordinator: WalletConnectServerDelegate {
}.done { choise in
guard case .walletConnect(let server) = choise else {
completion(.cancel)
JumpBackToPreviousApp.goBackForWalletConnectSessionCancelled()
return
}
completion(.connect(server))
JumpBackToPreviousApp.goBackForWalletConnectSessionApproved()
}.catch { _ in
completion(.cancel)
}.finally {
@ -577,3 +581,32 @@ extension WalletConnectCoordinator: CanOpenURL {
delegate?.didPressOpenWebPage(url, in: viewController)
}
}
fileprivate class JumpBackToPreviousApp {
static func goBack(forWalletConnectAction action: AlphaWallet.WalletConnect.Action) {
if action.type.shouldGoBackToPreviousAppAfterAction {
_ = UIApplication.shared.goBackToPreviousAppIfAvailable()
} else {
//no-op
}
}
static func goBackForWalletConnectSessionApproved() {
_ = UIApplication.shared.goBackToPreviousAppIfAvailable()
}
static func goBackForWalletConnectSessionCancelled() {
_ = UIApplication.shared.goBackToPreviousAppIfAvailable()
}
}
fileprivate extension AlphaWallet.WalletConnect.Action.ActionType {
var shouldGoBackToPreviousAppAfterAction: Bool {
switch self {
case .signMessage, .signPersonalMessage, .signTypedMessageV3, .signTransaction, .sendTransaction, .typedMessage, .sendRawTransaction, .walletSwitchEthereumChain, .walletAddEthereumChain:
return true
case .getTransactionCount, .unknown:
return false
}
}
}

@ -37,6 +37,7 @@ target 'AlphaWallet' do
pod 'CocoaLumberjack', '3.7.0'
pod 'AlphaWalletAddress', :path => 'modules/AlphaWalletAddress'
pod 'AlphaWalletCore', :path => 'modules/AlphaWalletCore'
pod 'AlphaWalletGoBack', :path => 'modules/AlphaWalletGoBack'
pod 'AlphaWalletENS', :path => 'modules/AlphaWalletENS'
pod 'AlphaWalletOpenSea', :path => 'modules/AlphaWalletOpenSea'
pod 'Apollo'

@ -14,6 +14,7 @@ PODS:
- PromiseKit
- Result
- web3swift
- AlphaWalletGoBack (1.0.0)
- AlphaWalletOpenSea (1.0.0):
- AlphaWalletAddress
- AlphaWalletCore
@ -135,6 +136,7 @@ DEPENDENCIES:
- AlphaWalletAddress (from `modules/AlphaWalletAddress`)
- AlphaWalletCore (from `modules/AlphaWalletCore`)
- AlphaWalletENS (from `modules/AlphaWalletENS`)
- AlphaWalletGoBack (from `modules/AlphaWalletGoBack`)
- AlphaWalletOpenSea (from `modules/AlphaWalletOpenSea`)
- AlphaWalletWeb3Provider (from `https://github.com/AlphaWallet/AlphaWallet-web3-provider`, commit `9a4496d02b7ddb2f6307fd0510d8d7c9fcef9870`)
- APIKit (= 5.1.0)
@ -224,6 +226,8 @@ EXTERNAL SOURCES:
:path: modules/AlphaWalletCore
AlphaWalletENS:
:path: modules/AlphaWalletENS
AlphaWalletGoBack:
:path: modules/AlphaWalletGoBack
AlphaWalletOpenSea:
:path: modules/AlphaWalletOpenSea
AlphaWalletWeb3Provider:
@ -289,6 +293,7 @@ SPEC CHECKSUMS:
AlphaWalletAddress: 4e82b3f32e1f1867b41db305b659252e6fa4d79b
AlphaWalletCore: 3cb5917cc8c867a63f67d76d4376fb0fc9eebd5c
AlphaWalletENS: df3cb79a2def496f78b06a838d0a111528e473db
AlphaWalletGoBack: 935efdbd98fa80039f2a350cde5b3a50cea46564
AlphaWalletOpenSea: 246846069baa0c5b0a578a9b48aee387652f376c
AlphaWalletWeb3Provider: 7ca1e1c1dc841dc1915f970daace48bf34931655
APIKit: 9e1a4069608bf0ae5238811e6cfc26928ad4d01e
@ -338,6 +343,6 @@ SPEC CHECKSUMS:
web3swift: 06118d4c4edc801444aaa995bbbddeda176b97ef
xcbeautify: b2c6b50c9cab6414296898e94cd153e4ea879662
PODFILE CHECKSUM: d70edc444b96b2e66265320d56d42b773c7c4055
PODFILE CHECKSUM: b5b4a34d65d6cecd431f895cb18324b3b99e4f27
COCOAPODS: 1.11.2

@ -0,0 +1,27 @@
#
# Be sure to run `pod lib lint AlphaWalletGoBack.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'AlphaWalletGoBack'
s.version = '1.0.0'
s.summary = 'Alpha Wallet GoBack library'
s.description = <<-DESC
Lightweight library representing the AlphaWalletGoBack with its functionality
DESC
s.homepage = "https://github.com/AlphaWallet/alpha-wallet-ios/tree/master/modules/AlphaWalletGoBack"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "Hwee-Boon Yar" => "hboon@motionobj.com" }
s.ios.deployment_target = '12.0'
s.swift_version = '4.0'
s.platform = :ios, "12.0"
s.source = { :git => 'git@github.com:AlphaWallet/alpha-wallet-ios.git', :tag => "#{s.version}" }
s.source_files = 'AlphaWalletGoBack/**/*.{h,m}'
s.pod_target_xcconfig = { 'SWIFT_OPTIMIZATION_LEVEL' => '-Owholemodule' }
s.frameworks = 'UIKit'
end

@ -0,0 +1,16 @@
//
// AlphaWalletGoBack.h
// AlphaWalletGoBack
//
#import <Foundation/Foundation.h>
//! Project version number for AlphaWalletGoBack.
FOUNDATION_EXPORT double AlphaWalletGoBackVersionNumber;
//! Project version string for AlphaWalletGoBack.
FOUNDATION_EXPORT const unsigned char AlphaWalletGoBackVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <AlphaWalletGoBack/PublicHeader.h>
#import <AlphaWalletGoBack/GoBack.h>

@ -0,0 +1,7 @@
#import <UIKit/UIKit.h>
@interface UIApplication(GoBack)
- (BOOL)goBackToPreviousAppIfAvailable;
@end

@ -0,0 +1,24 @@
@import UIKit;
@import ObjectiveC.runtime;
@interface UISystemNavigationAction : NSObject
@property(nonatomic, readonly, nonnull) NSArray<NSNumber*>* destinations;
-(BOOL)sendResponseForDestination:(NSUInteger)destination;
@end
@implementation UIApplication(GoBack)
//Derived from https://stackoverflow.com/a/43102093
- (BOOL)goBackToPreviousAppIfAvailable{
Ivar sysNavIvar = class_getInstanceVariable(UIApplication.class, "_systemNavigationAction");
UIApplication* app = UIApplication.sharedApplication;
UISystemNavigationAction* action = object_getIvar(app, sysNavIvar);
if (action) {
NSUInteger destination = action.destinations.firstObject.unsignedIntegerValue;
return [action sendResponseForDestination:destination];
} else {
return NO;
}
}
@end

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 AlphaWallet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading…
Cancel
Save