Magic for iOS Apps
Magic for iOS Apps
New to Magic?
Create a fully-functional Magic auth demo in minutes.
Reference for the Magic SDK for iOS: https://github.com/magiclabs/magic-ios-pod
#Overview
The Magic SDK for iOS is your entry-point to secure, passwordless authentication for your mobile app. This guide will cover some important topics for getting started with iOS APIs and to make the most of Magic's features.
Magic can support either server-based or serverless web applications. It is up to the developers to implement the Admin SDK to validate the DID Token.
- View the API documentation below to learn the methods you'll be using
#Swift Package Manager Installation
1. In Xcode, select File > Add Packages... and enter `github.com/magiclabs/magic-ios.git` as the repository URL.
2. Select `Up to Next Major Version 3.0.0`
3. Don't forget to add the libraries to your Package Dependencies
4. When it's been added to your dependencies, you're all set!
#Cocoapods Installation
It's easy to install Magic SDK if you manage your iOS dependencies using CocoaPods. If you don't have an existing Podfile, run the following command to create one:
01$ pod init
Add pod 'MagicSDK'
to your Podfile
:
01target 'TARGET_NAME' do
02 use_frameworks!
03
04 pod 'MagicSDK'
05
06 # Required for XCFramework
07 post_install do |installer|
08 installer.pods_project.targets.each do |target|
09 target.build_configurations.each do |config|
10 config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
11 config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO'
12 end
13 end
14 end
15
16end
Run the following command:
01$ pod install
To update pods to the latest:
01$ pod update
#Create an SDK Instance
All the examples are written in Swift. Objective-C examples will be added soon.
01import UIKit
02import MagicSDK
03
04@UIApplicationMain
05class AppDelegate: UIResponder, UIApplicationDelegate {
06
07 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
08
09 // assign the newly created Magic instance to shared property
10 Magic.shared = Magic(apiKey: "API_KEY")
11
12 // do any other necessary launch configuration
13 }
14 return true
15}
#Returns via PromiseKit
Magic offers functions that returns promisified results by using PromiseKit. For more detail about promiseKit in iOS, please check PromiseKit.
So this...
01magic.auth.loginWithMagicLink(configuration, response: { response in
02 guard let result = response.result else { return print("Error:", response.error.debugDescription) }
03 print("Result", result)
04})
...is equivalent to the follow function call:
01magic.auth.loginWithMagicLink(configuration).done({ result in
02 print("Result", result) // DIDToken
03}).catch({
04 print("Error:", error) // handle Error
05})
Once the authentication request is complete, the closure will be executed with either success or failure state in the result. If you choose to return as a promise, the promise will resolve with success state or reject when failure state is reached.