ios 开发 准备



With WWDC20 taking off on June 22, and alongside it the betas of the next operating systems, now is the time to get ready. We can only guess at the sorts of things that Apple may add in iOS 14, but that doesn’t mean we cant get ourselves into a good position for whatever it is. Here are a few things you should be doing to be in the best possible position — and how you can do them.

随着WWDC20将于6月22日起飞,同时还有下一个操作系统的Beta版,现在是时候准备好了。 我们只能猜测苹果可能会在iOS 14中添加的各种功能,但这并不意味着我们无论如何都无法让自己处于一个好的位置。 为了达到最佳状态,您应该做一些事情-以及如何做到这些。

暗模式 (Dark Mode)

Dark mode is the biggest user-facing change that we saw, bringing a system-wide appearance that satisfied the night owls among us. Implementing it can be extremely easy, and it gives you a chance to refine your assets, removing duplicate colours as you make new ones that can respond to the system-interface mode.

暗模式是我们看到的最大的面向用户的更改,它带来了整个系统的外观,使我们之间的夜猫子满意。 实施起来非常容易,它使您有机会优化资产,在制作可以响应系统界面模式的新颜色时,删除重复的颜色。



ios 开发适配 iphone14 ios14选开发版还是公测版_ios

The easiest way to implement dark mode is to have your colours in an asset catalogue.

实施暗模式的最简单方法是将颜色显示在资产目录中。

To do this, simply go to where you’d normally add a new image (the plus at the bottom of the list of assets ), and select new colour instead. Set the colour to what you’d like your light-mode colour to look like, and open the attributes inspector on the right-hand side. Set the Appearances option to “Any, Dark,” and you’ll see a second colour appear. Set this to whatever you’d like your colour to look like in dark mode, typically a slightly lighter shade of the one you had for normal appearances.

为此,只需转到通常要添加新图像的位置(资产列表底部的加号),然后选择新颜色即可。 将颜色设置为您希望的灯光模式颜色,然后打开右侧的属性检查器。 将外观选项设置为“ Any,Dark”,您将看到出现第二种颜色。 将此设置为您想要的颜色在黑暗模式下的显示效果,通常比正常外观下的颜色略浅。

To use these colours in code, you simply do:

要在代码中使用这些颜色,只需执行以下操作:

let color = UIColor(named: “’CBPurple”)

And these will automatically respond to the changes in interface state. Make sure to go through your app and set the various text colours to new ones that respond to dark mode, as users won’t be able to read your text. You do get a lot of system colours available (provided you set your deployment target to iOS 13), all of which adapt nicely to dark mode. To see this in action, set something to .systemRed, and look at it in light and dark mode. You’ll see that the red turns almost pink to become more readable.

这些将自动响应接口状态的更改。 由于用户将无法阅读您的文本,因此请务必浏览您的应用并将各种文本颜色设置为可响应黑暗模式的新颜色。 您确实可以获得很多系统颜色(前提是您将部署目标设置为iOS 13),所有这些颜色都能很好地适应黑暗模式。 要查看实际效果,请将某些内容设置为.systemRed ,然后在明暗模式下进行查看。 您会看到红色变成几乎粉红色,变得更具可读性。

Quick tip: Instead of jumping into developer settings in the simulator or using your phone and changing your own settings, you can easily simulate dark mode using interface overrides in Xcode. Select the Environment Overrides toggle when running the app, and adjust between the two modes quickly.

快速提示:无需进入模拟器中的开发人员设置或使用手机并更改自己的设置,而是可以使用Xcode中的界面替代轻松地模拟暗模式。 运行应用程序时,选择环境替代开关,然后在两种模式之间快速调整。


For more info, check out the WWDC session from last year.

有关更多信息,请查看去年的WWDC会议

情态表达风格 (Modal-Presentation Styles)

You’ll notice as soon as you use the system apps in iOS 13, or anything that’s been optimised, that modals present a little differently now.

您会在使用iOS 13中的系统应用程序或经过优化的任何内容后立即注意到,模态现在呈现出一些不同。

