Swiftpack.co - Infomaniak/ios-login as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Infomaniak.
Infomaniak/ios-login 2.2.0
Library to simplify login process with Infomaniak OAuth 2.0 protocol
⭐️ 2
🕓 2 weeks ago
iOS
.package(url: "https://github.com/Infomaniak/ios-login.git", from: "2.2.0")

InfomaniakLogin

Library to simplify login process with Infomaniak OAuth 2.0 protocol

Installation

  1. In your Xcode project, go to: File > Swift Packages > Add Package Dependency…
  2. Enter the package URL: [email protected]:Infomaniak/ios-login.git or https://github.com/Infomaniak/ios-login.git

Usage

With SFSafariViewController

If your project has a SceneDelegate.swift file:

  1. Add import InfomaniakLogin at the top of the file
  2. Add this method:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url {
        // Handle URL
        InfomaniakLogin.handleRedirectUri(url: url)
    }
}

If your project doesn't have a SceneDelegate.swift file:

  1. Add import InfomaniakLogin at the top of your AppDelegate
  2. Initialise a UIWindow variable inside your AppDelegate:
var window: UIWindow?
  1. Add this method inside your AppDelegate:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return InfomaniakLogin.handleRedirectUri(url: url)
}

Final part:

You can now use it where you want by adding the InfomaniakLoginDelegate protocol to the class who needs it:

func didCompleteLoginWith(code: String, verifier: String) {
    InfomaniakLogin.getApiTokenUsing(code: code, codeVerifier: verifier) { (token, error) in 
        // Save the token
    }
}

func didFailLoginWith(error: Error) {
    // Handle the error
}

And you can finally use the login fonction, for example with a button, by writing:

@IBAction func login(_ sender: UIButton) {
    InfomaniakLogin.loginFrom(viewController: self, delegate: self, clientId: clientId, redirectUri: redirectUri)
}

With these arguments:

  • clientId: The client ID of the app
  • redirectUri: The redirection URL after a successful login (in order to handle the codes)

With WKWebView

First, add import InfomaniakLogin at the top of the file.

You can now use it where you want by adding the InfomaniakLoginDelegate protocol to the class who needs it:

func didCompleteLoginWith(code: String, verifier: String) {
    InfomaniakLogin.getApiTokenUsing(code: code, codeVerifier: verifier) { (token, error) in 
        // Save the token
    }
}

func didFailLoginWith(error: Error) {
    // Handle the error
}

And you can finally use the login fonction, for example with a button, by writing:

@IBAction func login(_ sender: UIButton) {
    InfomaniakLogin.webviewLoginFrom(viewController: self, delegate: self, clientId: clientId, redirectUri: redirectUri)
}

With these arguments:

  • clientId: The client ID of the app
  • redirectUri: The redirection URL after a successful login (in order to handle the codes)

But if you are using the Web View method, you can also use this method:

InfomaniakLogin.setupWebviewNavbar(title: nil, color: .red, clearCookie: true)

With these arguments:

  • title: The title that will be shown in the navigation bar
  • color: The color of the navigation bar
  • clearCookie:
    • If true, the cookie will be deleted when the Web View is closed
    • If false, the cookie won't be deleted when the Web View is closed

License

Copyright 2021 Infomaniak

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

GitHub

link
Stars: 2
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics