Swiftios开发

Xcode 10, Swift 4.2及iOS 12升级适配

2018-09-27  本文已影响1574人  yzyanchao

将开发环境升级到Xcode 10, Swift 4.2及iOS 12后,工程里会出现许多错误。其中的很多错误,只需要点击它的小圆点,Xcode会自动帮我们修复,这些略去不谈。本文主要讲一下不能自动修复的错误。

  1. GRDB.swift使用Carthage编译错误。
    GRDB.swift本身不支持使用Carthage集成,在Xcode 9时可以成功集成,但升级后就不可以了,并且官方不提供技术支持,参见:https://github.com/groue/GRDB.swift/issues/417
    当必须要使用Carthage和GRDB时,只能手动集成GRDB了。
    1)首先去https://github.com/groue/GRDB.swift/releases, 下载最新版的发布版
    2)然后打开GRDB工程,编译出模拟器版和设备版的库
    3)将这两个库合并成一个universal framework,具体来说可以使用
lipo -create -output ./c/GRDB ./a/GRDB.framework/GRDB ./b/GRDB.framework/GRDB
cp -R ./a/GRDB.framework ./c/GRDB.framework
mv ./c/GRDB ./c/GRDB.framework/GRDB

最后,将目录b中的GRDB.framework/Modules/GRDB.swiftmodule中的所有内容复制到目录c中的对应位置。当前目录c中的GRDB.framework就是我们所需要的。
4)打包时不支持包含i386或者x86_64架构的包内容,所以还需要将它们去掉。这里使用的是如下脚本,是根据云信的脚本修改的(它的脚本在Xcode 10下不能正确工作)。

strip_invalid_archs() {
binary="$1"
echo "current binary ${binary}"
echo "$(lipo -info "$binary")"
# Get architectures for current file
archs="$(lipo -info "$binary" | rev | cut -d ':' -f1 | rev)"

isDevice=true

for arch in $ARCHS; do
    if [ ${arch} == "i386" ]; then
        echo "i386"
        isDevice=false
        break
    fi
    if [ ${arch} == "x86_64" ]; then
        echo "x86_64"
        isDevice=false
        break
    fi
    if [ ${arch} == "armv7" ]; then
        echo "armv7"
        break
    fi
    if [ ${arch} == "arm64" ]; then
        echo "arm64"
        break
    fi
done

for arch in $archs; do
    if [ ${isDevice} == true ]; then
        if [ ${arch} == "i386" ]; then
            lipo -remove i386 -output "$binary" "$binary" || exit 1
            echo "Stripped $binary of architectures:$arch"
        fi
        if [ ${arch} == "x86_64" ]; then
            lipo -remove x86_64 -output "$binary" "$binary" || exit 1
            echo "Stripped $binary of architectures:$arch"
        fi
    else
        if [ ${arch} == "armv7" ]; then
            lipo -remove armv7 -output "$binary" "$binary" || exit 1
            echo "Stripped $binary of architectures:$arch"
        fi
        if [ ${arch} == "arm64" ]; then
            lipo -remove arm64 -output "$binary" "$binary" || exit 1
            echo "Stripped $binary of architectures:$arch"
        fi
    fi
done

}

APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"

# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
do
FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"

strip_invalid_archs "$FRAMEWORK_EXECUTABLE_PATH"
done

这样就完成了,最后提供一下参考的云信的脚本:
https://dev.yunxin.163.com/docs/product/IM%E5%8D%B3%E6%97%B6%E9%80%9A%E8%AE%AF/SDK%E5%BC%80%E5%8F%91%E9%9B%86%E6%88%90/iOS%E5%BC%80%E5%8F%91%E9%9B%86%E6%88%90/%E9%9B%86%E6%88%90%E6%96%B9%E5%BC%8F

  1. 错误:UIEdgeInsetsInsetRect' has been replaced by instance method 'CGRect.inset(by:)
    比如之前是:
UIEdgeInsetsInsetRect(bounds, UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding)) 

修改为:

bounds.inset(by: UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding))

参考:https://stackoverflow.com/questions/51616073/uiedgeinsetsinsetrect-has-been-replaced-by-instance-method-cgrect-insetby

  1. 错误:Method does not override any method from its superclass
    有以下两个函数会报这个错:
override func childViewControllerForHomeIndicatorAutoHidden() -> UIViewController?
override func prefersHomeIndicatorAutoHidden() -> Bool

这是因为它们在Swift 4.2中被改成变量了,改成下面这样:

override var childForHomeIndicatorAutoHidden: UIViewController?
override var prefersHomeIndicatorAutoHidden: Bool
  1. 类型要求更加严格
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 

以前工程里是这样写的,但现在不行了,必须写成

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
  1. 错误:'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'
    将代码
UIImageJPEGRepresentation(image, quality)

改成

image.jpegData(compressionQuality: quality)
  1. 错误:AVAudioSession, 'setCategory' is unavailable in Swift
    这是Swift 4.2的一个bug,AVAudioSession的setCategory这个函数提供了一个新版本,然后旧版本被删了。导致iOS 10以下的版本没有办法设置category。好在Objective-C版本的函数还能用,有2种方案,详见参考链接,我使用了简单的那一种,如下:
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)

参考:https://stackoverflow.com/questions/52413107/avaudiosession-setcategory-swift-4-2

  1. Xcode 10提交的App在iOS 9.0 ~ iOS 9.2版本手机上随机crash
    这是Xcode 10的一个know issue:
    Apps that contain asset catalogs built using Xcode 10 or later with a deployment target set to iOS 9.0, 9.1 or 9.2 produce content incompatible with the runtimes of those iOS versions. (44535967, 45723580, 45723189)
    原文参见:https://developer.apple.com/documentation/xcode_release_notes/xcode_10_1_release_notes?preferredLanguage=occ

那么解决方案有两个:
a. 使用Xcode 9.4.1打包提交App
b. 将可支持的最低iOS版本提到iOS 9.3
参考:
https://stackoverflow.com/questions/52364231/our-app-crashed-in-ios-9-which-upload-by-xcode-10

上一篇下一篇

猜你喜欢

热点阅读