Skip to content

Commit 9fb37fd

Browse files
authored
Merge pull request #64 from auth0/prepare-user-notifications-fwk
Deprecate iOS 9 or older notification code
2 parents 57b1a97 + bc768f5 commit 9fb37fd

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

Guardian.xcodeproj/project.pbxproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@
876876
"$(PROJECT_DIR)/Carthage/Build/iOS",
877877
);
878878
INFOPLIST_FILE = GuardianApp/Info.plist;
879-
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
879+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
880880
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
881881
PRODUCT_BUNDLE_IDENTIFIER = com.auth0.guardian.sample;
882882
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -885,6 +885,7 @@
885885
SWIFT_INSTALL_OBJC_HEADER = NO;
886886
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
887887
SWIFT_VERSION = 4.0;
888+
TARGETED_DEVICE_FAMILY = 1;
888889
};
889890
name = Debug;
890891
};
@@ -902,14 +903,15 @@
902903
"$(PROJECT_DIR)/Carthage/Build/iOS",
903904
);
904905
INFOPLIST_FILE = GuardianApp/Info.plist;
905-
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
906+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
906907
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
907908
PRODUCT_BUNDLE_IDENTIFIER = com.auth0.guardian.sample;
908909
PRODUCT_NAME = "$(TARGET_NAME)";
909910
PROVISIONING_PROFILE = "32df3887-3767-4b67-84a6-144aaf2ef2f2";
910911
PROVISIONING_PROFILE_SPECIFIER = "match Development com.auth0.guardian.sample";
911912
SWIFT_INSTALL_OBJC_HEADER = NO;
912913
SWIFT_VERSION = 4.0;
914+
TARGETED_DEVICE_FAMILY = 1;
913915
};
914916
name = Release;
915917
};

Guardian/Guardian.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import Foundation
2424

25+
/// Default URLSession used to send requests to Guardian API.
2526
public let defaultURLSession: URLSession = {
2627
let config = URLSessionConfiguration.default
2728
config.requestCachePolicy = .reloadIgnoringLocalCacheData
@@ -380,7 +381,9 @@ public func notification(from userInfo: [AnyHashable: Any]) -> Notification? {
380381

381382
- parameter withAcceptTitle: the title for the "Accept" notification action
382383
- parameter rejectTitle: the title for the "Reject" notification action
384+
- important: Deprecated for iOS 10 or later, please use UserNotification framework
383385
*/
386+
@available(iOS, deprecated: 10.0, message: "Use UserNotification framework")
384387
public func categoryForNotification(withAcceptTitle acceptTitle: String, rejectTitle: String) -> UIUserNotificationCategory {
385388
let acceptAction = UIMutableUserNotificationAction()
386389
acceptAction.identifier = acceptActionIdentifier

Guardian/Notification.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import Foundation
2424

2525
public let AuthenticationCategory = "com.auth0.notification.authentication"
26-
let acceptActionIdentifier: String = "\(AuthenticationCategory).accept"
27-
let rejectActionIdentifier: String = "\(AuthenticationCategory).reject"
26+
public let acceptActionIdentifier: String = "\(AuthenticationCategory).accept"
27+
public let rejectActionIdentifier: String = "\(AuthenticationCategory).reject"
2828

2929
/**
3030
A Guardian Notification contains data about an authentication request.

GuardianApp/AppDelegate.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import UIKit
2424
import Guardian
25+
import UserNotifications
2526

2627
@UIApplicationMain
2728
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -36,12 +37,39 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3637
// Override point for customization after application launch.
3738

3839
// Set up push notifications
39-
let category = Guardian.categoryForNotification(withAcceptTitle: NSLocalizedString("Allow", comment: "Accept Guardian authentication request"),
40-
rejectTitle: NSLocalizedString("Deny", comment: "Reject Guardian authentication request"))
41-
let notificationTypes: UIUserNotificationType = [.badge, .sound]
42-
let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: [category])
43-
application.registerUserNotificationSettings(pushNotificationSettings)
44-
application.registerForRemoteNotifications()
40+
let acceptAction = UNNotificationAction(
41+
identifier: Guardian.acceptActionIdentifier,
42+
title: NSLocalizedString("Allow", comment: "Accept Guardian authentication request"),
43+
options: [.authenticationRequired]
44+
)
45+
let rejectAction = UNNotificationAction(
46+
identifier: Guardian.rejectActionIdentifier,
47+
title: NSLocalizedString("Deny", comment: "Reject Guardian authentication request"),
48+
options: [.destructive, .authenticationRequired]
49+
)
50+
51+
let category = UNNotificationCategory(
52+
identifier: Guardian.AuthenticationCategory,
53+
actions: [acceptAction, rejectAction],
54+
intentIdentifiers: [],
55+
options: [])
56+
57+
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound]) { granted, error in
58+
guard granted else {
59+
return print("Permission not granted")
60+
}
61+
if let error = error {
62+
return print("failed with error \(error)")
63+
}
64+
65+
UNUserNotificationCenter.current().setNotificationCategories([category])
66+
UNUserNotificationCenter.current().getNotificationSettings() { settings in
67+
guard settings.authorizationStatus == .authorized else {
68+
return print("not authorized to use notifications")
69+
}
70+
DispatchQueue.main.async { application.registerForRemoteNotifications() }
71+
}
72+
}
4573

4674
return true
4775
}

0 commit comments

Comments
 (0)