Introduction to iOS Development

IOS is a mobile operating system developed by Apple. Apple first unveiled the system (originally called iPhone Runs OS X) at the Macworld conference on January 9, 2007. Originally designed for the iPhone (hence the name iPhone OS), the system has since been adopted for the iPod Touch, iPad, and Apple TV (hence the name iOS announced at WWDC 2010).

History of iOS

A new version will be released each year from 2007 to 2020, the latest being iOS 14.

Apple website and developer website

  • www.apple.com
  • developer.apple.com

The development of hardware

  • Apple: An iMac or MacBook or Mac Mini is connected to a monitor.
  • You can also install a black Apple system on a regular computer (not recommended), which is complex and unstable to install.

Develop software

IOS uses the Xcode tool for development. You can search for it in the App Store or download it from the Apple Developer website (this tutorial is based on Xcode 12).

Development of language

Swift or Objective-C (this tutorial is based on Swift 5.x).

Developer account

  • $99 per person per year
  • Company $99 / year
  • Enterprise $299 / year

The iOS system has four layers

  • Core OS is the most Core system layer, including memory management, file system, hardware management, power management, security management and other content.
  • Core Services include a variety of Core Services for App use, such as network, thread, location, and so on.
  • Media layer mainly includes the processing of various Media files, through it we can use a variety of Media files in the application, audio and video recording, graphics drawing, and the production of basic animation effects.
  • The Cocoa Touch layer provides a variety of useful frameworks for application development, mostly related to the user interface, which is responsible for the user’s Touch interaction on an iOS device, as well as a number of other key functions.

Create your first iOS project

The project file

The App Settings

  • The application of
    • Item – > Target – > General – > Display Name
  • Application icon

  • Startup screen LaunchScreen
    • Launch screens affect resolution and can be removed by LaunchScreen.

The simulator

##App initialization process

AppDelegate

  • Program entrance@main(Previously @UIApplicationMain for iOS 14).
  • didFinishLaunchingWithOptionsStartup method.
  • Entrusted toSceneDelegate.

SceneDelegate

  • window
  • The life cycle
class SceneDelegate: UIResponder.UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene.willConnectTo session: UISceneSession.options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return}}func sceneDidDisconnect(_ scene: UIScene){}func sceneDidBecomeActive(_ scene: UIScene){}func sceneWillResignActive(_ scene: UIScene){}func sceneWillEnterForeground(_ scene: UIScene){}func sceneDidEnterBackground(_ scene: UIScene){}}Copy the code

Initialize the

Main Storyboard

  1. Parse info.plist to obtainApplication Scene ManifestTo findMain Storyboard file base nameSet the Storyboard.
  2. @main
  3. AppDelegate – > SceneDelegate.
  4. Displays the controller initialized in main.storyboard.
Storyboard is introduced
  • Container concept.
  • Is Initial View Controller (arrow move).
  • The interface in StoryboardViewController.swiftAn interface is associated with a class file.

Pure code

  1. Delete info.plistMain Storyboard file base nameandApplication Scene ManifestThe layer of theStoryboard Name.
  2. Parse info.plist and find that the Main Storyboard is not set.
  3. @ main.
  4. AppDelegate – > SceneDelegate
  5. In SceneDelegatewillConnectToSessionInitializes UIWindow in pure code and sets the first controller to display.Window -- > rootViewController -- > UIViewController -- > UIView.

UIView and UIViewController

First impression

In iOS development, an interface is a UIViewController, and what’s displayed on the interface is a UIView.

The relationship between UIView and UIViewController

UIViewController by default has a UIView that’s the size of the screen, and UIViewController manages its life cycle. All UI controls that are placed on the interface are placed on top of the UIView of the UIViewController, which can be retrieved in the UIViewController’s code via the self.view property. Other UIViews (and their subclasses) under development are placed on this view.

UIView lifecycle function in UIViewController

  • viewDidLoad: View Completes memory loading.
  • viewWillAppear: View is about to display.
  • viewDidAppear: View Full display.
  • viewWillDisappear: View is about to disappear.
  • viewDidDisappear: View completely disappears.

UIView

IOS coordinate system

  • Two dimensional coordinate system

  • The z axis

Get screen size

UIScreen.main.bounds
Copy the code

UIView adds a View to UIViewController

The container concept of UIView

  • You can put other UIViews in UIViews.
  • Hierarchy of UIViews (horizontal vs. inclusive).

UIView common properties

  • Background color: background color.
  • Frame: Coordinates and size relative to the superview.
  • Bounds: Coordinates and size relative to itself, so that x and y of bounds are always 0.
  • Center: coordinates of the center point relative to the superview.
  • Alpha: Transparency (0.0 ~ 1.0).
  • Tag: indicates the tag (Int type, default = 0). If set, it can passView.viewWithTagMethod to get the view.
  • Title: Indicates the title.
  • Superview: indicates the superview.
  • Subviews: All subviews.
Consider: What are the possibilities for a control to be invisible?
  1. The width or the height is actually 0.
  1. It’s out of place (e.g. it’s a negative number or a huge number, it’s off the screen).
  2. Hidden = = true.
  3. Alpha < = 0.01.
  4. No background color is set, no content is set.
  5. Maybe the text color is the same as the background color.

UIView common methods

  • AddSubview: Adds a view to the superview.
  • RemoveFromSuperview: Remove the view from the superview (touch method explained in advance).
  • ViewWithTag: Gets the view based on the tag value.
  • InsertSubview: inserts a view above/below the specified view.
  • BringSubviewToFront: Move the view to the top.
  • ExchangeSubviewAtIndex: Swaps views for two locations.

Demo UIView and UIViewController

  • Storyboard
    • Notice the view hierarchy in the left panel.
    • Description of the important properties of the property panel.
  • Pure code
    • Corresponds to the properties in the Storyboard properties panel.

Pay attention to where the code is written. There are often people who write code in the wrong place and cause errors.

@ IBOutlet and @ IBAction

The introduction of

How do I get a custom UIView in my Storyboard in my code? There are currently two ways:

  • Through the tag attribute: If you have too many views, or if you forget to set the tag, or if you set the same tag, you will have problems.
  • With the subviews property: Once there are too many views, it is difficult to get an exact View from an array.

Is there something more user-friendly, intuitive, convenient, and efficient? The answer is yes, and that’s @ibOutlet and @ibAction.

@IBOutlet

  • Attributes written in a class.
  • I’m going to associate with the UIView in my Storyboard.
  • The property in this case is the UIView in the Storyboard
  • All UI controls can drag and drop @ibOutlet.

@IBAction

  • Methods written in a class.
  • To the events generated by the UIView in the Storyboard.
  • The method is called when the corresponding event occurs.
  • Only UI controls that inherit from UIControl can drag and drop @ibAction.

Forward and reverse drag lines

  • Drag the line down: Drag the line to the class file through the Storyboard.
  • Drag the line backwards: Drag the line to the Storyboard through the class file.

Premise of connection

The UIViewController in the Storyboard is associated with the class that you want to drag.

Classic wiring error

  • The UIView line removes @IbOutlet or @IbAction from the class.
  • When UIView is wired, change the name of @IbOutlet or @IbAction in the class.
  • The error message
  1. @ IBAction:unrecognized selector sent to instance
  2. @ IBOutlet:this class is not key value coding-compliant for the key XXX