-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cocoa Touch #51
Comments
UINavigationController
|
NSArray1. 集合遍历
2. 如何给一个自定义类实现下标访问方式 |
View Controller 的生命周期 |
UIView 的生命周期1.
|
Child View Controller / Custom Container View Controller1. 什么是 Child View Controller2. 如何添加/移除一个 Child View Controller// add child view
UIViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
[self addChildViewController:controller];
controller.view.frame = CGRectMake(0, 44, 320, 320);
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
// remove child view
UIViewController *vc = [self.childViewControllers lastObject];
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController]; 3. Child View Controller 的视图状态管理默认情况下,当添加 Child View Controller 到 Container View Controller 中时,Container 会自动将 如果需要自己管理 Child 的视图状态,我们可以重写 Container 的 4. Child View Controller 与 UINavigationController 的关系默认情况下,当添加 Child View Controller 到 Container View Controller 中时,Container 会自动将自己的 navigation controller 传递给 Child。 5. Child View Controller 之间的切换我们可以自己定义 Child View Controller (view)之间的切换效果。 6. 一些建议
7. Container 可以适当把一些控制权交给 Child
参考:
|
NSNotification & NSNotificationCenter两种注册通知观察者的方式:
注意点
...
@property (nonatomic, weak) id <NSObject> observer;
...
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
__weak typeof(self) weakSelf = self;
self.observer = [center addObserverForName:@"TestNotification"
object:nil
queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
// 为什么这个 block 中必须要用 weakSelf ?
// observer 会引用这个 block,这个 block 会 copy self,对 self 强引用,而 observer 要等到被 remove 掉时,才会被移除,所以如果等到在 dealloc 时才 remove observer 的话,就会出现循环引用。
NSLog(@"%@ did receive notification %@", NSStringFromClass(weakSelf.class), note.name);
}];
7.1 根据官方文档,在多线程应用中,Notification在哪个线程中post,就在哪个线程中被转发,但不一定是在注册观察者的那个线程中。 7.2 如果我们的Notification是在主线程中post的,如何能在后台线程中对这个Notification进行处理呢?或者换个提法,如果我们希望一个Notification的post线程与转发线程不是同一个线程,应该怎么办呢?
也就是先在默认的线程中捕捉到这些通知,然后再将它们重定向到合适的线程。 大概的实现思路是: 我们仍然是像平常一样去注册一个通知的观察者,当Notification来了时,先看看post这个Notification的线程是不是我们所期望的线程,如果不是,则将这个Notification存储到我们的队列中,并通过 Mach Port 发送一个信号(signal)到期望的线程(其中会有 Runloop 监听到信号)中,来告诉这个线程需要处理一个Notification。指定的线程在收到信号后,将Notification从队列中移除,并进行处理(具体的实现代码见官方文档)。
另外,YYKit 中的 NSNotificationCenter+YYAdd.h 提供了直接在主线程发通知的便捷方法,也就是保证通知的收发都在主线程,这种方式可能是实际开发中遇到的更多的情况。 7.3 线程安全 苹果之所以采取通知中心在同一个线程中post和转发同一消息这一策略,应该是出于线程安全的角度来考量的。官方文档告诉我们,NSNotificationCenter是一个线程安全类,我们可以在多线程环境下使用同一个NSNotificationCenter对象而不需要加锁。我们可以在任何线程中添加/删除通知的观察者,也可以在任何线程中post一个通知。 NSNotificationCenter是线程安全的,但并不意味着在多线程环境中不需要关注线程安全问题。不恰当的使用仍然会引发线程问题。
每次添加一个观察者时,会将这个观察者的引用添加到它的分发表中。 另外,通知的发送与处理是同步的,在某个地方post一个消息时,会等到所有观察者对象执行完处理操作后,才回到post的地方,继续执行后面的代码。 参考: |
推送通知(Notification)1. 本地推送2. 远程推送2.1 推送原理iOS 系统的推送(APNS,即 Apple Push Notification Service)依托一个或几个系统常驻进程运作,是全局的(接管所有应用的消息推送),所以可看作是独立于应用之外,而且是设备和苹果服务器之间的通讯,而非应用的提供商服务器。 所以,iOS 的推送,可以不严谨的理解为:
首先,APNs会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书以确定其为苹果设备); 其实,APNs 在分别与设备和 provider 建立连接时都会进行认证:
而且这两个认证环节都有两种级别的认证方式:
这里以 APNs-to-device connection trust 为例,简单说一下两种不同级别的认证: TLS链接一旦建立,在没有数据的情况下,只需要每隔15分钟进行一次保活的握手,因此几乎不占流量。而一旦因为意外原因导致链接中断,iPhone会不断重新尝试建立TLS链接,直到成功。 (2)设备令牌认证 设备令牌是怎么生成的呢?是每次建立TLS连接时,APNS通过前一层次(TLS层)里我们提到的每台正常的iPhone唯一的设备证书(unique device certificate),并用令牌密钥(token key)加密生成的。 在令牌生成了之后,APNS会把设备令牌(device token)返回给iPhone,而对应的Push应用程序(如BeejiveIM),则把返回来的设备令牌(device token)直接发送给Provider(如BeejiveIM服务器)。这样,当Provider有Push消息要发送时,就会把对应帐号的设备令牌(device token)和消息一起发送给APNS,而APNS再依据设备令牌(device token),找到相应TLS链接的iPhone,并发送相应的Push消息。 2.2 推送流程:(1)App 启动时调用 注:APNS 判断 Push 推送消息该发给哪台 iPhone 是由 deviceToken 决定的。这里的 deviceToken 是唯一的,只在以下三种情况下会发生改变:
3. 静默推送(Background Modes - Remote notification):应用在后台运行的情况下,收到推送时可以在后台提前下载数据参考 |
后台模式
参考 |
UITextField1. 如何控制 UITextField 中文本的缩进?
参考: |
如何设计一个自定义 view?
延伸阅读 |
理解 UIScrollView 的原理参考 |
震动反馈:UIFeedbackGenerator 和 Taptic Engine参考 |
保存和恢复状态(Preserving Your App's UI Across Launches)延伸阅读 |
参考:
The text was updated successfully, but these errors were encountered: