Setting NSNotifications observer in a cordova plug in - ios

I have a cordova plugin that depends on a an accessory being connected to my iOS device. The accessory (a magnetic swiper) on notification that something is being swiped. However I not sure what I should be setting the observer as. In the example code they provide it they set it as self but that in a ViewController My plug in is not in the app controller. Any ideas?
this is the notification code that the sample code had( again this was in the ViewController:
[[NSNotificationCenter defaultCenter] addObserver: self
selector:#selector(trackDataReady:)
name: #"trackDataReadyNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(devConnStatusChange)
name:#"devConnectionNotification"
object:nil];

As long as the object (self) is still allocated it will receive the messages when they are posted to the NSNotificationCenter defaultCenter. It does not have to be a view controller. Remember though when the object is deallocated removed the observers otherwise you will get application crashes.

Related

iOS UIApplicationWillEnterForegroundNotification Without Memory Leak?

I currently make use of this method in my objective-C app:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleEnteredForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
If I don't remove it with
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
am I at risk of a memory leak? Or is there a good way to utilize this notification without the risk of a memory leak?
There is no danger of a memory leak; the notification center's reference to your observer self is weak.
But there is a danger — namely, that self will go out of existence and that the notification center will later attempt to send it a notification. This will cause a horrific crash, one that is very difficult to track down (dangling pointer).
That is why you must be certain to unregister your observer in iOS 8 and before.
Starting in iOS 9, however, this ceases to be a problem, because the notification center's reference to your observer is not simply weak but is ARC-weak. This means that the reference to a released observer becomes nil. The notification center detects this and stops sending notifications to it, safely.

Method in viewWillAppear and viewDidLoad didn't loaded

I have created a method to check the status of a server in my viewcontroller, I need to check this, everytime I will open the app.
I call [self checkStatus]; from viewWillAppear and viewDidLoad, but when I open the app, by clicking home button, and I try to open the app again (clicking the app icon in applications) this method is not called. I have a NSLog to view when it is launched or not.
I'm frustrated. Thanks for your help.
You can react to app changes using NotificationCenter:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doSomething:) name:UIApplicationDidBecomeActiveNotification object:nil];
BTW: don't forget to removeObserver when you don't need it!
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
You can also use UIApplicationWillEnterForegroundNotification etc, depending what do you need.
You should read about app lifecycle on Apple Developer pages :). Read:
AppleDeveloperLink Especially section: "Execution States for Apps" to know more about app lifecycle.
StackOverflowLink to know more about view lifecycle.
iOS is not calling those methods again, but the delegate methods in the AppDelegate. You have to propagate the message to your controllers then.
I hope this will help you: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/

On which ViewController should I implement FacebookSDK's removeObserver?

I am implementing facebook's SDK for iOS into my app. There are two functions however that are supposed to register and unregister for the notifications:
From Facebook's login to facebook with ios:
in the viewDidLoad method, register for the session change notification you defined in the app delegate by adding this code to the end of the method:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(sessionStateChanged:)
name:FBSessionStateChangedNotification
object:nil];
and
Unregister for the notifications by adding the following code to the end of the didReceiveMemoryWarning the method:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Since I have quite a few view controllers and all of them should be using facebook's API, I thought I should implement the register/unregister methods in the applicationDidFinishLoadingWithOptions (for register for notifications)
but I am not sure if and how I should implement the unregister's removeObserver command, because applicationDidReceiveMemoryWarning is not available for the AppDelegate.
Is DidReceiveMemoryWarning visiting all the viewControllers of the App?
Would it be sufficient to unregister in just one of my viewControllers ?
The application delegate does receive memory warnings:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
If it didn't, another option would be to use the notification center:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(whatever:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
That all said, it seems to me that removing the observer on a memory warning is inappropriate. At what point will your reinstate it? But hey, if that's what Facebook recommends...

How to judge whether it is a new day in an iOS project?

I have a UIView ,I want it to display in a new day.For example,today I launch my app,and that UIView comes to my view,but after this,I can't see that UIView until tomorrow.
So I want to know how to judge whether it is a new day to display the UIView?
You need to register for the UIApplicationSignificantTimeChangeNotification notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(timeChange) name:UIApplicationSignificantTimeChangeNotification object:nil];
Then in the timeChange method (or whatever you call it) update your view.
Don't forget to unregister:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationSignificantTimeChangeNotification object:nil];
This notification is sent at midnight, if the user changes the time on their device, or the timezone changes. If your app is in the background when the change happens, it will get notified when the app returns to the foreground.

Using two delegates

I am new to iPhone programming. I want to call a function which belongs to one of my views in MyAppDelegate. I want to do this because I cannot find any ApplicationDidEnterBackground() event in view.
So, basically I want to call view's function when the application is minimized. I did try using delegate. However I cannot find how to use two delegates as MyAppDelegate already has one. Please help.
The general design pattern is, if you have multiple interested parties in an event, don't use delegates but notifications. In this case, you should register a UIApplicationDidEnterBackgroundNotification in your -init method of every class where you wanna handle the event:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
Then write a -(void)didEnterBackground method with your code. In your -dealloc method, make sure to unregister the notification:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
Also, be aware that this code requires iOS 4.x or higher. If you want to maintain iOS 3 compatibility you've to check the availability first:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(applicationState)]) {
// register UIApplicationDidEnterBackgroundNotification
}

Resources