我试图从ViewController定义用户通知操作。根据我的理解,必须在应用程序代表中设置通知与操作。swift,UIUserNotificationAction从视图控制器,而不是应用程序委托
所以here's我的应用程序代表:
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
}
这种运作良好。当我将“notification.category = categoryID”添加到我的LocalNotifications之一时,它会记录“做得好”。现在我想调用的动作实际上应该从视图控制器中调用。它有很多变量,你必须先登录才能访问服务器以调用操作等。我该怎么做?当我把这个功能放在我的视图控制器中时,动作并没有被调用:
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()
}
很多谢谢! Stephan
嗨,我没有尝试过。不幸的是我的视图控制器功能不适用于应用程序代理。我已经开始了,所选的行不会被传输。所以这在我的ViewController中工作(selectedlock是一个选择器视图行):'let key = keys [selectedlock]'在App Delegate中,无论我在选择器视图中选择了哪一个, (ViewController()。selectedlock)' – StephanD
我想你的应用程序架构还没有准备好像那样使用,你似乎需要弄清楚类实例和它们的函数是如何相互交互的。例如。你不应该依赖全局变量('ViewController'中的'selectedLock')来在对象之间进行交互。 一种方法可能是使用通知:http://nshipster.com/nsnotification-and-nsnotificationcenter/ –
我可以使用NSNotificationCenter在我的ViewController中触发此操作吗? – StephanD