因为种种缘由,掘金等第三方平台博客再也不保证可以同步更新,欢迎移步 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 } // 前台,后台,已激活
复制代码