ios - 如何使用 UILocalNotification 打开包含核心数据的特定 View

标签 ios xcode swift core-data uilocalnotification

我正在开发一个应用程序,用户可以在其中设置通知提醒,以更改已设置提醒的 CoreData 实例的密码。

enter image description here

当用户点击“第一个操作”按钮时,我希望他被重定向到第二个和第四个 View 。

这是我正在尝试的

来 self 的 AirlineViewController.swift 和 AppDelegate.swift 的相关代码

AirlineViewController.swift:

@IBAction func buttonTapped(sender: AnyObject) {
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"showAMessage:", name: "actionOnePressed", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"drawAShape:", name: "actionTwoPressed", object: nil)


    var dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015;
    dateComp.month = 08;
    dateComp.day = 03;
    dateComp.hour = 21;
    dateComp.minute = 03;
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date:NSDate = calender.dateFromComponents(dateComp)!


    var notification:UILocalNotification = UILocalNotification()
    notification.category = "FIRST_CATEGORY"
    notification.alertBody = "Hi, I am a notification"
    notification.fireDate = date

    UIApplication.sharedApplication().scheduleLocalNotification(notification)

}


func drawAShape(notification:NSNotification){
    println("drawAShape")
    var view:UIView = UIView(frame:CGRectMake(10, 10, 100, 100))
    view.backgroundColor = UIColor.redColor()

    self.view.addSubview(view)
    println("drawAShape")

}

func showAMessage(notification:NSNotification){

    var message:UIAlertController = UIAlertController(title: "A Notification Message", message: "Hello there", preferredStyle: UIAlertControllerStyle.Alert)
    message.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(message, animated: true, completion: nil)
}

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //Actions
    var firstAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    firstAction.identifier = "FIRST_ACTION"
    firstAction.title = "First Actions"

    firstAction.activationMode = UIUserNotificationActivationMode.Foreground
    firstAction.destructive = true
    firstAction.authenticationRequired = false

    var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    secondAction.identifier = "SECOND_ACTION"
    secondAction.title = "Second Actions"

    secondAction.activationMode = UIUserNotificationActivationMode.Foreground
    secondAction.destructive = false
    secondAction.authenticationRequired = false

    var thirdAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    thirdAction.identifier = "THIRD_ACTION"
    thirdAction.title = "Third Actions"

    thirdAction.activationMode = UIUserNotificationActivationMode.Background
    thirdAction.destructive = false
    thirdAction.authenticationRequired = false

    //Category

    var firstCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
    firstCategory.identifier = "FIRST_CATEGORY"

    let defaultActions:NSArray = [firstAction, secondAction, thirdAction]
    let minimalActions:NSArray = [firstAction, secondAction]

    firstCategory.setActions(defaultActions as [AnyObject], forContext: UIUserNotificationActionContext.Default)
    firstCategory.setActions(minimalActions as [AnyObject], forContext: UIUserNotificationActionContext.Minimal)

    //NSSet of all our categories
    let categories:NSSet = NSSet(objects: firstCategory)


    let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge

    let mySettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as Set<NSObject>)

    UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)


    return true
}

func application(application: UIApplication!,
    handleActionWithIdentifier identifier:String!,
    forLocalNotification notification:UILocalNotification!,
    completionHandler: (() -> Void)!){

        if (identifier == "FIRST_ACTION"){

            NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)

        }else if (identifier == "SECOND_ACTION"){
            NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)

        }

        completionHandler()       
}

此代码的问题在于,如果应用程序完全关闭,它只会打开最后一个 View 或第一个 View 。

谁能帮帮我

最佳答案

据我所知,没有任何内置机制可以让 UILocalNotification 直接将您带到应用 UI 的特定部分。

您可以在发布前将 userInfo 字典附加到 UILocalNotification

然后您可以查询该 userInfo 字典,找出应该将用户带到您的 UI 中的什么位置,并编写自定义代码以将用户带到应用 UI 的那个部分。

我正在为我正在为客户开发的应用程序做这件事。我嵌入了让应用程序调用相关屏幕所需的信息。 (具体取决于应用程序的设计,它使用自定义表单描述文件来描述应用程序中 View Controller 的层次结构。)

关于ios - 如何使用 UILocalNotification 打开包含核心数据的特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794902/

相关文章:

ios - 打开 subview 后背景 uiview 仍然可触摸

swift - acceptsFirstMouse 在 macOS 上点击

ios - 迪尔德 : Library not loaded SwiftUI when app runs on iOS 12 using @available(iOS 13. 0, *)

ios - (iOS) 如何使用 shapeLayer 为圆角矩形制作动画?

ios - 如何使用导航 Controller 执行segue?

ios - ABAddressBookCopyArrayOfAllPeople 不返回任何人

java - 使用 GMImagePickerController 播放视频时黑屏

ios - 什么是已弃用的推送转场? (iOS 8)

ios - Swift 中 block 的语法

swift - 从 nsmutablearray swift3 Playground 中获取字典项目