iOS:在类

时间:2015-08-05 03:08:34

标签: ios class ibeacon localnotification

我正在制作一个iBeacon扫描程序插件,可用于不同的项目。

在myBeaconScanner.m文件中:

-(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**
}

生成本地通知的方法:

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

}

我知道每次手机进入iBeacon区域时都会调用此方法:

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

如果在didFinishLaunchingWithOptions下我创建了我的班级实例(myBeaconScanner),那么我打电话给sendLocalNotification我收到通知。但是我想确保 A行能够正常工作,这样我就不必从类本身以外的其他地方调用sendLocalNotification方法。

1 个答案:

答案 0 :(得分:0)

有几种解决方案

其中一个是创建一个单例类,它在第一次被调用时被实例化。

@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];

}

导入头文件后,您可以使用

从任何地方调用sendLocalNotification方法
[[NotificationManager sharedNotificationManager] sendLocalNotification:@"Welcome message.."];