支持Xcode10和适配iPhone XS Max、iPhone XR的方法

时间:2022-01-01 08:45:56

本文介绍了支持xcode10和适配iphone xs max、iphone xr的方法,分享给大家,具体如下,

支持Xcode10和适配iPhone XS Max、iPhone XR的方法

目前我们项目已做了xcode10(swift4.0)和新机型的适配,总结一下遇到的问题和修改的内容,希望帮助到其他人,如果您有不同的看法或遗漏,欢迎指出!

1.第三方库编译报错

如果项目里用到了mixpanel-swift和swiftlint,这两个在xcode10上会报错,目前作者已提交新版本分别是2.4.5和0.27.0,更新后即可解决报错。

2.library not found for - lstdc++.6.0.9

pod工程编译通过后会进行主工程的编译,如果依赖了libstdc++.tbd和libstdc++.6.0.9.tbd,就会报这个error,原因是苹果在xcode10和ios12中移除了libstdc++这个库,由libc++这个库取而代之,苹果的解释是libstdc++已经标记为废弃有5年了,建议大家使用经过了llvm优化过并且全面支持c++11的libc++库。

临时的解决方法就是把libstdc++.6.0.9.tbd这个文件导入到xcode10中,分别放到以下目录 /applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/sdks/iphoneos.sdk/usr/lib/   和 /applications/xcode.app/contents/developer/platforms/iphonesimulator.platform/developer/sdks/iphonesimulator.sdk/usr/lib/  这时编译可以通过。

但这只是临时的解决方案,如果你自己的业务模块使用了libstdc++,那么就把模块代码重新调整为依赖libc++,然后重新检查是否存在问题,重新编译。如果你引用的第三方厂商提供的sdk中依赖了libstdc++,那么抓紧联系厂商,要求版本升级。

3.enum case '...' not found in type '...'

解决好上面两个报错,编译程序时还会显示这个error,具体场景如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
posvisitquestiontype: string {
  case text
  case textarea = "text_area"
  case dropdownlist = "drop_down_list"
  case radiobutton = "radio_button"
}
let type: posvisitquestiontype!
...
switch type {
case .text, .textarea:
  errortext = nslocalizedstring("please enter the following options", comment: "")
case .dropdownlist, .radiobutton:
  errortext = nslocalizedstring("click the right button to get current location", comment: "")
default:
  break
}

xcode10建议每个case 情况下加“?”

支持Xcode10和适配iPhone XS Max、iPhone XR的方法

原因可能是 type是可选的,所以每个case情况要与type类型保持一致,所以提示加 “?”,可能是xcode10编译器更新的原因。

修改的方法是如果确定type会被赋值,那在定义的时候就把“!”去掉,如果不确定type是否有值就按照xcode提示修改。

4.适配iphone xs max、iphone xr

我们项目在获取机型等信息用的是devicekit这个第三方库,所以也需要更新一下才能获取到新机型的信息,最新版是1.8.1。在最新版有这样一个变量

?
1
2
3
4
/// all face id capable devices
  static public var allfaceidcapabledevices: [device] {
   return [.iphonex, .iphonexs, .iphonexsmax, .iphonexr]
  }

由于iphone x、iphone xs、iphone xs max、iphone xr这些机型的navigationbar高度以及tabbar高度都一致,所以可以用allfaceidcapabledevices是否包含当前设备,来判断当前设备是否有“齐刘海”。

示例:

?
1
2
3
4
5
6
7
8
9
static let faceiddevicearray = device.allfaceidcapabledevices
 
static let navigationheight: cgfloat = {
    if faceiddevicearray.contains(currentdevice) {
      return faceiddevicenavheight
    } else {
      return ordinarydevicenavheight
    }
  }()

同时devicekit中也提供这样一个方法,运行模拟器的时候调用,也会返回真实的设备名称

?
1
2
3
4
5
6
7
8
/// get the real device from a device. if the device is a an iphone8plus simulator this function returns .iphone8plus (the real device).
  /// if the parameter is a real device, this function returns just that passed parameter.
  ///
  /// - parameter device: a device.
  ///
  /// - returns: the underlying device if the `device` is a `simulator`,
  /// otherwise return the `device`.
  public static func realdevice(from device: devicekit.device) -> devicekit.device

示例:

?
1
2
3
4
static let currentdevice = device.realdevice(from: device())
if currentdevice == .iphonex {}
// 取代以下写法
if device() == .iphonex || device() == .simulator(.iphonex) {}

最后别忘了再切两张启动图,因为iphone xs和尺寸和iphone x是一样的,所以iphone xs可以忽略

iphone xr:828px x 1792px

iphone xs max: 1242px x 2688px

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/24920e577e07