iOS开发3DTouch导致Crush

原因推测

最近公司项目加入 3D touch 功能上线后, 有用户反映他们的机器在点击程序 icon 的时候就直接闪退了, 找了个问题机测试了一下发现是 3D touch 导致的崩溃. 正规渠道购买的机器都没有闪退问题, 某些港版, 日版, 美版机器会出问题, 所以推测是那些机器在购买之前经过越狱或是什么原因, 修改了底层 3D touch 某些属性, 导致本不支持 3D touch 的问题机误以为自己支持该功能导致崩溃.

  • 只是推测, 老鸟欢迎指正

初步方案

shortCutItem

由于没有那么多问题机测试, 只有两台, 只能算一个参考方案吧, 反正这两台机器是没问题了.
方案: 限制机型, 只允许 6S 和 9.0以上系统走 3D Touch 方法.
代码:
在AppDelegate.m中
首先导入头文件#import "sys/utsname.h"
pch 文件中定义系统版本号#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

// 获取机型设备号
struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    // 如果系统版本大于9.0
    if (IOS_VERSION >= 9.0) {
        
        // 如果设备号是 6S 或者 6S PLUS
        if ([deviceString isEqualToString:@"iPhone8,1"] || [deviceString isEqualToString:@"iPhone8,2"]) {
            
            // 创建 shortCutItem 的方法
            [self createItemsWithIcons];
            
            if (launchOptions[@"UIApplicationLaunchOptionsShortcutItemKey"] == nil) {
                return YES;
            } else {
                return NO;
            }
        }
    }

至此, 打开就闪退的问题在这两台机器上解决了


Preview

在支持 3d touch的视图控制器.m文件中同样导入#import "sys/utsname.h"
在设置traitCollection的方法中同样用上面的判断

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    // 获取设备型号
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    // 判断系统型号
    if (IOS_VERSION >= 9.0) {
        
        if ([deviceString isEqualToString:@"iPhone8,1"] || [deviceString isEqualToString:@"iPhone8,2"]) {
            
            if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
                
                [self registerForPreviewingWithDelegate:self sourceView:self.levelCollectionView];
            }
        }
    }
}

如果要预览的 webView 打开了allowsLinkPreview
同样需要重复导入上面的头文件

struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([deviceString isEqualToString:@"iPhone8,1"] || [deviceString isEqualToString:@"iPhone8,2"]) {
        self.webView.allowsLinkPreview = YES;
    }

至此, 这两台问题机器的 3D Touch 崩溃问题解决


设备型号(不完全版)

- (NSString*)deviceVersion {

    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    NSLog(@"%@",deviceString);
    
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    
    return deviceString;
}
  • iPhone 7,2是6, 7,1是6Plus

这个方案还不是很确定, 如果我说的有问题, 欢迎来纠错, 谢谢
补充一下, 不管上一页有没有设置preView, 只要下一页的webView打开了allowsLinkPreview属性, 也会导致问题机型的崩溃

你可能感兴趣的:(iOS开发3DTouch导致Crush)