因爲種種緣由,掘金等第三方平臺博客再也不保證可以同步更新,歡迎移步 GitHub:github.com/kingcos/Per…。謝謝!ios
Create an iOS single view application manually in Swift.git
Date | Notes | Swift | Xcode |
---|---|---|---|
2017-05-26 | CS193p UIApplication | 3.1 | 8.3.2 |
2017-03-28 | 首次提交 | 3.0 | 8.2.1 |
首先要感謝沒故事的卓同窗大大送的泊學會員,在泊學學了幾節課,瞭解了不少不一樣角度的 iOS 開發知識。這篇文章就啓發自其 iOS 101 中的一個純手工的 Single View Application 一文。但本文將更加深刻的敘述了啓動過程,且實現均爲 Swift 3.0。github
本文對應的 Demo 能夠在:github.com/kingcos/Sin… 查看、下載。swift
main.m in Objective-C Single View Application網絡
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
複製代碼
@UIApplicationMain
完成)。Run Loop: An event processing loop that you use to schedule work and coordinate the receipt of incoming events in your app. (From Start Developing iOS Apps (Swift)) Run Loop 是一個事件處理循環,能夠用來在應用中安排任務並定位收到的即將到來的事件。app
AppDelegate.swiftide
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.backgroundColor = UIColor.red
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()
return true
}
}
複製代碼
application(_:didFinishLaunchingWithOptions:)
:該方法在應用啓動進程幾乎完成且將要運行之際調用,所以在其中初始化 window,設置根控制器,並使得其可見。main.swift函數
import UIKit
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)
複製代碼
Code written at global scope is used as the entry point for the program, so you don’t need a main() function. (From The Swift Programming Language (Swift 3.0.1)) 全局範圍書寫的代碼將被看成程序入口,因此不須要 main() 函數。oop
int UIApplicationMain(int argc, char * _Nonnull argv[], NSString *principalClassName, NSString *delegateClassName);
方法。MyApp.swift字體
class MyApp: UIApplication {
override func sendEvent(_ event: UIEvent) {
print("\(#function) - Event: \(event)")
super.sendEvent(event)
}
}
複製代碼
sendEvent(:)
方法即可以監聽到整個應用發送事件。let myApp = UIApplication.shared
複製代碼
// 在其餘 App 中打開 URL
open func open(_ url: URL, options: [String : Any] = [:], completionHandler completion: ((Bool) -> Swift.Void)? = nil)
@available(iOS 3.0, *)
open func canOpenURL(_ url: URL) -> Bool
// 註冊接收推送通知
@available(iOS 8.0, *)
open func registerForRemoteNotifications()
@available(iOS 3.0, *)
open func unregisterForRemoteNotifications()
// 本地或推送的通知由 UNNotification 處理
// 設置後臺取回間隔
@available(iOS 7.0, *)
open func setMinimumBackgroundFetchInterval(_ minimumBackgroundFetchInterval: TimeInterval)
// 一般將其設置爲:
UIApplicationBackgroundFetchIntervalMinimum
// 後臺時請求更長時間
@available(iOS 4.0, *)
open func beginBackgroundTask(expirationHandler handler: (() -> Swift.Void)? = nil) -> UIBackgroundTaskIdentifier
// 不要忘記結束時調用 endBackgroundTask(UIBackgroundTaskIdentifier)
// 狀態來網絡使用 Spinner 顯示開關
var isNetworkActivityIndicatorVisible: Bool
var backgroundTimeRemaining: TimeInterval { get } // 直到暫停
var preferredContentSizeCategory: UIContentSizeCategory { get } // 大字體或小字體
var applicationState: UIApplicationState { get } // 前臺,後臺,已激活
複製代碼