Skip to content

Commit

Permalink
First pass at updating examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mapierce committed Mar 31, 2022
1 parent 9a1ce54 commit 48f35a5
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 22 deletions.
12 changes: 10 additions & 2 deletions Examples/Sample-ObjC/Sample-ObjC/ITCAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

NSString *email = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"];
if (email.length > 0) { //There is a user logged in
[Intercom registerUserWithEmail:email];
ICMUserAttributes *attributes = [ICMUserAttributes new];
attributes.email = email;
[Intercom loginUserWithUserAttributes:attributes success:^{
NSLog(@"Successfully logged in %@", email);
} failure:^(NSError * _Nonnull error) {
NSLog(@"Error logging in: %@", error.localizedDescription);
}];
}

return YES;
Expand All @@ -47,7 +53,9 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[Intercom setDeviceToken:deviceToken];
[Intercom setDeviceToken:deviceToken failure:^(NSError * _Nullable error) {
NSLog(@"Error setting device token: %@", error.localizedDescription);
}];
}

@end
17 changes: 10 additions & 7 deletions Examples/Sample-ObjC/Sample-ObjC/ITCViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ - (void)handleUserLogin:(UIAlertController *)alertController {
NSString *email = emailField.text;
if (email.length > 0) {
//Start tracking the user with Intercom
[Intercom registerUserWithEmail:email];

//Save email so we know the user is logged in
[[NSUserDefaults standardUserDefaults] setObject:email forKey:@"email"];
[[NSUserDefaults standardUserDefaults] synchronize];

self.loggedIn = YES;
ICMUserAttributes *attributes = [ICMUserAttributes new];
attributes.email = email;
[Intercom loginUserWithUserAttributes:attributes success:^{
//Save email so we know the user is logged in
[[NSUserDefaults standardUserDefaults] setObject:email forKey:@"email"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.loggedIn = YES;
} failure:^(NSError * _Nonnull error) {
NSLog(@"Error logging in: %@", error.localizedDescription);
}];
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Intercom.setDeviceToken(deviceToken)
Intercom.setDeviceToken(deviceToken) { error in
guard let error = error else { return }
print("Error setting device token: \(error.localizedDescription)")
}
}

// MARK: UISceneSession Lifecycle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

let defaults = UserDefaults.standard
if let email = defaults.string(forKey: emailKey) {
Intercom.registerUser(withEmail: email)
let attributes = ICMUserAttributes()
attributes.email = email
Intercom.loginUser(with: attributes) { result in
switch result {
case .success: print("Successfully logged in \(email)")
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ class ViewController: UIViewController {
let textField = (alertController.textFields?.first)!
if !(textField.text?.isEmpty ?? false) {
//Start tracking the user with Intercom
Intercom.registerUser(withEmail: textField.text!)

UserDefaults.standard.set(textField.text, forKey: emailKey)
loggedIn = true
let attributes = ICMUserAttributes()
attributes.email = textField.text!
Intercom.loginUser(with: attributes) { [unowned self] result in
switch result {
case .success:
UserDefaults.standard.set(textField.text, forKey: emailKey)
loggedIn = true
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
}
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Examples/Sample-SwiftUI/iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Set device token for push notifications.
Intercom.setDeviceToken(deviceToken)
Intercom.setDeviceToken(deviceToken) { error in
guard let error = error else { return }
print("Error setting device token: \(error.localizedDescription)")
}
}

// MARK: UISceneSession Lifecycle
Expand Down
9 changes: 8 additions & 1 deletion Examples/Sample-SwiftUI/iOS/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

let defaults = UserDefaults.standard
if let email = defaults.string(forKey: emailKey) {
Intercom.registerUser(withEmail: email)
let attributes = ICMUserAttributes()
attributes.email = email
Intercom.loginUser(with: attributes) { result in
switch result {
case .success: print("Successfully logged in \(email)")
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
}
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions Examples/Sample-SwiftUI/iOS/Views/LoggedOutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ struct LoggedOutView: View {

func loginToIntercom() {
// Start tracking the user with Intercom
Intercom.registerUser(withEmail: email)

let defaults = UserDefaults.standard
defaults.set(email, forKey: emailKey)
loggedIn = true
let attributes = ICMUserAttributes()
attributes.email = email
Intercom.loginUser(with: attributes) { result in
switch result {
case .success:
let defaults = UserDefaults.standard
defaults.set(email, forKey: emailKey)
loggedIn = true
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
}
}
}
}

Expand Down

0 comments on commit 48f35a5

Please sign in to comment.