diff --git a/AlphaWallet.xcodeproj/project.pbxproj b/AlphaWallet.xcodeproj/project.pbxproj index 423fed576..3d52ac224 100644 --- a/AlphaWallet.xcodeproj/project.pbxproj +++ b/AlphaWallet.xcodeproj/project.pbxproj @@ -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", diff --git a/AlphaWallet/WalletConnect/Coordinator/WalletConnectCoordinator.swift b/AlphaWallet/WalletConnect/Coordinator/WalletConnectCoordinator.swift index cab722185..697396680 100644 --- a/AlphaWallet/WalletConnect/Coordinator/WalletConnectCoordinator.swift +++ b/AlphaWallet/WalletConnect/Coordinator/WalletConnectCoordinator.swift @@ -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 + } + } +} \ No newline at end of file diff --git a/Podfile b/Podfile index 3d6a34bc8..425b1cc98 100644 --- a/Podfile +++ b/Podfile @@ -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' diff --git a/Podfile.lock b/Podfile.lock index 87c5904d8..322de78c8 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -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 diff --git a/modules/AlphaWalletGoBack/AlphaWalletGoBack.podspec b/modules/AlphaWalletGoBack/AlphaWalletGoBack.podspec new file mode 100644 index 000000000..de60ff15e --- /dev/null +++ b/modules/AlphaWalletGoBack/AlphaWalletGoBack.podspec @@ -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 \ No newline at end of file diff --git a/modules/AlphaWalletGoBack/AlphaWalletGoBack/AlphaWalletGoBack.h b/modules/AlphaWalletGoBack/AlphaWalletGoBack/AlphaWalletGoBack.h new file mode 100644 index 000000000..e1ee4a710 --- /dev/null +++ b/modules/AlphaWalletGoBack/AlphaWalletGoBack/AlphaWalletGoBack.h @@ -0,0 +1,16 @@ +// +// AlphaWalletGoBack.h +// AlphaWalletGoBack +// + +#import + +//! 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 + +#import diff --git a/modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.h b/modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.h new file mode 100644 index 000000000..911156274 --- /dev/null +++ b/modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.h @@ -0,0 +1,7 @@ +#import + +@interface UIApplication(GoBack) + +- (BOOL)goBackToPreviousAppIfAvailable; + +@end \ No newline at end of file diff --git a/modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.m b/modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.m new file mode 100644 index 000000000..c859687cb --- /dev/null +++ b/modules/AlphaWalletGoBack/AlphaWalletGoBack/GoBack.m @@ -0,0 +1,24 @@ +@import UIKit; +@import ObjectiveC.runtime; + +@interface UISystemNavigationAction : NSObject + @property(nonatomic, readonly, nonnull) NSArray* 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 \ No newline at end of file diff --git a/modules/AlphaWalletGoBack/LICENSE b/modules/AlphaWalletGoBack/LICENSE new file mode 100644 index 000000000..9acfe3c30 --- /dev/null +++ b/modules/AlphaWalletGoBack/LICENSE @@ -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.