简体   繁体   English

[ios]从框架或自定义静态库向目标应用程序添加观察者

[英][ios]add observer to Target Application from the Framework or custom static library

I have developed an ios lib/framework where i record user login and connect to the server and fetch data from server real time. 我开发了一个ios lib/framework ,其中记录用户登录并连接到服务器并从服务器实时获取数据。 In the same library/framework I would like to add observer to my application so that I come to know when the app went in background or became active again. 在同一个library/framework我想向应用程序中添加观察者,以便我知道该应用程序何时在后台运行或再次激活。

Something similar to what we usually do in the application code itself....(my test application code below. An observer functionality that i want to move to the common lib/framework ) 类似于我们通常在应用程序代码本身中执行的操作。...(下面是我的测试应用程序代码。我要移至通用的lib/framework的观察器功能)

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyAppWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];

But even though i have defined the method as 但是即使我将方法定义为

void MyAppWillResignActive(id self, SEL _cmd, id application) {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

Pay attention to the signature of the method to be invoked in your selector MyAppWillResignActive . 注意选择器MyAppWillResignActive要调用的方法的签名。

EDIT: Moved registration to library initialization as suggested in iosDeveloper09 comment. 编辑: 按照iosDeveloper09注释中的建议将注册移至库初始化。


Class level 班级

  • Register during initialize initialize期间注册

     + (void)initialize { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil]; } 
  • Class handler + 类处理程序+

     + (void)applicationWillResignActive:(NSNotification *)notification 

Instance level 实例级别

  • Register during init init期间注册

     - (instancetype)init { self = [super init]; if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil]; } return self; } 
  • Instance handler - 实例处理程序-

     - (void)applicationWillResignActive:(NSNotification *)notification 

Host 主办

@property (nonatomic, retain) SO_41526719Library * lib;

// Keep an instance around
self.lib = [[SO_41526719Library alloc] init];

At runtime 在运行时

applicationWillResignActive NSConcreteNotification 0x6000000545e0 {name = UIApplicationWillResignActiveNotification; applicationWillResignActive NSConcreteNotification 0x6000000545e0 {name = UIApplicationWillResignActiveNotification; object = } 对象=}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM