在生产中捕获UIViewAlertForUnsatisfiableConstraints [英] Catch UIViewAlertForUnsatisfiableConstraints in production

查看:105
本文介绍了在生产中捕获UIViewAlertForUnsatisfiableConstraints的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在生产中捕获自动布局约束歧义 - 相当于 UIViewAlertForUnsatisfiableConstraints 断点,但对于生产应用程序?

Is it possible to catch autolayout constraint ambiguities in production – the equivalent of a UIViewAlertForUnsatisfiableConstraints breakpoint but for production apps?

我的目标是添加一个全局处理程序,将这些错误报告给日志系统。

My goal would be to add a global handler that would report such errors to a logging system.

推荐答案

符号 UIViewAlertForUnsatisfiableConstraints 实际上是一个函数:

The symbol UIViewAlertForUnsatisfiableConstraints actually is a function :

_UIViewAlertForUnsatisfiableConstraints(NSLayoutConstraint * unsatisfiableConstraint,NSArray< NSLayoutConstraint *> ; * allConstraints)

它是私有的,所以你不能替换它。

It is private, so you can't replace it.

但它是从私有方法 - [UIView引擎:willBreakConstraint:dueToMutuallyExclusiveConstraints:] 调用的,可以调整。此方法大致包含以下内容:

But it is called from private method -[UIView engine:willBreakConstraint:dueToMutuallyExclusiveConstraints:], which can be swizzled. This method has approximately this content:

void -[UIView engine:willBreakConstraint:dueToMutuallyExclusiveConstraints:] {
  if ([self _isUnsatisfiableConstraintsLoggingSuspended]) {
    [self _recordConstraintBrokenWhileUnsatisfiableConstraintsLoggingSuspended:$arg4]; // add constraint to some pool
  }
  else {
    if (__UIConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints) {
      // print something in os_log
    }
    else {
      _UIViewAlertForUnsatisfiableConstraints($arg4, $arg5);
    }
  }
}

如果我理解正确的< a href =http://apprize.info/apple/ios_6/6.html\"rel =nofollow noreferrer>这篇文章, __ UIConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints 将永远在iOS上返回NO,所以您需要做的就是检查名为 _isUnsatisfiableConstraintsLoggingSuspended 的私有bool属性,然后调用原始方法。

If I understand correctly from this article, __UIConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints will always return NO on iOS, so all you need to do is to check private bool property called _isUnsatisfiableConstraintsLoggingSuspended and call original method then.

这是结果代码示例:

#import <objc/runtime.h>

void SwizzleInstanceMethod(Class classToSwizzle, SEL origSEL, Class myClass, SEL newSEL) {
  Method methodToSwizzle = class_getInstanceMethod(classToSwizzle, origSEL);
  Method myMethod = class_getInstanceMethod(myClass, newSEL);
  class_replaceMethod(classToSwizzle, newSEL, method_getImplementation(methodToSwizzle), method_getTypeEncoding(methodToSwizzle));
  class_replaceMethod(classToSwizzle, origSEL, method_getImplementation(myMethod), method_getTypeEncoding(myMethod));
}

@interface InterceptUnsatisfiableConstraints : NSObject
@end

@implementation InterceptUnsatisfiableConstraints

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    SEL willBreakConstantSel = NSSelectorFromString(@"engine:willBreakConstraint:dueToMutuallyExclusiveConstraints:");
    SwizzleInstanceMethod([UIView class], willBreakConstantSel, [self class], @selector(pr_engine:willBreakConstraint:dueToMutuallyExclusiveConstraints:));
  });
}

- (void)pr_engine:(id)engine willBreakConstraint:(NSLayoutConstraint*)constraint dueToMutuallyExclusiveConstraints:(NSArray<NSLayoutConstraint*>*)layoutConstraints {
  BOOL constrainsLoggingSuspended = [[self valueForKey:@"_isUnsatisfiableConstraintsLoggingSuspended"] boolValue];
  if (!constrainsLoggingSuspended) {
    NSLog(@"_UIViewAlertForUnsatisfiableConstraints would be called on next line, log this event");
  }
  [self pr_engine:engine willBreakConstraint:constraint dueToMutuallyExclusiveConstraints:layoutConstraints];
}

@end

适用于iOS 8.2 / 9/10(它在iOS 8.1中不起作用,所以要小心),但我无法保证。
此外,它还可以捕获系统组件中的约束问题,例如键盘/视频播放器/等。
此代码很脆弱(它可能导致任何系统版本更新崩溃,参数更改等),我不建议在生产中使用它(猜测它甚至不会通过自动审查过程)。你有最后一个字,但是你被警告了。

It works on iOS 8.2/9/10 (it doesn't work in iOS 8.1, so be careful), but I can't give any guarantee. Also, it catches constraint problems in system components, such as keyboard/video player/etc. This code is fragile (it can lead to crash on any system version update, parameters change, etc) and I will not recommend to use it in production (guess that it will not pass even automated review process). You have the last word, but you are warned.

但是我认为您可以在内部/外部测试人员的构建中使用它来修复生产前的autolayout中的错误。

However I think that you can use it in builds for internal/external testers to fix bugs in autolayout before production.

注意到您使用的是swift:您可以使用桥接头文件将此代码添加到swift项目中。

Noticed that you are using swift: you can add this code to your swift project with bridging header file.

这篇关于在生产中捕获UIViewAlertForUnsatisfiableConstraints的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