Skip to main content

Initialization

To initialize the SDK, you create an instance of LuneSDKManager (or LuneSDKObjcManager for Objective-C) that can be used across your app.

Since you'll need to initialize LuneSDK with credentials, it's a good practice to handle the preparation within a view-model. This can include:

  • Getting the credentials
  • Setting up a refresh callback (optional, based on your credentials' TTL)
  • Setting up logging (optional)

You can find specific implementation details for your project setup below:


SwiftUI

Follow the steps below to initialize LuneSDK in your SwiftUI Project.

MyViewModel.Swift
import Foundation
import LuneSDK // 1. Import LuneSDK

class MyViewModel: ObservableObject {
// 2. Add a property to hold the LuneSDKManager instance
@Published var luneSDKManager: LuneSDKManager? = nil

init() {
Task {
// 3. do everything you need to do to get your credentials
await getCredentials()

// 4. initialize the SDK with your credentials
let sdk = LuneSDKManager(
baseUrl: "<your.base.url>",
token: "<your.token>",
customerId: "<user.customer.id>"
)

// 5. optional: set up a refresh callback to handle token refresh
sdk.setupRefreshCallback(getRefreshToken)

// 6. optional: set up an event logger if you need to be informed about user actions in the SDK (for analytics)
sdk.initializeLogger { eventMap in
print("Logging event: \(eventMap)");
}

// 7. assign the SDK to the published property
DispatchQueue.main.async { [self] in
self.luneSDKManager = sdk
}
}
}

private func getCredentials() async {
// do everything you need to do to get your credentials (from server, or env, etc)
}

func getRefreshToken() async -> String? {
// do everything you need to do to get a refresh token
return "<refresh_token>"
}
}

Below are the steps within your view-model:

  1. Import LuneSDK.
  2. Instantiate a published property to hold the SDK instance. This will be referenced from your view. We'll call it luneSDKManager.
  3. Get LuneSDK credentials (baseUrl and token) ready. This could involve making requests to your backend or reading from environment variables.
  4. Initialize the SDK with the credentials and customerId.
  5. Optional: Set up a refresh callback function. It should be an async function with a return type of String?. This function will be called whenever your token expires.
  6. Optional: Set up a logging function for analytics events. This function should take in a map of String to Any and will be used to report user actions.
  7. Assign the initialized SDK to the published property, luneSDKManager.
SwiftUI Tip

While you could create multiple instances of LuneSDKManager, we recommend creating just one instance per app. You can share that single instance with other views using Environment Objects.

MyApp.Swift
import SwiftUI
import LuneSDK // 1. Import LuneSDK

@main
struct MyApp: App {
@ObservedObject private var viewModel = MyViewModel()

// 2. create getter
var luneSDKManager: LuneSDKManager? {
viewModel.luneSDKManager
}

var body: some Scene {
WindowGroup {
if(luneSDKManager != nil) {
ContentView()
.environmentObject(luneSDKManager!) // 3. optional: share instance with other views
}
}
}
}

Below are the steps within your view:

  1. Import LuneSDK.
  2. Create a getter for the published property in your view-model, luneSDKManager.
  3. You can now use luneSDKManager directly or pass it down your view hierarchy to share the instance.

You can then use the LuneSDKManager instance in any view of your app as shown below.

HomeView.Swift
import SwiftUI
import LuneSDK // 1.

struct HomeView: View {
// 2. Get instance with @EnvironmentObject
@EnvironmentObject var luneSDK: LuneSDKManager

var body: some View {
ScrollView{
// 3. Use instance to inject any view of your choice
luneSDK.TransactionListComponent()
}
}
}

Objective C

Update Needed

The Objective-C implementation needs to be updated in light of recent breaking changes. This documentation will be updated right after updates have been tested and pushed.

Follow the steps below to initialize LuneSDK in your Obj-C Project.

YourViewController.m
@import LuneSDK;

@interface YourViewController ()
// Declare luneSDK as a property of the class
@property (nonatomic, strong) LuneSDKObjcManager *luneSDK;
@end

@implementation YourViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize the SDK with your credentials.
_luneSDK = [
[LuneSDKObjcManager alloc]
initWithBaseUrl:@"<your.base.url>"
token:@"<your.token>",
customerId:@"<user.customer.id>"
];
}

You can then use the LuneSDKObjcManager instance in any view of your app as shown below.

YourViewController.m
// budget summary setup, after the above setup is complete

// Create a new view controller instance using the BudgetSummaryComponent method of the LuneSDK.
UIViewController *hostingController = [self.luneSDK BudgetSummaryComponent];

// Add the new view controller as a child view controller.
[self addChildViewController:hostingController];

// Add the new view controller's view as a subview.
[self.view addSubview:hostingController.view];

// Disable the autoresizing mask translation to enable Auto Layout constraints.
hostingController.view.translatesAutoresizingMaskIntoConstraints = NO;

// Activate Auto Layout constraints to pin the new view to the edges.
[NSLayoutConstraint activateConstraints:@[
[hostingController.view.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[hostingController.view.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[hostingController.view.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
[hostingController.view.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
]];