iOS13下新API和出现的问题


iOS13的API的变更和适配问题,在一段时间内可能会有不一样的问题和方式出现,因此本文会持续更新iOS13所存在的问题

1、目录

  • 修改的API
  • 被禁止的KVC方式
  • 关于暗黑模式和切换

2、修改的API

1. UIColor动态属性

在iOS13以前,UIColor指标是一种颜色。
在iOS13,UIColor拥有了动态属性。它能够在 LightMode 和 DarkMode 拥有不一样的颜色。
如下是iOS13最新系统提供的颜色的方法ios

extension UIColor {

    @available(iOS 13.0, *)
    public init(dynamicProvider: @escaping (UITraitCollection) -> UIColor)

    /* Resolve any color to its most fundamental form (a non-dynamic color) for a specific trait collection.
     */
    @available(iOS 13.0, *)
    open func resolvedColor(with traitCollection: UITraitCollection) -> UIColor
}
复制代码

系统颜色的动态切换是怎么作的,你是否很好奇,实际上是作了不一样的状态下设置颜色,在切换的时候就取了相应的颜色。下面来看看:web

@available(iOS 12.0, *)
public enum UIUserInterfaceStyle : Int {
    case unspecified 
    case light
    case dark
}

 //利用上面iOS13的 init方法
  let color = UIColor{ (traitCollection) -> UIColor in
            if traitCollection.userInterfaceStyle == .light {
                return .orange
            } else if traitCollection.userInterfaceStyle == .dark {
                return .white
            } else  { //traitCollection.userInterfaceStyle == .unspecified
                return .green
            }
        }
复制代码

新增的一些颜色,那你能够直接点进去看看。这里举例部分:windows

/* Foreground colors for static text and related elements.
     */
    @available(iOS 13.0, *)
    open class var label: UIColor { get }

    @available(iOS 13.0, *)
    open class var secondaryLabel: UIColor { get }

    @available(iOS 13.0, *)
    open class var tertiaryLabel: UIColor { get }

    @available(iOS 13.0, *)
    open class var quaternaryLabel: UIColor { get }

    
    /* Foreground color for standard system links.
     */
    @available(iOS 13.0, *)
    open class var link: UIColor { get }

    
    /* Foreground color for placeholder text in controls or text fields or text views.
     */
    @available(iOS 13.0, *)
    open class var placeholderText: UIColor { get }

    
  @available(iOS 13.0, *)
    open class var separator: UIColor { get }

    @available(iOS 13.0, *)
    open class var opaqueSeparator: UIColor { get }

   ...

复制代码
2. Status Bar更新

在iOS13以前有两种状态,defaultlightContent
在iOS13 有三种状态,default, darkContentlightContentapi

  • 如今的 darkContent 对应以前的 default
  • 如今的 default 会根据状况自动选择 darkContent 和 lightContent
3. UIActivityIndicatorView加载视图菊花
  • iOS13以前有三种样式:
    UIActivityIndicatorViewStyleGray 灰色
    UIActivityIndicatorViewStyleWhite 白色
    UIActivityIndicatorViewStyleWhiteLarge 白色(大型)xcode

  • iOS13废弃了以上三种样式,而用如下两种样式代替:
    UIActivityIndicatorViewStyleLarge (大型)
    UIActivityIndicatorViewStyleMedium (中型)bash

  • iOS13经过color属性设置其颜色微信

4. 获取Window用keyWindow
@available(iOS, introduced: 2.0, deprecated: 13.0, message: "Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes")
    open var keyWindow: UIWindow? { get }
    open var windows: [UIWindow] { get }
复制代码

iOS13以前网络

let window = UIApplication.shared.keyWindow
复制代码

如今可使用app

let window = UIApplication.shared.windows[0]
复制代码
5. MPMoviePlayerController 被弃用

在 iOS 9 以前播放视频可使用 MediaPlayer.framework 中的MPMoviePlayerController类来完成,它支持本地视频和网络视频播放。可是在 iOS 9 开始被弃用,若是在 iOS 13 中继续使用的话会直接抛出异常:ide

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'MPMoviePlayerController is no longer available. Use AVPlayerViewController in AVKit.'
复制代码

