UIPageViewController that has UIPageControl with different dot colours

 Customise page control

  1. Create page control subclassing UIView
  2. Draw the coloured dots in - (void) drawRect:(CGRect) rect
  3. Control the touch events using - (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
  4. Inform page view controller when user touch it using delegation.

Customise page view controller

  1. Override - (void) viewDidLayoutSubviews and search all the subviews and find the UIPageControl instance.
  2. Hide the UIPageControl instance and add the custom page control instance to its superview.
  3. Set the UIPageControl instance frame to custom page control.
  4. Bring the custom page control using [self.view bringSubviewToFront:pageControl];
  5. When custom page control informs you about the user touch then call the method - (void)setViewControllers:(nullable NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion; to move to right or left page.

Page view controller delegate

  1. Update custom control page dots/index using
    - (void) pageViewController:(COPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *) previousViewControllers transitionCompleted:(BOOL) completed
  2. This method - (void) pageViewController:(COPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *) pendingViewControllers will start when user swipes the page. What I did is maintained a variable that tells whether the page is still in transition, based on that I will allow the user interaction with custom page control.

 

Advertisements

CoreTelephony CTCallCenter

The [[CTCallCenter alloc] init] must be run in the main queue. Is it thread safe ??? Better call it on main thread only.

There is an iOS bug that causes instances of the CTCallCenter class to sometimes get notifications after they have been deallocated. Instead of instantiating, using, and releasing instances you must instead retain and never release them to work around the bug.

static CTCallCenter *netInfo; static dispatch_once_t dispatchToken; if (!netInfo) { dispatch_once(&dispatchToken, ^{ netInfo = [[CTCallCenter alloc] init]; }); }

 

How to programmatically know Bluetooth headset is connected to iOS device

Call this method to find out the bluetooth headset is connected or not.

 

- (BOOL) isBluetoothHeadsetConnected
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    AVAudioSessionRouteDescription *routeDescription = [session currentRoute];

    DEBUGLOG(@"Current Routes : %@", routeDescription);

    if (routeDescription)
    {
        NSArray *outputs = [routeDescription outputs];

        if (outputs && [outputs count] > 0)
        {
            AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
            NSString *portType = [portDescription portType];

            DEBUGLOG(@"dataSourceName : %@", portType);

            if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"])
            {
                return YES;
            }
        }
    }

    return NO;
}

How to enable or disable Proximity sensor programmatically in iPhone

Use the following API to enable/disable the proximity sensor.

[UIDevice currentDevice].proximityMonitoringEnabled = NO; // Disables the Proximity Sensor

[UIDevice currentDevice].proximityMonitoringEnabled = YES; // Enables the Proximity Sensor

Not all iOS devices have proximity sensors. To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityMonitoringEnabled property remains NO, proximity monitoring is not available.

Understand NSAssert (and its stdlib equivalent Assert)

Assert is to make sure a value is what its supposed to be. If an assertion fails that means something went wrong and so the app quits.

It is important to make sure that there is no data loss in any such situation.

Note that XCode 4 has NS_BLOCK_ASSERTIONS defined by default in release configurations. I guess if you don’t change that your released code will not contain NSAsserts. So, just put the macro in your distribution target [only].

NSAssert (and its stdlib equivalent Assert) can be very useful for debugging/unit testing, and also when you provide frameworks to stop the users from doing “evil” things. So not only does it safe-guard against potentially bad inputs but it logs them in a useful, standard way.

Assertions are commonly used to enforce the intended use of a particular method or piece of logic. They enforce that your code is used only as intended.

The value that’s passed is entered by the user, you need to do proper validation of the input rather than relying on the assertion. A user entering a value that is not allowed should be followed by a UI error, not NSAssert crashing the application.

NSAssert is to throw an exception, which you can catch and handle (try/catch).

KVO crash NSKVODeallocate

This crash happens when an instance gets released before all key value observers pointing to its properties release. You need to call removeObserver:forKeyPath: before releasing the instance.

In another scenario, it will crash if you call removeObserver:forKeyPath: on an instance if none of the key value observers are pointing to its properties.

how to uninstall unnecessary simulators in Xcode?

You can find and delete the simulators from the path /Library/Developer/CoreSimulator/Profiles/Runtimes. Restart Xcode in order to take effect.

or

You can delete a simulator from Xcode by navigation through Menu > Window > Devices > Simulators.