如何使用UILocalNotification打开包含CoreData的特定视图

时间:2015-08-03 19:06:17

标签: ios xcode swift core-data uilocalnotification

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

enter image description here

当用户点击" First Action"按钮我希望他被重定向到第二和第四视图。

继承人我正在尝试的事情

来自我的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()       
}

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

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

我知道没有内置机制让UILocalNotification直接将您带到应用程序用户界面的特定部分。

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

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

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