The Virtusize Auth SDK for iOS is a closed-source library that handles the SNS authentication process for our Virtusize iOS Integration.
- iOS 13.0+
- Xcode 12+
- Swift 5.0+
Install using the CocoaPods dependency manager. You can install it with the following command:
$ gem install cocoapodsTo integrate Virtusize SDK into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!
target '<your-target-name>' do
pod 'VirtusizeAuth', '~> 1.1.5'
endThen, run the following command:
$ pod repo update
$ pod install- In Xcode, select File > Swift Packages > Add Package Dependency... and enter
https://github.com/virtusize/virtusize_auth_ios.gitas the repository URL. - Select a minimum version of
1.1.5 - Click Next
The SNS authentication flow requires switching to a SFSafariViewController, which will load a web page for the user to login with their SNS account. In order to return the login response from a SFSafariViewController to your app, a custom URL scheme must be defined.
You must register a URL type and send it to the VirtusizeAuth.setAppBundleId method.
(1) Register a URL type
In Xcode, click on your project's Info tab and select URL Types.
Add a new URL type and set the URL Schemes and identifier to com.your-company.your-app.virtusize
(2) Set the app's bundle ID
In the App Delegate's application(_:didFinishLaunchingWithOptions:) method, call the VirtusizeAuth.setAppBundleId method with the app's bundle ID.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Virtusize initialization omitted for brevity
// Set the app bundle ID
VirtusizeAuth.setAppBundleId("com.your-company.your-app")
return true
}
❗IMPORTANT
- The URL type must include your app's bundle ID and end with .virtusize.
- If you have multiple app targets, add the URL type for all of them.
Use either of the following methods to enable Virtusize SNS login
To enable Virtusize SNS login on the web version of Virtusize integration inside your web view, please use this method:
-
If you have built your UI purely with UIKit, replace your
WKWebViewwithVirtusizeWebViewin your Swift file. If you use the WKWebViewConfiguration object to configure your web view, please access it from the closure like the example below.- Swift
- var webView: WKWebView + var webView: VirtusizeWebView
webView = VirtusizeWebView(frame: .zero) { configuration in // access the WKWebViewConfiguration object here to customize it // If you want to allow cookie sharing between multiple VirtusizeWebViews, // assign the same WKProcessPool object to configuration.processPool configuration.processPool = WKProcessPool() }
-
If you have built your UI with Xcode's Interface Builder, make sure that you set the Custom Class of your web view to
VirtusizeWebViewin the Identity inspector.- Swift
- @IBOutlet weak var webview: WKWebView! + @IBOutlet weak var webview: VirtusizeWebView!
yourWebView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = trueStep 2: Make sure your view controller confirms the WKNavigationDelegate and implement the code below to enable SNS buttons in Virtusize
class YourViewController: UIViewController {
private var yourWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// ... the other code
yourWebView.navigationDelegate = self
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("window.virtusizeSNSEnabled = true;")
}
}class YourViewController: UIViewController {
private var yourWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// ... the other code
yourWebView.uiDelegate = self
}
}
extension YourViewController: WKUIDelegate {
func webView(
_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures
) -> WKWebView? {
guard let url = navigationAction.request.url else {
return nil
}
if VirtusizeURLCheck.isExternalLinkFromVirtusize(url: url.absoluteString) {
UIApplication.shared.open(url, options: [:])
return nil
}
if VirtusizeAuthorization.isSNSAuthURL(viewController: self, webView: webView, url: url) {
return nil
}
if navigationAction.targetFrame == nil && VirtusizeURLCheck.isLinkFromSNSAuth(url: url.absoluteString) {
// By default, the Google sign-in page shows a 403 error: disallowed_useragent if you are visiting it within a web view.
// By setting up the user agent, Google recognizes the web view as a Safari browser
configuration.applicationNameForUserAgent = "CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1"
let popupWebView = WKWebView(frame: webView.frame, configuration: configuration)
popupWebView.uiDelegate = self
webView.addSubview(popupWebView)
popupWebView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
popupWebView.topAnchor.constraint(equalTo: webView.topAnchor),
popupWebView.leadingAnchor.constraint(equalTo: webView.leadingAnchor),
popupWebView.trailingAnchor.constraint(equalTo: webView.trailingAnchor),
popupWebView.bottomAnchor.constraint(equalTo: webView.bottomAnchor)
])
return popupWebView
}
// The rest of your code ...
return nil
}
func webViewDidClose(_ webView: WKWebView) {
webView.removeFromSuperview()
}
}
