In my 'viewDidLoad'in ViewController.m, I am registering to the NSNotificationCenter defaultCenter with 'EAAccessoryDidConnectNotification' and 'EAAccessoryDidDisconnectNotification'
When my app is active in foreground, I get the notification, and respond in accessoryDidConnect. all works OK.
But, when app is in background, how can I get such notification?
('EAAccessoryDidConnectNotification' and 'EAAccessoryDidDisconnectNotification')
[Code below]
Thanks a lot.
Dan
(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(accessoryDidConnect:)
name:EAAccessoryDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(accessoryDidDisconnect:)
name:EAAccessoryDidDisconnectNotification
object:nil];
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
}
According to the documentation, it's not possible:
If your app is suspended in the background when an accessory
notification arrives, that notification is put in a queue. When your
app begins running again (either in the foreground or background),
notifications in the queue are delivered to your app. Notifications
are also coalesced and filtered wherever possible to eliminate any
irrelevant events. For example, if an accessory was connected and
subsequently disconnected while your app was suspended, your app would
ultimately not receive any indication that such events took place.
But it would be interesting if someone proved me wrong.
in iOS 12.1.4, plug in the relay cable with iPhone and DSLR Camera when app is in foreground, then switch app into background mode, unplug the relay cable and switch app back into foreground, you will receive the accessoryDidDisconnect notify via EAAccessory's delegate methods.
Related
Have a simple QT console app, with QCoreApplication.
Is it possible to process notifications from notification center in another thread? For example create a thread and connect loop of this thread to process notifications? Or extend QT application to send me this notifications?
There are plenty of example in the source of Qt itself.(I did "git grep NSNotificationCenter" here.)
At least you could check the solution like following:
https://github.com/qtproject/qtbase/blob/stable/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm
L105-L109:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(updateScreens:)
name:NSApplicationDidChangeScreenParametersNotification
object:NSApp];
L130:
[[NSNotificationCenter defaultCenter] removeObserver:self];
I want to get notified when my application is in background with :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(calendarChanged:) name:EKEventStoreChangedNotification object:nil];
I tried to implement it but (calendarChanged:) never get called ?!
You should specify the EKEventStore object when registering the notification observer
Your method is not going to be called while in background, it will be called once your app comes to foreground.
Taken from this question, the search option doesn't hurt.
I have started listening to global keyDown events. Is there a way to get information from which application that event came?
A handler receives NSNotification instance and NSEvent is part of it. Can I somehow extract that information from those objects?
Listening snippet:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event){
NSLog(#"global keyDown %#", event);
[[NSNotificationCenter defaultCenter] postNotificationName:kKeyPressed
object:event];
}];
Observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyEventHandler:)
name:kKeyPressed
object:nil];
UPDATE
Global key downs are not send from any particular application. What I actually needed is to check for currently active application at event handler:
[[NSWorkspace sharedWorkspace] activeApplication]
This returns NSDictionary with information I needed.
You're not posting a distributed notification, or using a distributed notification center. This means you know that the notification came from the current application.
Meanwhile, you're generating the notifications yourself, so if you did need to know the application, you could just add that in.
Finally, the events you're embedding are global key events, which do not have an associated application. Except in special cases, they're not generated by any application, they're generated by the user typing on the keyboard.
I would like to perform certain cleanup tasks when the app shuts down. I use an observer like:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appWillResignActiveNotif:) name:UIApplicationWillResignActiveNotification object:nil];
to get notified when the app goes to background.
The problem is that if the app crashes, there is no notification for me to do something.
I saw that testflight.com use a hook to recover crash information, I was wondering if it was possible to also detect crashes and perform some tasks.
My concern is regarding the call to:
CLLocationManager.stopMonitoringSignificantLocationChanges
not being done when app crashes, leaving users with a constant location icon on top. I know that crashes are not supposed to be frequent, but would like to clean as much as possible if I can under these circumstances.
you could install a global exceptionHandler or even a signalHandler
http://www.cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html
but remember: dont continue running after a crash. it is NOT safe :D
I am trying to handle device orientation changes on a viewcontroller for one of my views.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad");
// Tell the UIDevice to send notifications when the orientation changes
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
// tell the director that the orientation has changed
- (void) orientationChanged:(NSNotification *)notification
{
NSLog(#"orientationChanged");
}
When I first launch the App, the orientationChanged selector gets called, but then after that it does not get called again no matter how much I rotate the iPad. Does anyone have any idea what I might be doing wrong? When I put similar code in the app delegate, it works fine, but in this particular view controller, it is not behaiving properly.
I know this question is a bit old, but just thought I'd add a link to a tutorial I found when I was in the same situation you were (wanting to get notifications for device rotation, regardless of what the interface orientation was).
This tutorial gives a nice and simple overview of how to handle device rotation using UIDeviceOrientationDidChangeNotification.
I also had issues with UIDeviceOrientationDidChangeNotification not firing (it worked fine in the simulator, but not on my device). After a bit of debugging I realised that I had Portrait Orientation Locked on the device, and this was causing the UIDeviceOrientationDidChangeNotification not to fire. Not sure if that's your issue, but it's something that's easy to overlook and worth checking.
the problem is that the orientation will not change unless you specify that it can do so. when you rotate the device/simulator, the framework calls
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
where interface orientation is the orientation the system wants to use, however you have to return YES or it will not change.
if you are not returning YES then your orientation is not changing and therefore you cannot get notified for an orientation change (because it did not happen)