Skip to content

Commit d844363

Browse files
authored
Merge pull request #403 from intercom/feature/update-examples
Update example projects
2 parents a23bfb5 + df3bdfb commit d844363

File tree

8 files changed

+65
-22
lines changed

8 files changed

+65
-22
lines changed

Examples/Sample-ObjC/Sample-ObjC/ITCAppDelegate.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2828

2929
NSString *email = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"];
3030
if (email.length > 0) { //There is a user logged in
31-
[Intercom registerUserWithEmail:email];
31+
ICMUserAttributes *attributes = [ICMUserAttributes new];
32+
attributes.email = email;
33+
[Intercom loginUserWithUserAttributes:attributes success:^{
34+
NSLog(@"Successfully logged in %@", email);
35+
} failure:^(NSError * _Nonnull error) {
36+
NSLog(@"Error logging in: %@", error.localizedDescription);
37+
}];
3238
}
3339

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

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

5361
@end

Examples/Sample-ObjC/Sample-ObjC/ITCViewController.m

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ - (void)handleUserLogin:(UIAlertController *)alertController {
8181
NSString *email = emailField.text;
8282
if (email.length > 0) {
8383
//Start tracking the user with Intercom
84-
[Intercom registerUserWithEmail:email];
85-
86-
//Save email so we know the user is logged in
87-
[[NSUserDefaults standardUserDefaults] setObject:email forKey:@"email"];
88-
[[NSUserDefaults standardUserDefaults] synchronize];
89-
90-
self.loggedIn = YES;
84+
ICMUserAttributes *attributes = [ICMUserAttributes new];
85+
attributes.email = email;
86+
[Intercom loginUserWithUserAttributes:attributes success:^{
87+
//Save email so we know the user is logged in
88+
[[NSUserDefaults standardUserDefaults] setObject:email forKey:@"email"];
89+
[[NSUserDefaults standardUserDefaults] synchronize];
90+
self.loggedIn = YES;
91+
} failure:^(NSError * _Nonnull error) {
92+
NSLog(@"Error logging in: %@", error.localizedDescription);
93+
}];
9194
}
9295

9396
}

Examples/Sample-Swift-UISceneDelegate/Sample-Swift-UISceneDelegate/AppDelegate.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1818
}
1919

2020
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
21-
Intercom.setDeviceToken(deviceToken)
21+
Intercom.setDeviceToken(deviceToken) { error in
22+
guard let error = error else { return }
23+
print("Error setting device token: \(error.localizedDescription)")
24+
}
2225
}
2326

2427
// MARK: UISceneSession Lifecycle

Examples/Sample-Swift-UISceneDelegate/Sample-Swift-UISceneDelegate/SceneDelegate.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
2828

2929
let defaults = UserDefaults.standard
3030
if let email = defaults.string(forKey: emailKey) {
31-
Intercom.registerUser(withEmail: email)
31+
let attributes = ICMUserAttributes()
32+
attributes.email = email
33+
Intercom.loginUser(with: attributes) { result in
34+
switch result {
35+
case .success: print("Successfully logged in \(email)")
36+
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
37+
}
38+
}
3239
}
3340
}
3441

Examples/Sample-Swift-UISceneDelegate/Sample-Swift-UISceneDelegate/ViewController.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,16 @@ class ViewController: UIViewController {
7474
let textField = (alertController.textFields?.first)!
7575
if !(textField.text?.isEmpty ?? false) {
7676
//Start tracking the user with Intercom
77-
Intercom.registerUser(withEmail: textField.text!)
78-
79-
UserDefaults.standard.set(textField.text, forKey: emailKey)
80-
loggedIn = true
77+
let attributes = ICMUserAttributes()
78+
attributes.email = textField.text!
79+
Intercom.loginUser(with: attributes) { [unowned self] result in
80+
switch result {
81+
case .success:
82+
UserDefaults.standard.set(textField.text, forKey: emailKey)
83+
loggedIn = true
84+
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
85+
}
86+
}
8187
}
8288
}
8389
}

Examples/Sample-SwiftUI/iOS/AppDelegate.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
1818

1919
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
2020
// Set device token for push notifications.
21-
Intercom.setDeviceToken(deviceToken)
21+
Intercom.setDeviceToken(deviceToken) { error in
22+
guard let error = error else { return }
23+
print("Error setting device token: \(error.localizedDescription)")
24+
}
2225
}
2326

2427
// MARK: UISceneSession Lifecycle

Examples/Sample-SwiftUI/iOS/SceneDelegate.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
2929

3030
let defaults = UserDefaults.standard
3131
if let email = defaults.string(forKey: emailKey) {
32-
Intercom.registerUser(withEmail: email)
32+
let attributes = ICMUserAttributes()
33+
attributes.email = email
34+
Intercom.loginUser(with: attributes) { result in
35+
switch result {
36+
case .success: print("Successfully logged in \(email)")
37+
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
38+
}
39+
}
3340
}
3441
}
3542

Examples/Sample-SwiftUI/iOS/Views/LoggedOutView.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ struct LoggedOutView: View {
2424

2525
func loginToIntercom() {
2626
// Start tracking the user with Intercom
27-
Intercom.registerUser(withEmail: email)
28-
29-
let defaults = UserDefaults.standard
30-
defaults.set(email, forKey: emailKey)
31-
loggedIn = true
27+
let attributes = ICMUserAttributes()
28+
attributes.email = email
29+
Intercom.loginUser(with: attributes) { result in
30+
switch result {
31+
case .success:
32+
let defaults = UserDefaults.standard
33+
defaults.set(email, forKey: emailKey)
34+
loggedIn = true
35+
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
36+
}
37+
}
3238
}
3339
}
3440

0 commit comments

Comments
 (0)