解决办法: Use AVPlayerViewController in AVKit,即便用 AVPlayer 做为视频播放控件

6.使用 UISearchDisplayController 致使崩溃

在 iOS 8 以前,咱们在 UITableView 上添加搜索框须要使用 UISearchBar + UISearchDisplayController 的组合方式,
在 iOS 8 以后,苹果就已经推出了 UISearchController 来代替这个组合方式。在 iOS 13 中,若是还继续使用 UISearchDisplayController会直接致使崩溃,崩溃信息以下:

Terminating app due to uncaught exception 'NSGenericException', reason: 'UISearchDisplayController is no longer supported when linking against this version of iOS. Please migrate your application to UISearchController.' 
复制代码
  • 解决办法: Please migrate your application to UISearchController.', 也就是用UISearchController代替
7. 模态弹出默认样式改变

在 iOS 13,使用 presentViewController 方式打开视图,会跟导航栏有部分视觉差,这里就不上图了,能够自行试一下。
缘由是:苹果将 UIViewControllermodalPresentationStyle 属性的默认值改为了新加的一个枚举值 UIModalPresentationAutomatic,对于多数 UIViewController,此值会映射成 UIModalPresentationPageSheet

  • 解决办法: 能够在vcpresent以前设置modalPresentationStyle 为 UIModalPresentationFullScreen

  • 另外,present的vc用户下拉能够dissmiss控制器,若是不想要这效果,能够这样设置

/*当该属性为 false 时,用户下拉能够 dismiss 控制器,为 true 时,下拉不能够 dismiss控制器*/
  xxVC.isModalInPresentation = true
复制代码
  • 还有一点须要注意,原来以UIModalPresentationFullScreen样式弹出页面,那么这个页面弹出 ViewController 会依次调viewWillDisappearviewDidDisappear。而后在这个页面被 dismiss 的时候,将他弹出的那个 ViewControllerviewWillAppearviewDidAppear会被依次调用。然而使用默认的视差效果弹出页面,将他弹出的那个 ViewController 并不会调用这些方法,原先写在这四个函数中的代码之后都有可能会存在问题。
8. 蓝牙权限更新

在 iOS 13 中,苹果将原来蓝牙申请权限用的 NSBluetoothPeripheralUsageDescription 字段,替换为 NSBluetoothAlwaysUsageDescription 字段。

For apps with a deployment target of iOS 13 and later, use NSBluetoothAlwaysUsageDescription instead.
复制代码
9.废弃UIWebview 改用 WKWebView
@available(iOS, introduced: 2.0, deprecated: 12.0, message: "No longer supported; please adopt WKWebView.")
复制代码

iOS13 开始苹果将 UIWebview 列为过时API(支持iOS2.0-iOS12)。 目前提交苹果应用市场(App Store)会发送邮件提示你在下一次提交时将应用中UIWebView 的 api 移除。邮件内容:

Dear Developer,

We identified one or more issues with a recent delivery for your app, "xxx". Your delivery was successful, but you may wish to correct the following issues in your next delivery:

