gpt4 book ai didi

ios - 委托(delegate)使用 CLLocationManagerDelegate 不工作 objective-c

转载 作者:行者123 更新时间:2023-11-28 21:53:28 25 4
gpt4 key购买 nike

当我在 CLLocationManagerDelegate 中调用委托(delegate)方法时,我使用 CLLocationManagerDelegate 创建了一个自定义委托(delegate),委托(delegate)无法正常工作。我认为我创建委托(delegate)的方式是错误的,有人可以告诉我我做错了什么吗?

.h file

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>
@protocol iBeaconDeligate<NSObject>
@required
-(void)getArray:(NSArray *)beaconArray;
@end

@interface iBeaconDeligate : NSObject <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property CLProximity lastProximity;

-(void) initiBeaconDeligate;

@end

这是 .m file

#import "iBeaconDeligate.h"

@implementation iBeaconDeligate


-(void) initiBeaconDeligate
{

NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:
@"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
NSString *beaconIdentifier = @"iBeaconModules.us";
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:
beaconUUID identifier:beaconIdentifier];

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

//[self.locationManager requestWhenInUseAuthorization];
//[self.locationManager requestAlwaysAuthorization];
// New iOS 8 request for Always Authorization, required for iBeacons to work!
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;

[self.locationManager startMonitoringForRegion:beaconRegion];
[self.locationManager startRangingBeaconsInRegion:beaconRegion];
[self.locationManager startUpdatingLocation];
}

-(void)sendLocalNotificationWithMessage:(NSString*)message {

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
notification.alertBody = message;
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:
(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
NSString *message = @"";

if(beacons.count > 0) {
CLBeacon *nearestBeacon = beacons.firstObject;



if(nearestBeacon.proximity == self.lastProximity ||
nearestBeacon.proximity == CLProximityUnknown) {
return;
}

self.lastProximity = nearestBeacon.proximity;



switch(nearestBeacon.proximity) {
case CLProximityFar:
message = @"You are far away from the beacon";
break;
case CLProximityNear:
message = @"You are near the beacon";
break;
case CLProximityImmediate:
message = @"You are in the immediate proximity of the beacon";
break;
case CLProximityUnknown:
return;
}
} else {
message = @"No beacons are nearby";
}


[self performSelector:@selector(getArray:) withObject:beacons];
NSLog(@"%@", message);
[self sendLocalNotificationWithMessage:message];


}

-(void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region {
[manager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
[self.locationManager startUpdatingLocation];

NSLog(@"You entered the region.");
[self sendLocalNotificationWithMessage:@"You entered the region."];
}

-(void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region {
[manager stopRangingBeaconsInRegion:(CLBeaconRegion*)region];
[self.locationManager stopUpdatingLocation];

NSLog(@"You exited the region.");
[self sendLocalNotificationWithMessage:@"You exited the region."];
}

@end

这就是我在我的 Controller 类中调用它的方式

.h @interface HomeViewController : UIViewController<iBeaconDeligate>

.m

iBeaconDeligate *sampleProtocol = [[iBeaconDeligate alloc]init];

[sampleProtocol initiBeaconDeligate];

问题是在 CLLocationManagerDelegate 中自动调用的方法没有调用我的自定义委托(delegate)

最佳答案

由于sampleProtocol是方法内部的一个局部变量,当方法退出时它会被释放。

您应该创建一个(strong) @property 来存储这个对象以确保它被保留。

关于ios - 委托(delegate)使用 CLLocationManagerDelegate 不工作 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27395051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com