问题描述
开发者被告知UIWebView
在2020年12底将被弃用,应用商店此后不再接受含有WebView
的应用。
///("No longer supported; please adopt WKWebView., ios(2.0, 12.0))
@interface UIWebView : UIView
@end
解决方案
- 利用IDE提供的快捷键全局搜索
- 利用runtime提供的方法查询(讲解点)
- 编写脚本代码查询
方案描述
1. 查询UIWebViewDelegate
查询工程中的有那些Class实现了UIWebViewDelegate协议。
static NSArray *LMLiveClassesConformingToProtocol(Protocol *protocol){
NSMutableArray *conformingClasses = [NSMutableArray new];
Class *classes = NULL;
//获取所有已经注册过的Class的总个数
int numClasses = objc_getClassList(NULL, 0);
if (numClasses > 0 ) {
classes = (Class *)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
for (int index = 0; index < numClasses; index++) {
Class nextClass = classes[index];
//校验是否实现了某个协议
if (class_conformsToProtocol(nextClass, protocol)) {
[conformingClasses addObject:nextClass];
}
}
free(classes);
}
return conformingClasses;
}
2.查询 objc_property_t
与ivar
static NSArray *LMLiveClassAllobjc_propertys(Class className){
/// objc_property_t的操作目前只提供
/// property_getName(objc_property_t _Nonnull property)
/// property_getAttributes(objc_property_t _Nonnull property)
NSMutableArray *allPropertys = [NSMutableArray new];
unsigned int propertyNumber = 0;
objc_property_t *propertys = class_copyPropertyList(className, &propertyNumber);
for (int i = 0; i < propertyNumber; i++) {
objc_property_t property_t = propertys[i];
QMUIPropertyDescriptor *propertyDescriptor = [QMUIPropertyDescriptor descriptorWithProperty:property_t];
[allPropertys addObject:propertyDescriptor];
}
return allPropertys;
}
static NSArray *LMLiveClassAllIvas(Class className){
/// ivar的操作目前提供
/// ivar_getName(Ivar _Nonnull v)
/// ivar_getTypeEncoding(Ivar _Nonnull v)
NSMutableArray *allIvas = [NSMutableArray new];
unsigned int allIvarsNumber = 0;
Ivar *ivars = class_copyIvarList(className, &allIvarsNumber);
for (int i = 0; i < allIvarsNumber; i++) {
Ivar ivar = ivars[i];
QMUIPropertyDescriptor *propertyDescriptor = [QMUIPropertyDescriptor descriptorWithIvar:ivar];
[allIvas addObject:propertyDescriptor];
}
return allIvas;
}
注意问题
但是通过上述步骤1、2只能解决一部分问题。如下:
- Categoty
@interface UIWebView (AFNetworking)
...
@end
@implementation UIWebView (AFNetworking)
...
@end
- 无
WebViewDelegate
与objc_property_t
、Ivar
@interface DoraemonDefaultWebViewController ()
@end
@implementation DoraemonDefaultWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = DoraemonLocalizedString(@"Doraemon内置浏览器");
UIWebView * view = [[UIWebView alloc] initWithFrame:self.view.frame];
[view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.h5Url]]];
[self.view addSubview:view];
}
@end
在DoraemonDefaultWebViewController
中没有设置UIWebViewDelegate
相关属性,也无property
关键字声明的属性,也无_变量
、_is变量
。因此class_conformsToProtocol
、class_copyPropertyList
与class_copyIvarList
操作是获取不到UIWebView
相关的属性的。
其实从class_操作
方法名称也可知道是无法获取的。
3.objc_setAssociatedObject
动态关联的属性
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
id _Nullable value, objc_AssociationPolicy policy)
使用objc_setAssociatedObject产生的副作用如下:
1.首先校验要设置的newValue是否为空,不为空执行第2步,否则执行第5步
2.
AssociationsManager
会维护一个全局hashMap
,根据object的地址转换成相应的key,并在hashMap
查询这个key,结果记为refs
。如果refs
不为空执行第3步,为空执行第4步。-
- 在
refs
中以key查找oldValue
,如果oldValue
不为空更换为新值newValue
,oldValue
为空则以key为键值,将newValue
存储起来。
- 在
-
- 生成新的
ObjectAssociationMap refs
,并将newValue
存储到refs
中。最后根据object的地址转换成相应的key,将新生成的refs
插入到全局的hashMap
中。
- 生成新的
5.在全局
hashMap
中查找object
对应的refs
,并在refs
中将key对应的属性销毁。
从上述步骤来看,这个全局hashMap
内部存储的子map是动态可变的(objc_setAssociatedObject方法的执行),我们必须要让当前对象执行objc_setAssociatedObject
才能在hashMap中获取到,对于一个庞大的工程(依赖众多)来说,这种操作是不可取的。
4.objc_registerClassPair
(懂的不多)
subclass = objc_allocateClassPair(baseClass, subclassName, 0);
objc_registerClassPair(subclass);
其他 objc_setAssociatedObject
与objc_getAssociatedObject
源码
void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {
// retain the new value (if any) outside the lock.
//声明一个存在oldValue的变量以便后续value为空时或者更换value时更换
ObjcAssociation old_association(0, nil);
//判断new_value是否为空,copy与retain方法的执行
id new_value = value ? acquireValue(value, policy) : nil;
{
//一个全局变量,维护一个map,key为object变量的地址某种变形DISGUISE(object),value为一个refs,refs存储是为object绑定set_associative的值
AssociationsManager manager;
//拿到HashMap,里面为懒加载
AssociationsHashMap &associations(manager.associations());
// 获取object的变形key
disguised_ptr_t disguised_object = DISGUISE(object);
//校验插入新的newVale是否为空
if (new_value) {
// break any existing association.
//查询object对应的disguised_object是否在associations存在
AssociationsHashMap::iterator i = associations.find(disguised_object);
//在associations查询到object对应的表
if (i != associations.end()) {
// secondary table exists
//取出object对应的表refs
ObjectAssociationMap *refs = i->second;
//在refs map查找key是否存在
ObjectAssociationMap::iterator j = refs->find(key);
//查找到key对应的j
if (j != refs->end()) {
//旧值赋给old_association
old_association = j->second;
//更新新值
j->second = ObjcAssociation(policy, new_value);
} else {
//之前没有存储过key对应的value,直接插入map中
(*refs)[key] = ObjcAssociation(policy, new_value);
}
} else {
// create the new association (first time).
//在associations没有找到,从新构建一个refs,以disguised_object为key,refs为value存储到
ObjectAssociationMap *refs = new ObjectAssociationMap;
associations[disguised_object] = refs;
//将new_value存储到refs中
(*refs)[key] = ObjcAssociation(policy, new_value);
//object对应的map不为空了
object->setHasAssociatedObjects();
}
} else {
// setting the association to nil breaks the association.
//查询object对应的map
AssociationsHashMap::iterator i = associations.find(disguised_object);
//associations存在object对应的map
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
//在上一步找到的map中,查讯key对应的j,存在就将值赋值给old_association
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
refs->erase(j);
}
}
}
}
// release the old value (outside of the lock).
//old_association有值,就将其release
if (old_association.hasValue()) ReleaseValue()(old_association);
}
id _object_get_associative_reference(id object, void *key) {
id value = nil;
uintptr_t policy = OBJC_ASSOCIATION_ASSIGN;
{
AssociationsManager manager;
//获取 hasMap associations
AssociationsHashMap &associations(manager.associations());
//获取object对应的key -> disguised_object
disguised_ptr_t disguised_object = DISGUISE(object);
//在associations中以disguised_object为key查找object对应的value->map
AssociationsHashMap::iterator i = associations.find(disguised_object);
//查找到disguised_object对应的i
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
//在refs中以key为键值查找对应的value
ObjectAssociationMap::iterator j = refs->find(key);
//查找到,取值
if (j != refs->end()) {
ObjcAssociation &entry = j->second;
value = entry.value();
policy = entry.policy();
if (policy & OBJC_ASSOCIATION_GETTER_RETAIN) ((id(*)(id, SEL))objc_msgSend)(value, SEL_retain);
}
}
}
if (value && (policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE)) {
//这个不敢解释
((id(*)(id, SEL))objc_msgSend)(value, SEL_autorelease);
}
return value;
}
void _object_remove_assocations(id object) {
//声明一个list,将object之前_object_set_associative_reference绑定的值存储起来
vector< ObjcAssociation,ObjcAllocator > elements;
{
AssociationsManager manager;
//获取associations
AssociationsHashMap &associations(manager.associations());
//associations里面未存储任何值 ->return操作返回
if (associations.size() == 0) return;
//获取key -> disguised_object
disguised_ptr_t disguised_object = DISGUISE(object);
//在associations查找以disguised_object为key,object对应的value ->map
AssociationsHashMap::iterator i = associations.find(disguised_object);
//查找到了 object对应的map
if (i != associations.end()) {
// copy all of the associations that need to be removed.
ObjectAssociationMap *refs = i->second;
//存储refs中的所有value,以便统一执行release操作
for (ObjectAssociationMap::iterator j = refs->begin(), end = refs->end(); j != end; ++j) {
elements.push_back(j->second);
}
// remove the secondary table.
delete refs;
associations.erase(i);
}
}
// the calls to releaseValue() happen outside of the lock.
for_each(elements.begin(), elements.end(), ReleaseValue());
}
链接
https://github.com/Tencent/QMUI_iOS