ITMS-90809: Deprecated API Usage - Apple will stop accepting submissions of apps that use UIWebView APIs . See [developer.apple.com/documentati…]([https://developer.apple.com/documentation/uikit/uiwebview](https://developer.apple.com/documentation/uikit/uiwebview)
) for more information.

After you’ve corrected the issues, you can use Xcode or Application Loader to upload a new binary to App Store Connect.

Best regards,

The App Store Team

复制代码
  • 暂时没有强制使用WKWebView,可是在iOS13开始UIWebView已经是废弃的API,之后更高的版本中防止出现问题,尽早移除是上上之策。

  • 目前我所用到的最新版本微信支付sdk(1.8.6版),已将UIWebView替换成了WKWebView.

  • 这个来自别人的文章,能够查看哪些sdk使用了UIWebView(我没有试过):

find . -type f | grep -e ".a" -e ".framework" | xargs grep -s UIWebView

复制代码
10.UISegmentedControl 默认样式改变,默认样式变为 白底黑字,若是设置修改过颜色的话,页面须要修改
11.Sign In with Apple

在 iOS 13 中苹果推出一种在 App 和网站上便捷登陆的方式: Sign In With Apple,这是 iOS 13 新增的功能,所以须要使用 Xcode 11 进行开发。请在 App Store 应用审核指南 中查看具体要求

12.LaunchImage (iOS 7.0–13.0)

目前个人项目中也是使用 LaunchImage来设置启动图。可是在iOS13开始苹果建议使用 Launch Screen.

UILaunchImages has been deprecated; use Xcode launch storyboards instead. For more information on how to construct and format your launch storyboard, seeLaunch Screen

复制代码
  • 从2020年4月开始,全部支持 iOS 13 的 App 必须提供 LaunchScreen.storyboard,不然将没法提交到 App Store 进行审批。

3、 被禁止的kvc

1.UITextFiled 修改根据kvc提示文字的大小和颜色,在iOS13会直接崩溃,报错信息以下

修改办法

if #available(iOS 13.0, *) {
    let arrStr = NSMutableAttributedString(string: field.placeholder!, attributes: [NSAttributedString.Key.foregroundColor: UIColor.systemGray3, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)])
 field.attributedPlaceholder = arrStr
} else {
    field.setValue(UIColor.systemGray3, forKeyPath: "_placeholderLabel.textColor")
    field.setValue(UIFont.systemFont(ofSize: 15), forKeyPath:"_placeholderLabel.font")
}

复制代码
2. 获取UISearchBar的textField

在iOS13以前,咱们经过"_searchField"来获取UISearchTextField来修改一些属性。

let searchTextField = searchBar.value(forKey: "_searchField")

复制代码

在iOS13中,继续这样会崩溃,以下图。


系统提供能够直接获取到的方法

//系统提供的方法
extension UISearchBar {
    open var searchTextField: UISearchTextField { get }
}

复制代码

在使用的过程当中须要判断处理

if #available(iOS 13.0, *) {
      let searchTextField =  searchBar.searchTextField
} else {
      let searchTextField = searchBar.value(forKey: "_searchField")
}
复制代码
3.UISearchBar 黑线处理致使崩溃

iOS13以前为了处理搜索框的黑线问题,一般会遍历 searchBar 的 subViews,找到并删除 UISearchBarBackground
在 iOS13 中这么作会致使 UI 渲染失败,而后直接崩溃,崩溃信息以下:

Terminating app due to uncaught exception'NSInternalInconsistencyException', reason: 'Missing or detached view for search bar layout'
复制代码
  • 解决办法 设置 UISearchBarBackground 的 layer.contents 为 nil:
for (UIView *view in _searchBar.subviews.lastObject.subviews) {
    if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        view.layer.contents = nil;
        break;
    }
} 
复制代码
4.获取状态栏视图
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
复制代码

4、关于暗黑模式和切换

1. 暗黑模式是iOS13的一大亮点,下面来看看模式切换的设置。
  • 切换 修改当前 UIViewControllerUIView的模式。只要设置了控制器为暗黑模式,那么它子view也会对应的修改
    即:只会影响当前的视图,不会影响前面的 controller 和后续 present 的 controller。

可是当咱们在 window 上设置 overrideUserInterfaceStyle 的时候,就会影响 window 下全部的 controller, view,包括后续推出的 controller。

self.overrideUserInterfaceStyle = .dark // .light
复制代码
  • 获取当前的模式
if self.traitCollection.userInterfaceStyle == .dark {
            // Dark
            print("是dark模式、。。。")
        } else  if self.traitCollection.userInterfaceStyle == .light {
            // Light
            print("是light模式、。。。")
        } else {
            //unspecified
            print("是unspecified模式、。。。")
        }
复制代码
  • 监听模式的变化
///监听模式的变化
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
            //模式发生变化会回调这里
        }
    }
复制代码
2. 图片简单适配

须要在xcode11上处理,以下图



选择后出现下图所示的,能够防止 Dark模式下图片的地方

这样仍是比较快捷的,例外若是切图是有背景色的估计会有点麻烦,可能会须要从新切,具体看适配状况。



未完待续

相关文章
相关标签/搜索