Skip to content

Instantly share code, notes, and snippets.

@moveo123
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save moveo123/327629a53779eb76e254 to your computer and use it in GitHub Desktop.

Select an option

Save moveo123/327629a53779eb76e254 to your computer and use it in GitHub Desktop.
Mewseum Quest Estimote Analytics integration code
/*
This is the code to fetch nearest beacon and pass to DescriptionViewController.
*/
- (void)beaconManager:(id)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
if(beacons.count!=0){
self.beacon = [beacons firstObject];
NSLog(@"UUID - %@",self.beacon.proximityUUID.UUIDString);
NSLog(@"%@",[NSString stringWithFormat:@"Major: %@, Minor: %@", self.beacon.major, self.beacon.minor]);
[self.beaconManager stopRangingBeaconsInRegion:self.region];
DescriptionViewController *descriptionViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DescriptionViewController"];
descriptionViewController.mewseum = _mewseum;
descriptionViewController.beacon = self.beacon;
[self.navigationController pushViewController:descriptionViewController animated:YES];
}
}
/*
Estimote analytics code
*/
//
// AppDelegate.m
// Mewseum Quest
//
// Created by Manish Dudharejia on 08/04/15.
// Copyright (c) 2015 Manish Dudharejia. All rights reserved.
//
#import "AppDelegate.h"
#import <Parse/Parse.h>
#import <GooglePlus/GooglePlus.h>
#import "Common.h"
#import <EstimoteSDK/EstimoteSDK.h>
#import "Mewseum.h"
#import "DescriptionViewController.h"
@interface AppDelegate () <CBPeripheralManagerDelegate>
@property (strong, nonatomic) CBPeripheralManager *bluetoothManager;
@end
@implementation AppDelegate
#define APPLICATION_ID @"ChfXbGbFWQpTcxDafN4sNMzFPjyTqMTC4hy98zEe"
#define CLIENT_ID @"R1f9PHuY92jsqtYnm8U5hgFekINtqh22F0TWOUu8"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Parse setApplicationId:APPLICATION_ID clientKey:CLIENT_ID];
[self detectBluetooth];
[ESTCloudManager setupAppID:@"mewseum-quest" andAppToken:@"23d6902205bbf8bfbbc5fc2894f43389"];
[ESTCloudManager enableAnalytics:YES];
[ESTCloudManager isAuthorized];
[self registerForRemoteNotifications:application];
return YES;
}
- (void)registerForRemoteNotifications:(UIApplication*)application{
if([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound)];
}
}
- (void)detectBluetooth{
if(!self.bluetoothManager){
self.bluetoothManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
}
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
NSString *stateString = nil;
switch (self.bluetoothManager.state) {
case CBPeripheralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
case CBPeripheralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
case CBPeripheralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
case CBPeripheralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
case CBPeripheralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
default: stateString = @"State unknown, update imminent."; break;
}
NSLog(@"%@",stateString);
}
//- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
// NSString *stateString = nil;
// switch(self.bluetoothManager.state){
// case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
// case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
// case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
// case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
// case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
// default: stateString = @"State unknown, update imminent."; break;
// }
// //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth state" message:stateString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// //[alert show];
//}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *strDevicetoken = [[NSString alloc]initWithFormat:@"%@",[[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""]];
NSLog(@"devicetoken = %@",strDevicetoken);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Error %@",error.localizedDescription);
}
#ifdef isAtLeastiOS8
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler{
if ([identifier isEqualToString:@"declineAction"]){
} else if ([identifier isEqualToString:@"answerAction"]){
}
}
#endif
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
/*
NSString *beaconId = userInfo[@"beaconUUID"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
DescriptionViewController *descriptionViewController = [storyboard instantiateViewControllerWithIdentifier:@"DescriptionViewController"];
descriptionViewController.beaconId = beaconId;
[navController.visibleViewController.navigationController pushViewController:descriptionViewController animated:YES];
*/
}
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"Hree");
}
- (void)applicationWillResignActive:(UIApplication *)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.
}
- (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.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication
annotation: (id)annotation {
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
@end
/*
This code is to receive notification when app enters in beacon's region.
*/
//
// DescriptionViewController.m
// MewseumQuest
//
// Created by Manish Dudharejia on 10/04/15.
// Copyright (c) 2015 Manish Dudharejia. All rights reserved.
//
#import "DescriptionViewController.h"
#import "QuizViewController.h"
#import <Parse/Parse.h>
#import "Loader.h"
#import "Messages.h"
#import "Store.h"
#import "Helper.h"
#import "LevelView.h"
@interface DescriptionViewController () <ESTBeaconManagerDelegate,CLLocationManagerDelegate,LevelViewDelegate>
@property (strong, nonatomic) IBOutlet UILabel *lblTitle;
@property (strong, nonatomic) IBOutlet UITextView *txtDescription;
@property (strong, nonatomic) LevelView *levelView;
@property (nonatomic, strong) ESTBeaconManager *beaconManager;
@property (nonatomic, strong) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) Store *store;
@end
@implementation DescriptionViewController
#pragma mark - View life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self commonInit];
[self getDescriptionForBeacon];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Common Init
- (void)commonInit{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
for (CLRegion *r in locationManager.monitoredRegions) {
[locationManager stopMonitoringForRegion:r];
}
self.beaconManager = [ESTBeaconManager new];
self.beaconManager.delegate = self;
[self.beaconManager requestAlwaysAuthorization];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:self.beacon.proximityUUID
major:[self.beacon.major unsignedIntValue]
minor:[self.beacon.minor unsignedIntValue]
identifier:@"MintRegionIdentifier"];
self.beaconRegion.notifyOnEntry = YES;
self.beaconRegion.notifyOnExit = YES;
self.beaconRegion.notifyEntryStateOnDisplay = YES;
[self.beaconManager startMonitoringForRegion:self.beaconRegion];
_levelView = [[NSBundle mainBundle] loadNibNamed:@"LevelView" owner:nil options:nil][0];
_levelView.delegate = self;
}
#pragma mark - Button Click Event
- (IBAction)btnPlayTapped:(id)sender{
[_levelView viewWithTag:101].alpha = 0.0;
[self.view addSubview:_levelView];
_levelView.frame = self.view.bounds;
[UIView animateWithDuration:0.3 animations:^{
[_levelView viewWithTag:101].alpha = 1.0;
}];
}
#pragma mark - Level View Delegate
- (void)levelSelected:(NSInteger)levelType{
QuizViewController *quizViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"QuizViewController"];
quizViewController.levelType = (DifficultyLevel)levelType;
quizViewController.mewseum = _mewseum;
quizViewController.store = _store;
[self.navigationController pushViewController:quizViewController animated:YES];
}
- (void)getDescriptionForBeacon{
[[Loader defaultLoader] displayLoadingView:msgLoading];
PFQuery *query = [PFQuery queryWithClassName:@"store"];
[query whereKey:@"beaconenid" containsString:self.beacon.proximityUUID.UUIDString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
PFObject *obj = [objects lastObject];
if(obj){
_store = [Store dataWithInfo:obj];
_lblTitle.text = _store.storeTitle;
_txtDescription.text = _store.storeDescription;
}
[[Loader defaultLoader] hideLoadingView];
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
[Helper showAlertView:titleAlert withMessage:error.localizedDescription delegate:nil];
[[Loader defaultLoader] hideLoadingView];
}
}];
}
#pragma mark - ESTBeaconManager delegate
- (void)beaconManager:(id)manager didEnterRegion:(CLBeaconRegion *)region{
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = @"Enter region notification";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
- (void)beaconManager:(id)manager didExitRegion:(CLBeaconRegion *)region{
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = @"Exit region notification";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment