Advertisement
Guest User

AppDelegate.swift

a guest
Jun 22nd, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  AppDelegate.swift
  3. //  iBeacon Demo
  4. //
  5. //  Created by Orkhan Alizade on 15.06.15.
  6. //  Copyright (c) 2015 Orkhan Alizade. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreLocation
  11.  
  12. @UIApplicationMain
  13. class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
  14.    
  15.     var window: UIWindow?
  16.     var locationManager: CLLocationManager?
  17.     var lastProximity: CLProximity?
  18.    
  19.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
  20.         let uuidString = "B9407F30-F5F8-466E-AFF9-25556B57FE6"
  21.         let beaconIdentifier = "Apexx Group"
  22.         let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)!
  23.         let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID,
  24.             identifier: beaconIdentifier)
  25.        
  26.         locationManager = CLLocationManager()
  27.        
  28.         if(locationManager!.respondsToSelector("requestAlwaysAuthorization")) {
  29.             locationManager!.requestAlwaysAuthorization()
  30.         }
  31.        
  32.         locationManager!.delegate = self
  33.         locationManager!.pausesLocationUpdatesAutomatically = false
  34.        
  35.         locationManager!.startMonitoringForRegion(beaconRegion)
  36.         locationManager!.startRangingBeaconsInRegion(beaconRegion)
  37.         locationManager!.startUpdatingLocation()
  38.        
  39.         if(application.respondsToSelector("registerUserNotificationSettings:")) {
  40.             application.registerUserNotificationSettings(
  41.                 UIUserNotificationSettings(
  42.                     forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound,
  43.                     categories: nil
  44.                 )
  45.             )
  46.         }
  47.        
  48.         return true
  49.     }
  50.    
  51.     func applicationWillResignActive(application: UIApplication) {
  52.         // 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.
  53.         // 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.
  54.     }
  55.    
  56.     func applicationDidEnterBackground(application: UIApplication) {
  57.         // 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.
  58.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  59.     }
  60.    
  61.     func applicationWillEnterForeground(application: UIApplication) {
  62.         // 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.
  63.     }
  64.    
  65.     func applicationDidBecomeActive(application: UIApplication) {
  66.         // 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.
  67.     }
  68.    
  69.     func applicationWillTerminate(application: UIApplication) {
  70.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  71.     }
  72.    
  73.    
  74. }
  75.  
  76. extension AppDelegate: CLLocationManagerDelegate {
  77.     func sendLocalNotificationWithMessage(message: String!, playSound: Bool) {
  78.         let notification:UILocalNotification = UILocalNotification()
  79.         notification.alertBody = message
  80.        
  81.         if(playSound) {
  82.             notification.soundName = "tos_beep.caf";
  83.         }
  84.        
  85.         UIApplication.sharedApplication().scheduleLocalNotification(notification)
  86.     }
  87.    
  88.     func locationManager(manager: CLLocationManager!,
  89.         didRangeBeacons beacons: [AnyObject]!,
  90.         inRegion region: CLBeaconRegion!) {
  91.             let viewController:ViewController = window!.rootViewController as! ViewController
  92.             viewController.beacons = beacons as! [CLBeacon]?
  93.            
  94.             //            NSLog("didRangeBeacons");
  95.             var message:String = ""
  96.            
  97.             var playSound = false
  98.            
  99.             if(beacons.count > 0) {
  100.                 let nearestBeacon:CLBeacon = beacons[0] as! CLBeacon
  101.                
  102.                 if(nearestBeacon.proximity == lastProximity ||
  103.                     nearestBeacon.proximity == CLProximity.Unknown) {
  104.                         return;
  105.                 }
  106.                 lastProximity = nearestBeacon.proximity;
  107.                
  108.                 switch nearestBeacon.proximity {
  109.                 case CLProximity.Far:
  110.                     return
  111.                 case CLProximity.Near:
  112.                     message = "You are near the beacon"
  113.                     playSound = true
  114.                 case CLProximity.Immediate:
  115.                     return
  116.                 case CLProximity.Unknown:
  117.                     return
  118.                 }
  119.             } else {
  120.                
  121.                 if(lastProximity == CLProximity.Unknown) {
  122.                     return;
  123.                 }
  124.                
  125.                 //                message = "No beacons are nearby"
  126.                 playSound = true
  127.                 lastProximity = CLProximity.Unknown
  128.             }
  129.            
  130.             //            NSLog("%@", message)
  131.             sendLocalNotificationWithMessage(message, playSound: playSound)
  132.     }
  133.    
  134.     func locationManager(manager: CLLocationManager!,
  135.         didEnterRegion region: CLRegion!) {
  136.             manager.startRangingBeaconsInRegion(region as! CLBeaconRegion)
  137.             manager.startUpdatingLocation()
  138.            
  139.             //            NSLog("You entered the region")
  140.             //            sendLocalNotificationWithMessage("You entered the region", playSound: false)
  141.     }
  142.    
  143.     func locationManager(manager: CLLocationManager!,
  144.         didExitRegion region: CLRegion!) {
  145.             manager.stopRangingBeaconsInRegion(region as! CLBeaconRegion)
  146.             manager.stopUpdatingLocation()
  147.            
  148.             //            NSLog("You exited the region")
  149.             //            sendLocalNotificationWithMessage("You exited the region", playSound: true)
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement