Ich versuche eine Benutzerbenachrichtigungsaktion von einem ViewController zu definieren. Aus meiner Sicht muss die Benachrichtigung mit Aktionen im App-Delegierten eingerichtet werden.swift, UIUserNotificationAction vom View-Controller anstelle des Anwendungsdelegaten
Heres meines App Delegierter So:
class AppDelegate: UIResponder, UIApplicationDelegate {
enum Actions:String{
case confirmunlock = "CONFIRMUNLOCK_ACTION"
}
var categoryID:String {
get{
return "CONFRIMUNLOCK_CATEGORY"
}
}
// Register notification settings
func registerNotification() {
// 1. Create the actions **************************************************
// confirmunlock Action
let confirmunlockAction = UIMutableUserNotificationAction()
confirmunlockAction.identifier = Actions.confirmunlock.rawValue
confirmunlockAction.title = "Öffnen"
confirmunlockAction.activationMode = UIUserNotificationActivationMode.Background
confirmunlockAction.authenticationRequired = false
confirmunlockAction.destructive = false
// 2. Create the category ***********************************************
// Category
let confirmunlockCategory = UIMutableUserNotificationCategory()
confirmunlockCategory.identifier = categoryID
// A. Set actions for the default context
confirmunlockCategory.setActions([confirmunlockAction],
forContext: UIUserNotificationActionContext.Default)
// B. Set actions for the minimal context
confirmunlockCategory.setActions([confirmunlockAction],
forContext: UIUserNotificationActionContext.Minimal)
// 3. Notification Registration *****************************************
let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: confirmunlockCategory) as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
// MARK: Application Delegate
func application(application: UIApplication,
handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification,
completionHandler:() -> Void) {
// Handle notification action *****************************************
if notification.category == categoryID {
let action:Actions = Actions(rawValue: identifier!)!
switch action{
case Actions.confirmunlock:
print("Well done!")
}
}
completionHandler()
}
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
registerNotification()
return true
}
Das funktioniert gut. Wenn ich "notification.category = categoryID" zu einer meiner LocalNotifications hinzufüge, wird "Well done" protokolliert. Nun sollte die Aktion, die ich aufrufen möchte, eigentlich vom View-Controller aufgerufen werden. Es hat viele Variablen und Sie müssen zuerst eingeloggt sein, um auf einen Server zuzugreifen, um die Aktion aufzurufen usw. Wie kann ich das tun? Wenn ich dieses func in meinem Ansichtsteuerpult einsetze, wird die Tätigkeit nicht angerufen:
func application(application: UIApplication,
handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification,
completionHandler:() -> Void) {
// Handle notification action *****************************************
if notification.category == categoryID {
let action:Actions = Actions(rawValue: identifier!)!
switch action{
case Actions.confirmunlock:
print("Well done!")
}
}
completionHandler()
}
Vielen Dank! Stephan
Hallo, habe ich versuchen. Leider funktioniert meine View-Controller-Funktion nicht vom App-Delegaten. Ich beginne schon damit, dass die ausgewählte Zeile nicht übertragen wird. Das funktioniert also in meinem ViewController (selectedlock ist eine Picker-View-Zeile): 'let key = keys [selectedlock]' Im App-Delegate gibt mir immer 0 als selektierte Sperre, egal was ich in der Picker-Ansicht ausgewählt habe: 'print (ViewController(). Selectedlock) ' – StephanD
Ich denke, Ihre Anwendungsarchitektur ist nicht bereit, so verwendet zu werden und es scheint, dass Sie herausfinden müssen, wie Klasseninstanzen und ihre Funktionen miteinander interagieren können. Z.B. Sie sollten sich nicht auf globale Variablen verlassen ('selectedLock' in' ViewController'), um zwischen Objekten zu interagieren. Eine Möglichkeit könnte die Verwendung von Benachrichtigungen sein: http://shipster.com/nsnotification-and-nsnotificationcenter/ –
Kann ich NSNotificationCenter verwenden, um diese Aktion in meinem ViewController auszulösen? – StephanD