iOS Native
1. Register
Register your mobile application at the Zoho Accounts Developer Console to receive your Client ID and Client Secret. You must enter the client name, client domain, valid authorized redirect URI. Make sure you use the drop-down to change the client type to Mobile Applications.
Note: The authorized redirect URI is the URLScheme (Example: MyDemoApp://) of your application.
2. Set up Your Development Environment
Using Cocoapods
- Navigate to your project folder in a terminal window.
- Make sure you have the CocoaPods gem installed on your machine before installing the ZohoAuth pod.
$ sudo gem install cocoapods
$ pod init
- Add the following to your Podfile:
pod 'ZohoAuth'
- Run the following command in your project root directory from a terminal window:
$ pod install
3. ZohoAuth Integration
3a. Clear App Details on First Launch
This ensures that any stored authentication details are cleared when the app is launched for the first time.
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if !UserDefaults.standard.bool(forKey: "MY_APP_LAUNCHED") {
UserDefaults.standard.set(true, forKey: "MY_APP_LAUNCHED")
UserDefaults.standard.synchronize()
ZohoAuth.clearDetailsForFirstLaunch()
}
return true
}
3b. Initialize ZohoAuth SDK
Before using ZohoAuth, ensure you import the necessary framework in your AppDelegate file. If using Swift, you will need to add it to your Objective-C bridging header.
//Swift
import ZohoAuthKit
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
ZohoAuth(clientID: <Your Client ID>,
clientSecret: <Your Client Secret>,
scope: <Your Scopes Array>,
urlScheme: <Your URL Scheme: Example: "MyDemoApp://">,
mainWindow: UIApplication.shared.delegate?.window,
accountsURL:<Your Accounts URL: Example: "https://accounts.zoho.com">)
return true
}
3c. Handle Redirection
(i) In AppDelegate
This method ensures that the ZohoAuth SDK handles authentication responses properly. Note that application:openURL:options:
is available only on iOS 10 and later.
//Swift
func application(_application: UIApplication, openurl: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
let handled: Bool = ZohoAuth.handle(url,
sourceApplication: options[.sourceApplication],
annotation: options[.annotation])
//Add any custom logic here.
return handled
}
(ii) In SceneDelegate
For apps that use SceneDelegate, redirection handling should be done here instead.
//Swift
func scene(_ scene: UIScene, openURLContexts URLContexts:
Set<UIOpenURLContext>) {
ZohoAuth.handleURL(URLContexts.first?.url,
sourceApplication: URLContexts.first?.options.sourceApplication,
annotation: URLContexts.first?.options.annotation)
}
4. Add Zoho Login to Your Code
4a. Add Zoho Login
To add Zoho Login to your app, add the following code snippet to the login button action of your view controller.
//Swift
@IBAction func loginButtonAction(_ sender: Any) {
ZohoAuth.presentZohoSign(in: {(_ token: String?, _ error: Error?) -> Void in
if token != nil && error == nil {
// Handle sign in success logic here.
} else {
// Handle sign in failure logic here.
}
})
}
Now you should be able to run your app and log in using Zoho.
4b. Add Custom Zoho Login
To add Zoho Login to your app and customize Zoho login page, add the following code snippet to the login button action of your view controller.
//Swift
@IBAction func loginButtonAction(_ sender: Any) {
ZohoAuth.presentZohoSign(inHavingCustomParams: <Your Custom Params: Ex: "hide_fs=true">,
signinHanlder: {(_ token: String?, _ error: Error?) -> Void in
if token != nil && error == nil {
// Handle sign in success logic here.
} else {
// Handle sign in failure logic here.
}
})
}
List of supported Custom params:
Custom Param Name | Supported Values | Purpose |
---|
hide_signup | true/false | To hide/show Signup option |
portal_domain | <Your Org Registered Domain Name> | To show Org Logo in Sign in page |
hide_fs | true/false | To hide/show Federated Sign in option |
Note: Multiple custom params should be given in URL query params format. Example: @hide\_signup=false\&hide\_fs=true
5. OAuth Token
5a. Get OAuth Access Token:
You need an OAuth access token to access Zoho’s APIs. You can get an access token by calling the following method:
// Swift
func viewDidLoad() {
super.viewDidLoad()
ZohoAuth.getOauth2Token({(_ token: String?, _ error: Error?) -> Void in
if token != nil && error == nil {
//use this token to access Zoho's APIs here.
} else {
// Handle token failure logice here.
}
})
}
Note: This method will always return a valid access token.
5b. How to send the access token?
Send the access token only via the authorization header. Example: Zoho-oauthtoken<space><access_token>
//Swift
request.setValue("Zoho-oauthtoken \(token)", forHTTPHeaderField: "Authorization")
5c. Logout/Sign-out Handling:
Add the following code snippet to the logout/sign-out button action:
//Swift
ZohoAuth.revokeAccessToken({(_ error: Error?) -> Void in
if error == nil {
} else {
}
})
5d. Special Error Handling:
If you receive the invalid_mobile_code error, you must bring the user to the signed-out state and have the user sign-in again.
Note: You will get this error if the user deletes your app from Connected Apps.
6. Give Feedback
Please report bugs or issues to support-mobilesdk@zohoaccounts.com