This is because the default presentation style has changed from UIModalPresentationFullScreen to UIModalPresentationAutomatic, which changes based on its context. The default presentation style for automatic is UIModalPresentationPageSheet.

这是因为默认的表示样式已从UIModalPresentationFullScreen更改为UIModalPresentationAutomatic ,后者根据其上下文而更改。 自动的默认表示样式是UIModalPresentationPageSheet


ios 开发适配 iphone14 ios14选开发版还是公测版_ios 开发适配 iphone14_02

A page sheet presentation on the iPhone

iPhone上的页面演示文稿

The page sheet presentation on iPhone gives you an interactive drag-to-dismiss feature for free and a gorgeous animation for making the underlying screen smaller.

iPhone上的页面演示文稿为您提供了一个交互式的拖放功能,该功能是免费的,并且还提供了精美的动画以缩小基础屏幕。

On iPad, you get a modal in the centre of the screen that you can again drag to dismiss. When in a compact environment, such as the smallest multitasking window, it’ll revert to the iPhone-style presentation.

在iPad上,您会在屏幕中央看到一个模态,然后可以再次拖动以将其关闭。 在紧凑的环境中(例如最小的多任务窗口),它将恢复为iPhone样式的演示文稿。


ios 开发适配 iphone14 ios14选开发版还是公测版_应用程序_03

A page sheet presentation on the iPad iPad上的页面演示文稿


You should be aware of these throughout the app, ensuring that if you have something the user shouldn’t be able to dismiss, like a login form, you either change the presentation style or disable dismissing.

您应该在整个应用程序中都注意这些问题,以确保如果您有某些用户无法关闭的内容(例如登录表单),则可以更改演示样式或禁用关闭。

To stop the user from being able to dismiss, override the property isModalInPresentation on the view controller, and set it to true. This will disable the interactive dismiss until you change the property or dismiss the view controller programmatically.

要阻止用户退出,请覆盖视图控制器上的属性isModalInPresentation ,并将其设置为true 。 这将禁用交互式关闭,直到您更改属性或以编程方式关闭视图控制器为止。

There’s a lot you can do with these, and I recommend checking out the WWDC video on this topic to find out more. At around 15 minutes, you’ll see detailed information about modal presentation, including some new delegate methods that can help you detect dismissing.

您可以使用这些工具做很多事情,我建议您查看有关此主题的WWDC视频 ,以了解更多信息。 在大约15分钟时,您将看到有关模式表示的详细信息,包括一些新的委托方法,可以帮助您检测解雇。

场景委托 (Scene Delegate)

iOS 13 brought with it the brand new SceneDelegate and support for multiple windows. You should consider if your app is one that may benefit from this — most productivity apps will see a great improvement by allowing users on Mac and iPad to open multiple instances. Look at the way notes behaves on your iPad, allowing you to open two side by side.

iOS 13带来了全新的SceneDelegate并支持多个窗口。 您应该考虑自己的应用程序是否可以从中受益—大多数生产力应用程序将允许Mac和iPad上的用户打开多个实例,从而带来巨大的进步。 查看笔记在iPad上的表现方式,让您并排打开两个。

If you make a new app, the scene delegate is now the default. The role of the app delegate is slightly different now that we have the scene delegate. It should be used for one time setup only, for example initialising your database or registering for push notifications.

如果创建新应用,则场景代理现在是默认应用。 现在我们有了场景委托,应用程序委托的角色稍有不同。 它仅应用于一次设置,例如初始化数据库或注册推送通知。

The scene delegate has all the lifecycle hooks you’re used to from the app delegate, including:

场景委托拥有您习惯于应用委托的所有生命周期挂钩,包括:

  • Connecting (starting)
  • Disconnecting (stopping)
  • Becoming active
  • Resigning active
  • Enter foreground
  • Enter background

You can use all of these to recreate functionality that you used to have in your app delegate, except now you’ve got yourself ready for multiple windows. If you ensure that on a scene becoming active it keeps all of its data sources up to date, you should have very little work to do.

您可以使用所有这些来重新创建您在应用程序委托中曾经拥有的功能,除非现在您已经为多个窗口做好了准备。 如果您确保在一个活动的场景上保持其所有数据源都是最新的,那么您应该要做的工作很少。

