如何从 ViewController 或其他地方调用或触发 AppDelegate 中的通知功能?
How to call or trigger notification function in AppDelegate from ViewController or somewhere else?
我如何调用这种在 ViewController
、appDelegate
或其他地方的方法:
func contactDelete(notification : NSNotification){}
我必须在 appDelegate
里面调用它
`didFinishLaunchingWithOptions` here or inside other classes:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
if let aLaunchOptions = launchOptions { // Checking if there are any launch options.
// Check if there are any local notification objects.
if let notification = (aLaunchOptions as NSDictionary).objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification {
// Handle the notification action on opening. Like updating a table or showing an alert.
UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
let root : UIViewController = self.window!.rootViewController! as UIViewController
let alertview = JSSAlertView().show(root, title: "Oops!", text: "Unable to add the new Contact, check contact permission from Settings.", buttonText: "Check it", cancelButtonText: "Nope", color: UIColor.init(red: 0.216, green:0.043, blue:0.129, alpha: 1))
alertview.addAction(contactDelete)
alertview.setTextTheme(.Light)
}
}
它需要一些额外的参数,但我不知道是什么参数。我什至不能在 viewDidLoad
方法中调用它。我试过像这样触发它,但它不起作用:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.contactDelete(_:)), name:DELETECONTACT, object: nil)
在您的 Appdelegate 代码中,您根据通知到达或简单启动分配 RootviewController 使用:
if let notification = (aLaunchOptions as NSDictionary).objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification {
但是你将 NotificationViewController
设置为 rootViewController
并且你在 MainViewController 上 addObserver
所以当你收到通知并且你的 rootView 设置为 NotificationViewController
你不会NSNotificationCenter 已创建,您无法 post 它。所以我建议创建如下代码:
你的 appdelegate didFinishLaunchingWithOptions
方法:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert , categories: nil))
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("first") as UIViewController
let navController: UINavigationController = UINavigationController(rootViewController: initialViewControlleripad)
navController.navigationBarHidden = true
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
if (notification != nil) {
runAfterDelay(2.0) { // add 2 second delay of call method open notificationViewController from mainViewController
self.FireNewViewControlelr(notification.userInfo!)
}
}
return true
}
以下是两种调用NotificationViewController的方法:
func FireNewViewControlelr(Value: [NSObject : AnyObject]) {
print("userInf \(Value)")
let navigationController: UINavigationController = (self.window!.rootViewController as! UINavigationController)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
NSNotificationCenter.defaultCenter().postNotificationName(DELETECONTACT, object: Value)
navigationController.pushViewController(initialViewControlleripad, animated: true)
}
func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), block)
}
如果您的应用程序处于后台模式并且您通过点击通知横幅打开应用程序 didReceiveLocalNotification
如下所示:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
print("notification - tapped")
if application.applicationState == UIApplicationState.Active {
print("App already open")
} else {
print("App opened from Notification")
runAfterDelay(2.0) {
self.FireNewViewControlelr(notification.userInfo!)
}
}
}
更改以下方法中的代码:
func FireNewViewControlelr(Value: [NSObject : AnyObject]) {
print("userInf \(Value)")
let navigationController: UINavigationController = (self.window!.rootViewController as! UINavigationController)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : NotificationViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
initialViewControlleripad.notificationInfo = Value
navigationController.pushViewController(initialViewControlleripad, animated: true)
}
在NotificationViewController
var notificationInfo = [NSObject : AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
print("data is \(notificationInfo)")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func action(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName(DELETECONTACT, object: notificationInfo)
self.navigationController?.popToRootViewControllerAnimated(true)
}
我如何调用这种在 ViewController
、appDelegate
或其他地方的方法:
func contactDelete(notification : NSNotification){}
我必须在 appDelegate
里面调用它
`didFinishLaunchingWithOptions` here or inside other classes:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
if let aLaunchOptions = launchOptions { // Checking if there are any launch options.
// Check if there are any local notification objects.
if let notification = (aLaunchOptions as NSDictionary).objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification {
// Handle the notification action on opening. Like updating a table or showing an alert.
UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
let root : UIViewController = self.window!.rootViewController! as UIViewController
let alertview = JSSAlertView().show(root, title: "Oops!", text: "Unable to add the new Contact, check contact permission from Settings.", buttonText: "Check it", cancelButtonText: "Nope", color: UIColor.init(red: 0.216, green:0.043, blue:0.129, alpha: 1))
alertview.addAction(contactDelete)
alertview.setTextTheme(.Light)
}
}
它需要一些额外的参数,但我不知道是什么参数。我什至不能在 viewDidLoad
方法中调用它。我试过像这样触发它,但它不起作用:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.contactDelete(_:)), name:DELETECONTACT, object: nil)
在您的 Appdelegate 代码中,您根据通知到达或简单启动分配 RootviewController 使用:
if let notification = (aLaunchOptions as NSDictionary).objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification {
但是你将 NotificationViewController
设置为 rootViewController
并且你在 MainViewController 上 addObserver
所以当你收到通知并且你的 rootView 设置为 NotificationViewController
你不会NSNotificationCenter 已创建,您无法 post 它。所以我建议创建如下代码:
你的 appdelegate didFinishLaunchingWithOptions
方法:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert , categories: nil))
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("first") as UIViewController
let navController: UINavigationController = UINavigationController(rootViewController: initialViewControlleripad)
navController.navigationBarHidden = true
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
if (notification != nil) {
runAfterDelay(2.0) { // add 2 second delay of call method open notificationViewController from mainViewController
self.FireNewViewControlelr(notification.userInfo!)
}
}
return true
}
以下是两种调用NotificationViewController的方法:
func FireNewViewControlelr(Value: [NSObject : AnyObject]) {
print("userInf \(Value)")
let navigationController: UINavigationController = (self.window!.rootViewController as! UINavigationController)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
NSNotificationCenter.defaultCenter().postNotificationName(DELETECONTACT, object: Value)
navigationController.pushViewController(initialViewControlleripad, animated: true)
}
func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), block)
}
如果您的应用程序处于后台模式并且您通过点击通知横幅打开应用程序 didReceiveLocalNotification
如下所示:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
print("notification - tapped")
if application.applicationState == UIApplicationState.Active {
print("App already open")
} else {
print("App opened from Notification")
runAfterDelay(2.0) {
self.FireNewViewControlelr(notification.userInfo!)
}
}
}
更改以下方法中的代码:
func FireNewViewControlelr(Value: [NSObject : AnyObject]) {
print("userInf \(Value)")
let navigationController: UINavigationController = (self.window!.rootViewController as! UINavigationController)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : NotificationViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
initialViewControlleripad.notificationInfo = Value
navigationController.pushViewController(initialViewControlleripad, animated: true)
}
在NotificationViewController
var notificationInfo = [NSObject : AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
print("data is \(notificationInfo)")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func action(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName(DELETECONTACT, object: notificationInfo)
self.navigationController?.popToRootViewControllerAnimated(true)
}