很难在测试手机上运行地理围栏-IOS
这是我每年在这里的第一篇帖子。呜呼 这是我的故事。。。我花了大约一周的时间尝试IOS中的地理围栏教程。问题是我现在必须在Objective-C中完成 我正试图让一个小应用程序来运行这个地理围栏,但我一直有实际触发事件的问题 我正在我的实际手机(iPhone7plus)上测试它,我无法让一些事件显示在屏幕上,无论我在该地区还是在外地 我将粘贴.h和.m文件中的代码。到目前为止,我只实施了这些措施很难在测试手机上运行地理围栏-IOS,ios,Ios,这是我每年在这里的第一篇帖子。呜呼 这是我的故事。。。我花了大约一周的时间尝试IOS中的地理围栏教程。问题是我现在必须在Objective-C中完成 我正试图让一个小应用程序来运行这个地理围栏,但我一直有实际触发事件的问题 我正在我的实际手机(iPhone7plus)上测试它,我无法让一些事件显示在屏幕上,无论我在该地区还是在外地 我将粘贴.h和.m文件中的代码。到目前为止,我只实施了这些措施 #import <UIKit/UIKit.h> #import <CoreLocat
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *myLat;
@property (weak, nonatomic) IBOutlet UILabel *myLon;
@property (weak, nonatomic) IBOutlet UILabel *destLat;
@property (weak, nonatomic) IBOutlet UILabel *destLon;
@property (weak, nonatomic) IBOutlet UILabel *diff;
@property (weak, nonatomic) IBOutlet UILabel *answer;
@property BOOL alwaysOn;
@property BOOL isCLickOn;
@property (retain, nonatomic) CLLocationManager *locationManager;
@end
发生的情况是,即使应用程序处于打开状态,我的手机上也不会显示任何事件。因此,我在故事板上有一个视图控制器,它应该显示我的位置和目标位置,并显示两个对象之间的距离。但即使我步行400米,然后走回中心,它也不会显示出来
我知道Swift是我应该使用的,我会改用Swift,但现在我真的很想了解地理围栏的过程
任何帮助都将不胜感激。我在编程方面也很新,所以我仍在努力学习和理解Objective-C
多谢各位 大家好,欢迎来到Stack Overflow,请阅读如何创建和检查,以增加获得反馈和有用答案的机会。谢谢。我试试看。我仍在努力适应这一点。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidAppear:(BOOL)animated{
// [self showAlertMessage:@"Test" message:@"Maybe works"];
// [self showAlertMessage:@"Test" message:@"Maybe works"];
[self setupTheAuthorizations];
if(!_isCLickOn){
[self startTheRegionMonitoring];
}
// _answer.text = @"Merge";
}
- (void) setupTheAuthorizations {
_locationManager = [[CLLocationManager alloc] init];
[_locationManager requestAlwaysAuthorization];
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
//check the type of authorization
CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
switch (authStatus) {
case kCLAuthorizationStatusAuthorizedAlways:
// should do somethinglike getting the things started and stuff like that ...
_alwaysOn = YES;
break;
case kCLAuthorizationStatusNotDetermined:
[_locationManager requestAlwaysAuthorization];
default:
break;
}
}
- (void) startTheRegionMonitoring {
if (_alwaysOn && [CLLocationManager isMonitoringAvailableForClass:[CLRegion class]]){
CLLocationDegrees latitude = 37.972004;
CLLocationDegrees longitude = -121.293740;
CLLocationDistance radius = 200;
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(latitude, longitude);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"CH"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
_destLat.text = [NSString stringWithFormat:@"%f", latitude];
_destLon.text = [NSString stringWithFormat:@"%f", longitude];
_myLat.text = [NSString stringWithFormat:@"%f", _locationManager.location.coordinate.latitude];
_myLon.text = [NSString stringWithFormat:@"%f", _locationManager.location.coordinate.longitude];
[_locationManager startMonitoringForRegion:region];
}
_isCLickOn = YES;
}
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
}
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region{
NSLog(@"entered region");
if ([region.identifier isEqualToString:@"CH"]){
_answer.text = @"IN";
CLLocation *me = [[CLLocation alloc] initWithLatitude:_locationManager.location.coordinate.latitude
longitude:_locationManager.location.coordinate.longitude];
CLLocation *dest = [[CLLocation alloc] initWithLatitude:37.972004
longitude:-121.293740];
int dist = [me distanceFromLocation:dest];
_diff.text = [NSString stringWithFormat:@"%i", dist];
}
}
- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region{
NSLog(@"entered region");
if ([region.identifier isEqualToString:@"CH"]) {
_answer.text = @"OUT";
CLLocation *me = [[CLLocation alloc] initWithLatitude:_locationManager.location.coordinate.latitude
longitude:_locationManager.location.coordinate.longitude];
CLLocation *dest = [[CLLocation alloc] initWithLatitude:37.972004
longitude:-121.293740];
int dist = [me distanceFromLocation:dest];
_diff.text = [NSString stringWithFormat:@"%i", dist];
}
}
- (void)locationManager:(CLLocationManager *)manager
didStartMonitoringForRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_8,__IPHONE_5_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED{
NSLog(@"Monitoring started");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"error");
}
- (void) showAlertMessage:(NSString *)title message:(NSString *)message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end