In order to open a new window in your app, you have to take advantage of one of the existing APIs that responds to users trying to open one. A great example is that collection view has a new …

为了在您的应用中打开一个新窗口,您必须利用现有的API之一来响应试图打开一个窗口的用户。 一个很好的例子是集合视图有一个新的…

collectionView(_collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath)

… method exposed to the UICollectionViewDragDelegate. You may already conform to this if you supported drag and drop in the past — in which case, you’ll just have to use the new method to add this whole new window.

…方法公开给UICollectionViewDragDelegate 。 如果您过去曾经支持拖放操作,则可能已经符合此要求-在这种情况下,您只需使用new方法来添加整个新窗口。

In the provided sample, your can see the whole lifecycle: setting up an NSUserActivity, registering that to an item provider, and then creating a drag item from that provider.

在提供的示例中,您可以看到整个生命周期:设置NSUserActivity ,将其注册到项目提供程序,然后从该提供程序创建拖动项目。

If you do this, users can drag items from your collection view to make a new window of your app, and all you did was conform to this delegate and respond to user activities in your new scenes — almost effortless!

如果执行此操作,则用户可以从集合视图中拖动项目以创建应用程序的新窗口,而您所做的全部工作就是遵守该委托并响应新场景中的用户活动-几乎毫不费力!

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { 
    var dragItems = [UIDragItem]()
    let selectedPhoto = photo(at: indexPath)    if let imageToDrag = UIImage(named: selectedPhoto.name) {        let userActivity = selectedPhoto.openDetailUserActivity        let itemProvider = NSItemProvider(object: imageToDrag)        itemProvider.registerObject(userActivity, visibility: .all)        let dragItem = UIDragItem(itemProvider: itemProvider)        dragItem.localObject = selectedPhoto        dragItems.append(dragItem)    }    return dragItems}

Apple has a great guide on this topic alone.

Apple仅在此主题方面有出色的指南

弃用 (Deprecations)

Possibly the biggest thing to keep in mind when getting ready is the deprecated SDK features you’re using in your app.

准备就绪时可能要记住的最大事情是您在应用中使用的不推荐使用的SDK功能。

Any of these warnings may suddenly turn into errors when we get access to the new SDK — so get rid of them

当我们访问新的SDK时,这些警告中的任何一个都可能突然变成错误-因此,请消除它们

Here’s a couple to be aware of.

这里有几个需要注意的地方。

'UINavigationBar.appearence()' (‘UINavigationBar.appearence()’)

The old UINavigationBar.appearance APIs already take no effect on iOS 13. You should look at implementing UINavigationBarAppearance instead. This is used in a similar fashion but with more customisability.

旧的UINavigationBar.appearance API在iOS 13上已经无效。您应该考虑实现UINavigationBarAppearance 。 它以类似的方式使用,但具有更高的可定制性。

let navigationBarAppearance = UINavigationBarAppearance()navigationBarAppearance.backgroundColor = UIColor(named: “purple”)navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearancenavigationController?.navigationBar.compactAppearance = navigationBarAppearancenavigationController?.navigationBar.standardAppearance = navigationBarAppearance

Be sure to test your navigation bars in all of the different appearances — for example, landscape on the iPhone takes on the compactAppearance. You may want to customise each appearance to suit its use better, possibly changing things such as the font colours or the bar background.

确保以所有不同的外观测试导航栏-例如,iPhone上的横向外观采用compactAppearance 。 您可能需要自定义每个外观以更好地使用它,可能会更改诸如字体颜色或条形背景之类的内容。

'StatusBarStyle' (‘StatusBarStyle’)

The status bar customisation has been adjusted to work better with the new UIWindowScene APIs.

状态栏自定义已进行调整,以更好地与新的UIWindowScene API配合使用。

UIApplication.shared.statusBarStyle = .lightContent

UIApplication.shared.statusBarStyle = .lightContent

