Thursday 22 October 2015

Proximity in IOS application


First of all what is proximity in IOS ?

Do you ever play audio in Whatsapp in iphone ? when it is playing far from the face its volume is high and when you take it near to your ear or face then you can hear the audio in Receiver from microphone. This functionality is implemented by checking proximity.

First  Proximity can not be checked in Ipod and Ipad , You can check whether your device support Proximity or not by implement this code.

      
UIDevice *device = [UIDevice currentDevice];
device.proximityMonitoringEnabled = YES;    

if (device.proximityMonitoringEnabled == YES) // yes your device support proximity
{
    NSLog(@"proximity enabled");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChanged:) name:@"UIDeviceProximityStateDidChangeNotification" object:device];
} else {
    NSLog(@"proximity not enabled");
}


When proximity change you can change your avaudiosession property When your device is near to your face you can overrideOutputAudioPort to    AVAudioSessionPortOverrideNone and when your
device is far from your face you can overrideOutputAudioPort to    AVAudioSessionPortOverrideSpeaker so it will play in speaker

- (void) proximityChanged:(NSNotification *)notification {
    NSLog(@"proximity changed called");
    UIDevice *device = [notification object];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL success;
     success = [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    if(device.proximityState == 1){
        NSLog(@"bool %s", success ? "true" : "false");
        [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
        [audioSession setActive:YES error:nil];
    }
    else{
        NSLog(@"bool %s", success ? "true" : "false");
        [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
        [audioSession setActive:YES error:nil];
        }
}


Now proximity should be off when it is not needed , that's why in viewWillAppear of each screen you should make it of and only when your audio is playing on it .

 -(void)viewWillAppear:(BOOL)animated
    {   
        [super viewWillAppear:animated];

        UIDevice *device = [UIDevice currentDevice];

        device.proximityMonitoringEnabled = NO;
    }

Monday 5 October 2015

StoryBoard Reference in Xcode 7


If you have multiple Storyboard in your project if you want to navigate from one storyboard to other storyboard until Xcode 6.4 you have to use this code  :

first make object of storyboard , then make object of that storyboard's view controller

      self.myProfileStoryboard = [UIStoryboard storyboardWithName:@"MyProfile"
                                                        bundle: nil];
     
      self.homeNavC = (AHKNavigationController*)[self.homeStoryboard
 
                                             instantiateViewControllerWithIdentifier: @"HomeViewControllerIDNavCntrl"];



But now in Xcode 7 you can use  Storyboard Reference


1) Make one storyboard , add one navigationController and add one ViewController name it FirstViewController

















2) In First Storyboard add one storyboard Reference as under

3) Add  one ViewController in  second storyboard 
4) In second storyboard gave StoryboardId - SecondViewID for that viewController 






5)  Select FirstView Controller and gave the values as under:



Thats you done run your project and you can see you can navigate from one storyboard to another storyboard using StoryBoard Reference.


But this project you can not open now in previous xcode if you open you will see following error