Saturday, August 25, 2018

Understanding iOS NSNotification






iOS notification center delivers notifications to observers synchronously. In other words, when posting a notification, control does not return to the poster until all observers have received and processed the notification. 
To send notifications asynchronously use a notification queue, which is described in Notification Queues.
In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

This is similar to how javascript addEventListener and dispatchEvent works, so when dispatchEvent is called, the method will not return to caller until all added event listeners returns from their event handler call back methods.
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent

Wednesday, August 15, 2018

Utility functions for ios NSUserDefault

1. Dump all items in NSUserDefault


NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys];

for(NSString* key in keys){
        NSLog(@"%@ : %@",key, [[NSUserDefaults standardUserDefaults] valueForKey:key]);
}



2. Observe changes in NSUserDefault


- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
  [[NSUserDefaults standardUserDefaults] addObserver:self
                                            forKeyPath:@"mykey"
                                               options:NSKeyValueObservingOptionNew
                                               context:NULL];

    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
    if([keyPath isEqual:@"mykey"])
    {
       NSLog(@"SomeKey change: %@", change);
    }
}

3. Settings bundle with NSUserDefault
Application can use ios Settings app to allow user to configure settings. However, settings put into Settings.bundle root.plist will not be automatically picked up when app reads settings from NSUserDefault. The settings only apply to NSUserDefault after user change the value from settings app.
So if user has not change a setting from ios setting app, read the key from nsuserdefault will return nil, which means the key does not exist in NSUserDefault.
One way to overcome this issue is calling NSUserDefault regsiterDefault method, this will set the initial values in NSUserDefault for all the keys in NSDictionary parameter. 

Friday, August 3, 2018

How to revert from iOS 12 beta to iOS 11

Recently a device was upgraded to 12 beta for testing, but as the device was previously used by other developers, so there is no backup available for restoring the device to ios 11.

Tried few things mentioned in web, but unfortunately none of them works.

Finally, find a simple way to do so, by just deleting the ios 12 beta installation profile from device settings app at Settings->general->Profile & Device Management, restore the device using iTune. It will restore the device to a new ios 11.4.1 setup.