Introduction
Welcome to the documentation for RealmLoginKit!
Overview
RealmLoginKit is a powerful framework that provides seamless integration of authentication and user management functionalities with Realm Database in your iOS app. With RealmLoginKit, you can easily implement features like user registration, login, logout, and password recovery.
Requirements
- iOS 10.0 or later
- Xcode 10 or later
- Swift 5
- Realm Database
Installation
To install RealmLoginKit, follow these steps:
- Open your project in Xcode.
- Go to your project’s root directory.
- Create a new file named
Podfile
. - Add the following lines to your
Podfile
:
source ‘https://cdn.cocoapods.org/’
platform :ios, ‘10.0’
use_frameworks!
pod ‘RealmLoginKit’
- Save the
Podfile
. - Open Terminal and navigate to your project’s root directory.
- Run the following command:
pod install
Wait for the installation process to finish. Once it’s completed, you can close Terminal and open your project using the .xcworkspace
file.
Usage
RealmLoginKit is designed to be straightforward and easy to use. To integrate it into your app, follow the steps below:
Step 1: Import RealmLoginKit
In the file where you want to use RealmLoginKit, add the following import statement:
import RealmLoginKit
Step 2: Configure RealmLoginKit
Before using RealmLoginKit, you need to configure it with your Realm App ID. In your app delegate’s didFinishLaunchingWithOptions
method, add the following code:
// Replace <YOUR_REALM_APP_ID> with your actual Realm App ID
RealmLoginKit.configure(with: "<YOUR_REALM_APP_ID>")
Step 3: Implement Authentication
RealmLoginKit provides convenient methods for authentication, such as registerUser
, login
, logout
, and recoverPassword
. Here’s an example of how to use these methods:
// Register a new user
RealmLoginKit.shared.registerUser(withEmail: "user@example.com", password: "mypassword") { success, error in
if let error = error {
print("Registration failed with error: \(error.localizedDescription)")
} else {
print("Registration successful")
}
}
// Log in an existing user
RealmLoginKit.shared.login(withEmail: "user@example.com", password: "mypassword") { success, error in
if let error = error {
print("Login failed with error: \(error.localizedDescription)")
} else {
print("Login successful")
}
}
// Log out the current user
RealmLoginKit.shared.logout { success, error in
if let error = error {
print("Logout failed with error: \(error.localizedDescription)")
} else {
print("Logout successful")
}
}
// Recover password for a user
RealmLoginKit.shared.recoverPassword(forEmail: "user@example.com") { success, error in
if let error = error {
print("Password recovery failed with error: \(error.localizedDescription)")
} else {
print("Password recovery request successful")
}
}
Step 4: Persistence and User Access
RealmLoginKit allows you to access the current logged-in user at any time using the currentUser
property. You can also check if a user is currently logged in with the isLoggedIn
property. Here’s an example:
// Access the currently logged-in user
if let currentUser = RealmLoginKit.shared.currentUser {
print("Current user: \(currentUser.email)")
} else {
print("No user logged in")
}
// Check if a user is logged in
if RealmLoginKit.shared.isLoggedIn {
print("User is logged in")
} else {
print("No user logged in")
}
Step 5: Protecting Realm Data
To protect your Realm data, you can use the setAccessLevel
method of RLMSyncUser
to define different access levels for each user. You can specify access levels such as .noAccess
, .read
, or .write
. Here’s an example of how to protect a specific Realm:
// Replace <YOUR_REALM_NAME> with the name of your Realm
let realm = try! Realm(configuration: Realm.Configuration(syncConfiguration: SyncConfiguration.user(<YOUR_REALM_NAME>)))
// Set access level for the current user
realm.syncSession?.currentUser?.setAccessLevel(.write, for: realm.configuration.syncConfiguration)
Conclusion
Congratulations! You have successfully integrated RealmLoginKit into your iOS app. With RealmLoginKit, you can easily implement user authentication and management functionalities using Realm Database. Feel free to explore more features and options provided by RealmLoginKit to enhance the user experience of your app.