Some notes when coding with iOS 13 (Part 1)

Tram Ho

Introduction

At the September event, along with the launch of the three major iPhone products, Apple has released iOS 13 with a series of new updates.

However, along with useful features, this version of iOS is also considered to be the most error ever.

From September 20 to the present, in less than 2 months, Apple had to release 6 patches continuously – a record number .

In addition to many annoying bugs for users, iOS 13 version for developers is also equally headache. There are a lot of small UI changes that Apple does not have specific documents, leading to a series of bugs that are difficult to fix.

So in this article, we will share and learn some noticeable changes when updating UI on iOS 13.

Adaptation Requirement

Starting April, 2020, all iPhone and iPad apps submitted to the App Store will need to be built with the iOS 13 SDK or later. They must also support the all-screen design of iPhone XS Max or the 12.9-inch iPad Pro (3rd Generation), or later.

According to the Apple developer page, starting from April 2020 , all games and apps for iPhone and iPad submitted to the App Store must be compile using the SDK of iOS 13 or higher (i.e. build with Xcode 11.0 or higher. up).

And all have to support full screen design for new screens from the iPhone XS Max or iPad Pro 12.9 inches (3rd generation) or higher.

Apps that are not eligible will be rejected for submission or worse, they may be removed from the App Store .

New Feature Adaptation

Dark Mode

iOS 13 introduces a new Dark Mode, with UIKit supporting APIs, system colors and adaptive images in xcasset .

Learn more about Dark Mode at WWDC 2019 video: Implementing Dark Mode on iOS

If you are not ready to implement Dark Mode, you can disable it in two simple ways:

  • In Info.plist , add the UIUserInterfaceStyle key (Interface Style) and set this value to Light , so your app will always display a bright theme despite having Dark Mode turned on.
  • Using the code, set the property overrideUserInterfaceStyle to .light in any UIView or UIViewController you want.

However, the system’s default alert view popup, such as alert asking for notification, asking for camera permission … will still show black even when Dark Mode is off.

Sign In with Apple

Since iOS 13, Apple has officially launched a quick and convenient way to log into apps and websites: Sign In with Apple .

This is a new feature, so it can only be implemented on Xcode 11. According to the latest App Store App Review Guide :

Apps that exclusively use a third-party or social login service (such as Facebook Login, Google Sign-In, Sign in with Twitter, Sign In with LinkedIn, Login with Amazon, or WeChat Login) to set up or authenticate the user’s primary account with the app must also offer Sign in with Apple as an equivalent option.

If your app uses third-party login services (such as Facebook Login, Google Sign-in, Twitter, LinkedIn, Amazon, Zalo …) to authenticate user accounts, you MUST implement additional. Sign In with Apple option.

The following cases are not required:

  • Apps that use the internal accounts of a company or organization to register and login. (Example: Sun’s WSM app *).
  • Educational, enterprise, and business apps that authenticate users using educational and enterprise accounts that existed before. (Example: Edumall app).
  • The app is supported by the government, the verification organization through the citizen management system, identity, electronic ID … (Example: civil management apps).
  • Apps that are clients for a specific third-party service, require the user to log in to gain access to, use that third party’s content and media. (Example: Netflix app).

With Sign In with Apple , Apple mentioned in News and Updates that:

Starting today, new apps submitted to the App Store can follow these guidelines. Existing apps and app updates must follow them by April 2020.

Starting September 12, 2019, new apps submitted for review to the App Store must comply with the above criteria, as set out in the App Review Guide.

Current apps and updates must be completed by April 2020 .

Adaption API

Private method KVC

In iOS 13, using KVC (Key Value Coding) valueForKey like valueForKey or setValue:forKey to get or set private properties can cause a runtime crash with the following log:

In the above example, the app crashed when we tried to get the private property _searchField of a UISearchBar .

To solve the above problem, we can use the new public property searchTextField :

Or write a method to find view and use:

Some specific crash situations and how to fix using findViewWithClassName method on:

Device Token

When the user provides push notification rights, we receive the device token that identifies that device and uses it to send to the server.

Normally, we just need to convert the device token from NSData to NSString type and remove the extra characters in code.

However, in iOS 13, the above code will fail, giving no results as expected:

How to parse for iOS 13 as follows:

Modal Pop-up Default Style

Starting with iOS 13, when using the presentViewController:animated:completion: to present a view controller, there will be new animation and behavior as follows:

That’s because the default value of the modalPresentationStyle property of UIViewController has been changed. Enum UIModalPresentationStyle has a new value called UIModalPresentationAutomatic . This value with most view controllers is set by UIModalPresentationPageSheet .

Note that, with the old display type, the new view controller is full screen when present, the presenting view controller will call the viewWillDisappear and viewDidDisappear methods. When dismiss presenting view controller, the viewWillAppear and viewDidAppear methods will be called. However, with this new present page sheet, the above methods will not be called.

Solution: Set manually this property value to UIModalPresentationFullScreen .

UISearchBar Background

To remove the black background of the UISearchBar , we usually use the way to browse the view structure of the search bar and remove it from the superview.

However, using this method on iOS 13 will cause a crash:

Solution: set layer.contents of UISearchBarBackground with nil:

UITabBarButton Structure

The view structure of UITabBarButton and many of the default controls in iOS 13 have been changed:

As we can see, the UITabBarSwappableImageView and UITabBarButtonLabel have been wrapped into a new view of type _UIVisualEffectContentView . And when custom tab bar button, we usually browse the subview to find UITabBarSwappableImageView and change.

But because it has been wrapped in another view, it should be noted if you want to set the frame, customize …

UINavigationBar Button Margins

Starting with iOS 11, the UINavigationBar uses auto layout with the left and right buttons at 16 pts (for devices with width <= 375 pts) or 20 pts (for devices with width> 375 pts).

To increase the size of the button’s tap action, we usually define a subclass of the UINavigationBar and override the layoutSubviews method. Then browse through the subview, find _UINavigationBarContentView and set layoutMargins with UIEdgeInsetsZero .

This will also cause iOS 13 to crash again, error message:

Solution: use the frame to expand the dimensions:

There are many other bugs and changes on iOS 13, we will learn in the next article.

Source article: https://juejin.im/post/5d8af88ef265da5b6e0a23ac

Share the news now

Source : Viblo