Reputation: 845
I am dealing with the External Accessory Framework and here is my code for registering the notofication..
override func viewDidLoad() {
super.viewDidLoad()
EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
}
And here is my method handling function...
func accessoryDidConnectNotify(notification: NSNotification){
let alert : UIAlertController = UIAlertController(title: "Alert", message: "MFi Accessory Connected", preferredStyle:UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
}))
self.presentViewController(alert, animated: true, completion: nil)
And my problem is if i dont give any parameters inside the accessoryDidConnectNotify function the application works good proceeding with the alert view when I insert a MFi accessory..
(i.e) func accessoryDidConnectNotify(){ // works fine (with no arguments)
}
but i need the NSNotification object to be used inside my accessoryDidConnectNotify function to get the name of the accessory ...but if I add the NSNotification object the appliaction crashes on inserting a MFi accessory...
(i.e) func accessoryDidConnectNotify(notification: NSNotification){
} // crashes app (with arguments)
If someone also came across the problem...please do share
Upvotes: 5
Views: 383
Reputation: 71852
If your method doesn't have any parameter then you can call it this way:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
by using "accessoryDidConnectNotify"
.
So that you can use that method like:
func accessoryDidConnectNotify(){ // works fine (with no arguments)
//Your code
}
But if your method have parameters then you have to call it this way:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify:", name: EAAccessoryDidConnectNotification, object: nil)
By using this "accessoryDidConnectNotify:"
. here you have to add :
.
Now you can call your method with parameters this way:
func accessoryDidConnectNotify(notification: NSNotification){
//Your code
}
Upvotes: 4