Method Call By Audio Jack
Add framework : -
- CoreLocation
- AudioToolbox
Background Mode in Capabilities : -
Add In info.plist : -
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Your location is shared with Campus Security Staff to give them Route instructions on map so that they can reach you. </string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is shared with Campus Security Staff to give them Route instructions on map so that they can reach you. </string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is shared with Campus Security Staff to give them Route instructions on map so that they can reach you. </string>
ViewController.m : -
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
int countHeadPhonePlugInOut;
double headPhonePluggedTimeStamp;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
countHeadPhonePlugInOut = 0;
headPhonePluggedTimeStamp = [[NSDate date] timeIntervalSince1970];
//Lat long code
locationManager = [[CLLocationManager alloc] init];
[locationManager requestAlwaysAuthorization];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//-----------------------------
locationManager.allowsBackgroundLocationUpdates = true;
if (@available(iOS 11.0, *)) {
locationManager.showsBackgroundLocationIndicator = false;
} else {
// Fallback on earlier versions
}
locationManager.delegate = self;
[locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:CLTimeIntervalMax];
//-----------------------------
[locationManager startUpdatingLocation];
[self setupNotifications];
}
-(void) appDidEnterBackground{
[self setupNotifications];
}
- (void)routeChanged:(NSNotification *)notification {
//============================
NSNumber *reason = [notification.userInfo objectForKey:AVAudioSessionRouteChangeReasonKey];
if ([reason unsignedIntegerValue] == AVAudioSessionRouteChangeReasonNewDeviceAvailable) {
NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
} else if ([reason unsignedIntegerValue] == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
double difference = [[NSDate date] timeIntervalSince1970] - headPhonePluggedTimeStamp;
NSLog(@"difference : %f", difference);
if (difference > 5) {
headPhonePluggedTimeStamp = [[NSDate date] timeIntervalSince1970];
countHeadPhonePlugInOut = 1;
NSLog(@"Reset Count");
}else {
NSLog(@"countHeadPhonePlugInOut : %d", countHeadPhonePlugInOut);
countHeadPhonePlugInOut++;
}
if (countHeadPhonePlugInOut >=3) {
NSLog(@"Start count again");
AudioServicesPlayAlertSoundWithCompletion(kSystemSoundID_Vibrate
, ^{
});
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Audio Jack Method Done"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Handel your yes please button action here
}];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
NSLog(@"******************************************");
countHeadPhonePlugInOut = 0;
headPhonePluggedTimeStamp = [[NSDate date] timeIntervalSince1970];
}else {
}
}
}
-(void) setupNotifications{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChanged:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
}
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
// CLLocation *location = locations[0];
// NSLog(@"location.coordinate.latitude :%f, location.coordinate.longitude : %f", location.coordinate.latitude, location.coordinate.longitude);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
