Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Open
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
2 changes: 1 addition & 1 deletion MagicSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
Pod::Spec.new do |s|
s.name = 'MagicSDK'
s.version = '10.1.1'
s.version = '10.2.1'
s.summary = 'Magic IOS SDK'

s.description = <<-DESC
Expand Down
9 changes: 7 additions & 2 deletions Sources/MagicSDK/Core/Magic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,26 @@ public class Magic: NSObject {
/// - apiKey: Your client ID. From https://dashboard.Magic.com
/// - ethNetwork: Etherum Network setting (ie. mainnet or goerli)
/// - customNode: A custom RPC node
/// - customViewWrapper: An custom `UIViewController` to act as the wrapping container of Magic's WebView
public convenience init(apiKey: String, ethNetwork: EthNetwork, locale: String = Locale.current.identifier) {
self.init(urlBuilder: URLBuilder(apiKey: apiKey, network: ethNetwork, locale: locale))
}

public convenience init(apiKey: String, customNode: CustomNodeConfiguration, locale: String = Locale.current.identifier) {
self.init(urlBuilder: URLBuilder(apiKey: apiKey, customNode: customNode, locale: locale))
}

public convenience init(apiKey: String, customViewWrapper: UIViewController, locale: String = Locale.current.identifier) {
self.init(urlBuilder: URLBuilder(apiKey: apiKey, locale: locale), customViewWrapper: customViewWrapper)
}

public convenience init(apiKey: String, locale: String = Locale.current.identifier) {
self.init(urlBuilder: URLBuilder(apiKey: apiKey, network: EthNetwork.mainnet, locale: locale))
}

/// Core constructor
private init(urlBuilder: URLBuilder) {
self.rpcProvider = RpcProvider(urlBuilder: urlBuilder)
private init(urlBuilder: URLBuilder, customViewWrapper: UIViewController? = nil) {
self.rpcProvider = RpcProvider(urlBuilder: urlBuilder, customViewWrapper: customViewWrapper)

self.user = UserModule(rpcProvider: self.rpcProvider)
self.auth = AuthModule(rpcProvider: self.rpcProvider)
Expand Down
6 changes: 3 additions & 3 deletions Sources/MagicSDK/Core/Provider/RpcProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class RpcProvider: NetworkClient, Web3Provider {
let overlay: WebViewController
public let urlBuilder: URLBuilder

required init(urlBuilder: URLBuilder) {
self.overlay = WebViewController(url: urlBuilder)
required init(urlBuilder: URLBuilder, customViewWrapper: UIViewController? = nil) {
self.overlay = WebViewController(url: urlBuilder, customViewWrapper: customViewWrapper)
self.urlBuilder = urlBuilder
super.init()
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public class RpcProvider: NetworkClient, Web3Provider {
}.catch { error in
let errResponse = Web3Response<Result>(error: ProviderError.encodingFailed(error))
response(errResponse)
// handleRollbarError(error, log: false)

}
}
}
Expand Down
31 changes: 30 additions & 1 deletion Sources/MagicSDK/Core/Relayer/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WebViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler,
case messageEncodeFailed(message: String)

case webviewAttachedFailed
case customViewNotFound
case topMostWindowNotFound
}

Expand All @@ -43,12 +44,16 @@ class WebViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler,
/// Queue and callbackss
var queue: [String] = []
var messageHandlers: Dictionary<Int, MessageHandler> = [:]

var customViewWrapper: UIViewController?

typealias MessageHandler = (String) throws -> Void

// MARK: - init
init(url: URLBuilder) {
init(url: URLBuilder, customViewWrapper: UIViewController? = nil) {
self.urlBuilder = url
self.customViewWrapper = customViewWrapper

super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -241,6 +246,21 @@ class WebViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler,
override func viewDidLoad() {
super.viewDidLoad()
webView.scrollView.delegate = self // disable zoom

if let customViewWrapper = customViewWrapper {
// Add webView as a subview of the custom wrapper's view
customViewWrapper.view.addSubview(webView)
loadWebViewContent()
} else {
loadWebViewContent()
}
}

private func loadWebViewContent() {
if let url = URL(string: urlBuilder.url) {
let request = URLRequest(url: url)
webView.load(request)
}
}

/// Check did finished navigating, conforming WKNavigationDelegate
Expand Down Expand Up @@ -345,6 +365,15 @@ class WebViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler,
throw AuthRelayerError.webviewAttachedFailed
}
}

func detachWebViewFromCustomView() throws {
if customViewWrapper != nil {
webView.removeFromSuperview()
} else {
print("Please make sure you have provided Magic with a custom UIViewController to use this method.")
throw AuthRelayerError.customViewNotFound
}
}
}


9 changes: 9 additions & 0 deletions Sources/MagicSDK/Modules/Auth/AuthModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public class AuthModule: BaseModule {
}
}

// MARK: - External Dismiss WebView
public func externalDismissLoginView() {
do {
try self.provider.overlay.detachWebViewFromCustomView()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this call recycle webview?

Copy link
Contributor Author

@Ariflo Ariflo Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the point, the developer wants to remove the WebView from the view hierarchy. See his comments here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered potentially naming this method something like authOnlyExternalDismissLoginView 🤔 Perhaps add some more comments to inform web3 devs that they should not use this method.

} catch let error {
debugPrint("Failed to dismiss login view due to \(error.localizedDescription)")
}
}

public enum LoginEmailOTPLinkEvent: String {
case emailNotDeliverable = "email-not-deliverable"
case emailSent = "email-sent"
Expand Down