如何解决AppDelegate的类别属性不起作用
我已将AppDelegate扩展为我的类别。当用户使用终止的应用程序输入某些地理围栏时,目标是启动服务器请求并显示通知。这些都在GeoNotificationManager类中完成。我需要做的-从位置事件启动应用后,实例化此类。
一切正常,但问题除外:-当GeoNotificationManager
位于类别内时,GeoNotificationManager* manager = [GeoNotificationManager new];
[manager sendTestServerRequest:@"test"]; // REQUEST NOT SENDING,WHY???
内部的代码不起作用(无服务器请求,没有通知)。而且,当它位于AppDelegate自身内部时,一切正常即可正常工作。
我也尝试过在类别中不带属性,这也行不通。
//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
{ "RedirectUrl": "www.youtube.com"}
解决方法
OBJC_ASSOCIATION_ASSIGN仅应替换为OBJC_ASSOCIATION_RETAIN,它可以工作! 但是,如果有人知道如何使用局部变量执行此操作,请分享
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。