AppDelegate 的类别 属性 不工作

category property for AppDelegate not working

我已经用我的类别扩展了 AppDelegate。目标是在用户使用已终止的应用程序进入某个地理围栏时启动服务器请求并显示通知。这一切都在 class GeoNotificationManager 中完成。我需要做的就是实例化此 class 一旦应用程序从位置事件启动。

一切正常,除了 问题: - 当 @property (strong) GeoNotificationManager* geof; 在类别内时,GeoNotificationManager 内的代码不起作用(没有服务器请求,没有通知)。当它在 AppDelegate 本身内部时,一切正常

我也试过没有 属性 在 category 中,它也不起作用。

GeoNotificationManager* manager = [GeoNotificationManager new];
[manager sendTestServerRequest:@"test"]; // REQUEST NOT SENDING, WHY???
//AppDelegate+Geofence.h

#import "AppDelegate.h"

@class GeoNotificationManager;

@interface AppDelegate (Geofence)
@property (strong) GeoNotificationManager* geof;
@end
NSString * const kNewPropertyKey = @"kNewPropertyKey";

@class GeoNotificationManager;

@implementation AppDelegate(Geofence)


-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:
(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
     if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey])
     {
         self.geof = [GeoNotificationManager new];
         [self.geof sendTestServerRequest:@"test"];
         
    }
    return true;
}
 // I don't understand what is that below, but seems it's required for having property in category

- (void)setGeof:(id)aObject
{
    objc_setAssociatedObject(self, (__bridge const void * _Nonnull)(kNewPropertyKey),
     aObject, OBJC_ASSOCIATION_ASSIGN);

}
- (id)geof
{
    return objc_getAssociatedObject(self, (__bridge const void * _Nonnull)(kNewPropertyKey));
}
@objc class GeoNotificationManager : NSObject, CLLocationManagerDelegate {
    let locationManager = CLLocationManager()

    override init() {
        super.init()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.showsBackgroundLocationIndicator = true
    }
// OTHER CODE, -didEnterRegion, server request etc

OBJC_ASSOCIATION_ASSIGN 只需替换为 OBJC_ASSOCIATION_RETAIN,就可以了! 但是如果有人知道如何用局部变量做到这一点,请分享