Skip to content

Instantly share code, notes, and snippets.

@gali8
Last active December 3, 2018 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gali8/de2d6ff7ed21388304c1b539f1a66ebc to your computer and use it in GitHub Desktop.
Save gali8/de2d6ff7ed21388304c1b539f1a66ebc to your computer and use it in GitHub Desktop.
//
// AppDelegate.m
// Gap
//
// Created by Daniele on 02/04/13.
// Copyright (c) 2013 Daniele Galiotto - www.nexor.it. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
{}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[super application:application didFinishLaunchingWithOptions:launchOptions];
//register for notification
if IOS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
else
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
self.locationManager.delegate = self;
[self initRegion];
return YES;
}
//#pragma mark - appy days
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"E2C56DB3-AAAA-BBBB-CCCC-D0F5A7109622"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:0 minor:0 identifier:@"myidentifier1_1"];
self.beaconRegion.notifyEntryStateOnDisplay = YES;
self.beaconRegion.notifyOnEntry = YES;
self.beaconRegion.notifyOnExit = YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (state == CLRegionStateInside)
{
//Start Ranging
[manager startRangingBeaconsInRegion:self.beaconRegion];
}
else
{
//Stop Ranging here
[manager stopRangingBeaconsInRegion:self.beaconRegion];
}
}
-(void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error
{
}
//-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
//{
//////attivare solo quando in test, serve per entrare sempre nella region
//// [manager startRangingBeaconsInRegion:self.beaconRegion];
//}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[manager startRangingBeaconsInRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[manager stopRangingBeaconsInRegion:self.beaconRegion];
[self.arrayOfRegions removeAllObjects];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
//beacon in region found!
//NEVER instanciate CLBeacon!!!
CLBeacon *foundBeacon = [beacons firstObject];
NSLog(@"FOUND %@!!! proximity %i accuracy %f rssi %i", foundBeacon.proximityUUID, (int)foundBeacon.proximity, foundBeacon.accuracy, (int)foundBeacon.rssi);
switch (foundBeacon.proximity) {
case CLProximityImmediate:
break;
case CLProximityNear:
break;
case CLProximityFar:
break;
case CLProximityUnknown:
default:
break;
}
if(foundBeacon.accuracy < 0)
{
[manager startRangingBeaconsInRegion:region];
[self.arrayOfRegions removeAllObjects];
return;
}
//(foundBeacon.proximity == CLProximityImmediate || foundBeacon.proximity == CLProximityNear) &&
if(foundBeacon.accuracy <= 20 && foundBeacon.proximityUUID)
{
if(self.arrayOfRegions == nil)
self.arrayOfRegions = [NSMutableArray new];
//[manager stopRangingBeaconsInRegion:region];
if([self.arrayOfRegions containsObject:[foundBeacon.proximityUUID UUIDString]] == NO)
{
[self.arrayOfRegions addObject:[foundBeacon.proximityUUID UUIDString]];
}
else
{
return;
}
[self showLocalNotification:foundBeacon];
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
//go everywhere using postNotificationName
[[NSNotificationCenter defaultCenter] postNotificationName:menuSelectionProgrammatically object:NULL];
}
else
{
//mostra localnot e quando la apre fa segue
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
}
}
}
-(void)showLocalNotification:(CLBeacon *)foundBeacon
{
//recupera messaggio dall'url
NSString *urlString = [self getUrlStringForType:BackgroundUrl beacon:foundBeacon];
NSString *urlStringMessage = [urlString stringByAppendingString:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"UrlBackgroundSuffix"]];
NSError *error;
NSString *message = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStringMessage] encoding:NSStringEncodingConversionAllowLossy error:&error];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = message.length > 0 && error == nil ? message : @"Messaggio di test.";
localNotification.alertAction = @"Apri";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:4];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
} else {
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2]; //2 seconds
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
-(NSMutableString *)getUrlStringForType:(UrlToOpenType)urlToOpenType beacon:(CLBeacon *)beacon
{
NSMutableString *urlString = [NSMutableString stringWithString:[[NSBundle mainBundle] objectForInfoDictionaryKey:urlToOpenType == ForegroundUrl ? @"UrlForeground" : @"UrlBackground"]];
[urlString replaceOccurrencesOfString:@"{uuid}" withString:[beacon.proximityUUID UUIDString] options:NSCaseInsensitiveSearch range:NSMakeRange(0, urlString.length)];
[urlString replaceOccurrencesOfString:@"{minor}" withString:[NSString stringWithFormat:@"%@", beacon.minor] options:NSCaseInsensitiveSearch range:NSMakeRange(0, urlString.length)];
[urlString replaceOccurrencesOfString:@"{major}" withString:[NSString stringWithFormat:@"%@", beacon.major] options:NSCaseInsensitiveSearch range:NSMakeRange(0, urlString.length)];
return urlString;
}
//#pragma mark
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
}
#endif
-(MainContainerInViewController *)searchInViewController:(UIViewController *)dadViewController
{
if([dadViewController isKindOfClass:[MainContainerInViewController class]])
{
return (MainContainerInViewController *)dadViewController;
}
else
{
for (UIViewController *childVc in dadViewController.childViewControllers)
{
return [self searchInViewController:childVc];
}
}
return nil;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[super applicationWillResignActive:application];
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
//#pragma mark - Appy Days
[self.arrayOfRegions removeAllObjects];
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
[self.locationManager stopMonitoringForRegion:self.beaconRegion];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
//#pragma mark -
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment