0

I am making an iBeacon scanner plugin to use in different projects.

in myBeaconScanner.m file:

-(id)initWithUUID:(NSString *) _uuid{
    self = [super init];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:_uuid;
    NSString *appID = [[NSBundle mainBundle] bundleIdentifier];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:appID];

    self.beaconRegion.notifyEntryStateOnDisplay = YES;
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager requestAlwaysAuthorization];
    [self locationManager:self.locationManager didStartMonitoringForRegion:self.beaconRegion];

    UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];

    NSLog(@"started..");
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
    [self sendLocalNotification:@"Welcome message.."]; **<-------------Line A**
}

Method to generate local notification:

-(void)sendLocalNotification:(NSString *)body{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.alertBody = body;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

}

I know that every time the phone enters an iBeacon region this method is called:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

If under didFinishLaunchingWithOptions I make an instance of my class (myBeaconScanner) then I call sendLocalNotification I get the notification. However I want to make sure Line A works instead so that I don't have to call sendLocalNotification method from elsewhere other than inside the class itself.

Kourosh
  • 608
  • 2
  • 14
  • 34

1 Answers1

0

There are several solutions

One of them is to create a singleton class, which is instantiated once the first time it's called.

@interface NotificationManager : NSObject

+ (NotificationManager *)sharedNotificationManager;
- (void)sendLocalNotification:(NSString *)body;

@end

@implementation NotificationManager

+ (NotificationManager *)sharedNotificationManager
{
  static dispatch_once_t token = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&token, ^{
    _sharedObject = [[NotificationManager alloc] init];
  });
  return _sharedObject;
}

- (void)sendLocalNotification:(NSString *)body
{
  UILocalNotification *localNotification = [[UILocalNotification alloc] init];
  localNotification.alertBody = body;
  localNotification.timeZone = [NSTimeZone defaultTimeZone];
  localNotification.soundName = UILocalNotificationDefaultSoundName;
  [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

}

After importing the header file you can call the sendLocalNotification method from everywhere with

[[NotificationManager sharedNotificationManager] sendLocalNotification:@"Welcome message.."];
vadian
  • 274,689
  • 30
  • 353
  • 361