Tuesday, July 24, 2018

Building Your First iOS App

The purpose of this post is just to show you how to create a new application in Xcode and explain the project files automatically created by Xcode.

Open Xcode and create a new Single View Application. Then, open the Project Navigator and you'll see the project files created automatically as shown in the image below.


You can run the app on the simulator or an actual device by:
- Select Product > Destination and then select either a simulator or your device connected to the machine
- Select Product > Build
- Select Product > Run


About Project's Files


1). The AppDelegate.swift file defines AppDelegate class which will be executed by the system when you launch your app. This is done by @UIApplicationMain attribute at the top of the file. The attribute is just like calling @UIApplicationMain function and passing your AppDelegate's class name as the name of the delegate class. It might be simpler to explain it using the java code snippets below.

// declaring function
void uiApplicationMain(Class<?> delegateClass) {
   UIApplicationDelegate delegate = delegateClass.newInstance();
   UIApplication application = new UIApplication(delegate);
   
   // may present the delegate.window object
   // and call appropriate application() methods of the delegate
   application.launch(); 
}

// System calls the function
uiApplicationMain(AppDelegate.class);

The AppDelegate class contains the stub implementation of the delegate methods:
  1. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
  2. func applicationWillResignActive(_ application: UIApplication)
  3. func applicationDidEnterBackground(_ application: UIApplication)
  4. func applicationWillEnterForeground(_ application: UIApplication)
  5. func applicationDidBecomeActive(_ application: UIApplication)
  6. func applicationWillTerminate(_ application: UIApplication)

Those stub methods will be called by the application in response to the events. You can add your own codes there to do any actions responded to the events or leave them empty for default behaviors.

2). Assets.xcassets file lets you define the app icons, images used within the app, and other resources. See this post for how to define the app icons.

3). Main.storyboard file is managed by Xcode to make it simple for developers to design the interface of their apps. They just drag and drop the controls needed. Then, Xcode will create necessary Swift or Objective-C classes, configuration and resource files behind the scene. The storyboard represents the screen of content in your app.

4). ViewController.swift file contains the ViewController class corresponding to the view controller you designed in the Main.storyboard file (or Interface  Builder). They're linked together by Class setting under Custom Class section in the Property Inspector. The image below shows how their link is defined.


The ViewController class let you programmatically manage or customize the behavior of your view controller. You can add more view controllers in the Interface Builder and define their corresponding view controller classes in Swift files separately. Each view controller has its own view while the view might have many sub views such as textboxes, buttons, and labels.

5) Info.plist (Information property list file) is the way an app provides its metadata to the system. It is mandatory for the bundled executables (apps, plug-ins, and frameworks). It's encoded using UTF-8 and is structured as XML. See here for more details.



Reference
https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/BuildABasicUI.html#//apple_ref/doc/uid/TP40015214-CH5-SW1



No comments:

Post a Comment