You can no longer simply set the statusBarStyle on the whole application. You have to do it on a per-ViewController level, using something like the following. If you do this on your navigation controllers, it’ll display the way you intend for every view within that navigation stack — removing the need to override on every single view controller.

您不能再简单地在整个应用程序上设置statusBarStyle 。 您必须在每个V iewController级别上执行此操作,方法如下。 如果在导航控制器上执行此操作,它将显示该导航堆栈中每个视图的预期方式,而无需在每个单个视图控制器上进行覆盖。

override var preferredStatusBarStyle: UIStatusBarStyle {    return .lightContent}

'UIWebView' (‘UIWebView’)

Well, this is a big one.

好吧,这是一个很大的。

Apps that reference UIWebView will no longer be allowed on the store. For new apps, this is already the case — so you don’t really have a choice here.Bear in mind, if you’re swapping out UIWebView for WKWebView, Apple also has new rules on the main functionality of your app being a web view, so be cautious here. Be wary that your pods may still reference UIWebView, even if you don’t. This is very common in any older pod that has some form of networking built in, such as AFNetworking. Provided your app isn’t entirely based on a UIWebView, you should very easily be able to transfer across due to the very similar APIs. You’ll be getting a lot of performance improvements completely free here.

商店中将不再允许引用UIWebView应用程序。 对于新应用程序,情况已经是这样-因此您在这里实际上没有选择。请记住,如果您将UIWebView WKWebView ,Apple还对应用程序作为网络的主要功能有新规定 。观点,所以在这里要谨慎。 请注意,即使您没有,吊舱可能仍会引用UIWebView 。 这在任何内置了某种形式的网络的较旧的Pod中都很常见,例如AFNetworking 。 如果您的应用程序不完全基于UIWebView ,则由于API非常相似,因此您应该可以轻松地进行迁移。 您将在这里完全免费获得许多性能改进。



'KeyWindow' (‘KeyWindow’)

As a result of the addition of the scene delegate, certain properties of the application we may be used to have been removed. You can still access them, but they may not behave as you expect.

由于添加了场景委托,因此我们可能曾经删除了应用程序的某些属性。 您仍然可以访问它们,但是它们可能无法按预期运行。

UIApplication.shared.keyWindow will present you with the error:

UIApplication.shared.keyWindow将为您显示错误:

keyWindow was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes.”

keyWindow在iOS 13.0中已弃用:不应用于支持多个场景的应用程序,因为它会返回所有已连接场景的关键窗口。”

Apple’s recommended solution from WWDC19 was to simply track your windows manually. This way you can keep track of the initial window in your app, then add the others as you go.

苹果从WWDC19推荐的解决方案是简单地手动跟踪Windows。 这样,您可以跟踪应用程序中的初始窗口,然后在添加时添加其他窗口。

It makes presenting something over the whole window trivial once again, as you can make sure you present on the correct window and not just any window. Imagine if you got an error for something you did in a second window presenting on the first — not a great user experience. If you really have to grab the key window, you can iterate over UIApplication.shared.windows and look for the first one where .isKeyWindow returns true.

这样可以使在整个窗口中呈现的内容再次变得微不足道,因为您可以确保在正确的窗口中呈现,而不仅仅是在任何窗口中呈现。 想象一下,如果在第二个窗口中出现的错误是在第一个窗口中显示的,那不是很好的用户体验。 如果确实需要获取键窗口,则可以遍历UIApplication.shared.windows并查找第一个.isKeyWindow返回true .isKeyWindow

Here’s a handy backwards-compatible extension to do just that:

这是一个方便的向后兼容扩展,可以做到这一点:

extension UIWindow {
    static var key: UIWindow? {
        if #available(iOS 13, *) {
        return UIApplication.shared.windows.first { $0.isKeyWindow }
        } else {
        return UIApplication.shared.keyWindow
        }
}

(Conclusion)

While that might seem like a decent amount of work — it’s going to be worth it when you end up with a project that’s ready for the next best things!

尽管这似乎是一件可观的工作,但是当您完成一个可以准备下一件好事的项目时,这将是值得的!


翻译自: https://medium.com/better-programming/getting-ready-for-ios-14-7cffcf198239

ios 开发 准备