-
-
Save JoeyBodnar/a0b06e306ada899ff2aca318360a6940 to your computer and use it in GitHub Desktop.
NearBee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is all nearbee related code in the app | |
// info.plist | |
// co.nearbee.api_key: abcd... | |
// co.nearbee.organization_id: 1234 | |
// AppDelegate.swift | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
let localCatergory = UNNotificationCategory(identifier: "nearbyNotificationView", actions: [], intentIdentifiers: [], options: []) | |
UNUserNotificationCenter.current().setNotificationCategories([localCatergory]) | |
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { | |
(granted, error) in | |
} | |
FBSDKApplicationDelegate.sharedInstance()?.application(application, didFinishLaunchingWithOptions: launchOptions) | |
return true | |
} | |
// ViewController.swift | |
import UIKit | |
import NearBee | |
import CoreLocation | |
import CoreBluetooth | |
import UserNotifications | |
import WebKit | |
import SafariServices | |
import CoreData | |
// the view controller with the items to be revealed when the hamburger menu is pressed | |
class ViewController: UIViewController { | |
let tableView = UITableView() | |
let options = ["About", "FAQs", "Special Offers", "Logout"] | |
let locationManager = CLLocationManager() | |
weak var delegate: RevealViewDelegate? | |
var currentLocation = CLLocation() | |
var nearBee: NearBee! | |
override func loadView() { | |
super.loadView() | |
layout() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
locationManager.requestAlwaysAuthorization() | |
UNUserNotificationCenter.current().delegate = self | |
nearBee = NearBee.initNearBee() | |
nearBee.delegate = self | |
nearBee.enableBackgroundNotification(true) | |
nearBee.startScanning() | |
UserDefaults.standard.setValue(true, forKey: "NearBeeToggleSound") | |
if let url = Bundle.main.url(forResource: "notification", withExtension: "mp3") { | |
nearBee?.notificationSound = UNNotificationSoundName(rawValue: url.lastPathComponent) | |
} | |
setupViews() | |
setupLocationManager() | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
nearBee.delegate = self | |
} | |
func setupLocationManager() { | |
locationManager.delegate = self | |
locationManager.requestAlwaysAuthorization() | |
locationManager.desiredAccuracy = 100 | |
locationManager.startUpdatingLocation() | |
} | |
func handleBluetoothPoweredOff() { | |
if Helpers.shouldShowBluetoothTurnedOffAlert() { | |
self.showAlert(withTitle: "Bluetooth", andMessage: "Your bluetooth is currently turned off. To enable restaurant discovery, please turn on bluetooth in your iOS Control Center.") { | |
Helpers.setHasShownBluetoothTurnedOffAlert(to: true) | |
} | |
} | |
} | |
} | |
extension ViewController: UNUserNotificationCenterDelegate { | |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
guard let authToken = Helpers.currentAuthorizationToken() else { return } | |
let isNearBeeNotification = nearBee.checkAndProcessNotification(response.notification, queryParameters: ["authToken" : authToken]) | |
BeaconCacheManager.cacheBeacon(from: response) | |
if (isNearBeeNotification) { | |
completionHandler() | |
} | |
} | |
} | |
extension ViewController: UITableViewDelegate { | |
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
return 50 | |
} | |
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
print(view.safeAreaInsets.top) | |
return 87 | |
} | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
self.delegate?.didSelectItem(at: indexPath) | |
} | |
} | |
extension ViewController: UITableViewDataSource { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return options.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
if let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SideMenuCell, for: indexPath) as? SideMenuCell { | |
cell.setup(with: options[indexPath.row]) | |
return cell | |
} | |
return UITableViewCell() | |
} | |
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
if let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: HeaderIdentifiers.SideMenuHeader) as? SideMenuHeader { | |
header.nameLabel.text = User.currentUser()?.name ?? "" | |
return header | |
} | |
return UIView() | |
} | |
} | |
extension ViewController: NearBeeDelegate { | |
func didUpdateState(_ state: NearBeeState) { | |
} | |
func didLoseBeacons(_ beacons: [NearBeeBeacon]) { | |
print("beacons updated here \(beacons)") | |
} | |
func didUpdateBeacons(_ beacons: [NearBeeBeacon]) { | |
print("beacons lost here \(beacons)") | |
} | |
func didFindBeacons(_ beacons: [NearBeeBeacon]) { | |
} | |
func didThrowError(_ error: Error) { | |
} | |
} | |
extension ViewController: CLLocationManagerDelegate { | |
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { | |
if status == CLAuthorizationStatus.authorizedAlways || status == .authorizedWhenInUse { | |
locationManager.startUpdatingLocation() | |
} else if status == .denied { | |
self.showErrorAlert(with: "You have turned off location monitoring. Go to Privacy -> Location Services to enable location updates.") | |
} else if status == .notDetermined { | |
// this is the default state before ever asking for permission | |
} | |
} | |
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
if let first = locations.first { | |
if currentLocation.horizontalAccuracy < 100.0 { | |
self.currentLocation = first | |
Helpers.setLastKnownUserLocation(to: currentLocation) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment