【问题标题】:couldn't call the user defined method in objective c无法在目标 c 中调用用户定义的方法
【发布时间】:2018-07-01 07:01:09
【问题描述】:

我正在使用核心位置框架。我正在用户定义的方法中检查授权状态 (kCLAuthorizationStatusDenied/kCLAuthorizationStatusRestricted)。
值得注意的一点是,在正常执行流程的情况下会调用此方法(没有问题)。

但如果用户退出/终止应用并再次重新启动它(通过点击应用图标)。在这种情况下,将调用 before 和 after 方法,只有 this(Authorization()) 方法不会调用。 为什么会发生这种突然的行为? 谁能告诉我Objective C中的执行流程?

在 .h 文件中 -

@property (strong, nonatomic) LocationTracker * locationTracker;

在 .m 文件中 -

    [self userTrialMethodBefore];
      [self.locationTracker startLocationTracking];
      [self userTrialMethodAfter];

- (void)userTrialMethodBefore
        { 
           NSLog(@"This method gets called before ");

         }


        - (void)startLocationTracking
        { 
            NSString *responeParam = @"";
            if ([CLLocationManager locationServicesEnabled] == NO)
            {
                responeParam = @"Disabled location";
            }
            else
            {

                CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];


                if(authorizationStatus == kCLAuthorizationStatusDenied || authorizationStatus == kCLAuthorizationStatusRestricted)
                {
                    responeParam = @"Unauthorize location";

                }
                else
                {
                    responeParam = @"Authorize location";


                    CLLocationManager *locationManager = [LocationTracker sharedLocationManager];
                    locationManager.delegate = self;
                    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
                    locationManager.distanceFilter = kCLDistanceFilterNone;
                    if (SystemVersion > 9.2)
                    {

                        [locationManager setAllowsBackgroundLocationUpdates:YES];
                        locationManager.pausesLocationUpdatesAutomatically = false;
                    }

                    [locationManager startUpdatingLocation];
                }
            }
        }

- (void)userTrialMethodAfter
        { 
           NSLog(@"This method gets called after ");

         }

[注意:我使用的是 Xcode 9.2,iOS 11.2.2 设备,objective c 语言]

【问题讨论】:

  • 向我们展示您的代码、您如何检查授权、您希望被调用的内容以及 ecc...
  • @Shebuka 嘿,谢谢您的回复。代码刚刚添加。看看吧。

标签: ios objective-c iphone core-location cllocation


【解决方案1】:

当您分配CLLocationManager 时,您还需要像这样调用requestWhenInUseAuthorization

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

以及为什么您不使用self.locationTrackerstartUpdatingLocation 而是检索sharedLocationManager

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多