Cocoa Touch广泛地使用了委托,委托是负责为其他对象处理特定任务的对象。通过应用程序委托,能够在某些预定义时间点为UIApplication类做一些工作。
每个iOS应用程序有且仅有一个UIApplication实例,它负责应用程序的运行循环,以及处理应用程序级的功能。
在应用程序执行过程中的某些特定时间点,UIApplication将会调用特定的委托方法。例如,如果需要在程序退出时触发一段代码,可以在应用程序委托中实现applicationWillTerminate:方法。
查看BIDAppDelegate.h,查看应用程序委托的头文件。代码如下所示:
#import <UIKit/UIKit.h>
@class BIDViewController;
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BIDViewController *viewController;
@end
其中有一行代码需要特别注意:
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>
尖括号里面的内容,表示这个类遵循UIApplicationDelegate协议。
UIApplicationDelegate
协议简介
The UIApplicationDelegate protocol declares methods that are implemented by the delegate of the singleton UIApplication object. These methods provide you with information about key events in an application’s execution such as when it finished launching, when it is about to be terminated, when memory is low, and when important changes occur. Implementing these methods gives you a chance to respond to these system events and respond appropriately.
可选方法
Monitoring App State Changes
– application:willFinishLaunchingWithOptions:
– application:didFinishLaunchingWithOptions:
– applicationDidBecomeActive:
– applicationWillResignActive:
– applicationDidEnterBackground:
– applicationWillEnterForeground:
– applicationWillTerminate:
– applicationDidFinishLaunching:
Managing App State Restoration
– application:shouldSaveApplicationState:
– application:shouldRestoreApplicationState:
– application:viewControllerWithRestorationIdentifierPath:coder:
– application:willEncodeRestorableStateWithCoder:
– application:didDecodeRestorableStateWithCoder:
Providing a Window for Storyboarding
window property
Managing the Default Interface Orientations
– application:supportedInterfaceOrientationsForWindow:
Opening a URL Resource
– application:openURL:sourceApplication:annotation:
– application:handleOpenURL:
Managing Status Bar Changes
– application:willChangeStatusBarOrientation:duration:
– application:didChangeStatusBarOrientation:
– application:willChangeStatusBarFrame:
– application:didChangeStatusBarFrame:
Responding to System Notifications
– applicationDidReceiveMemoryWarning:
– applicationSignificantTimeChange:
Handling Remote Notifications
– application:didReceiveRemoteNotification:
– application:didRegisterForRemoteNotificationsWithDeviceToken:
– application:didFailToRegisterForRemoteNotificationsWithError:
Handling Local Notifications
– application:didReceiveLocalNotification:
Responding to Content Protection Changes
– applicationProtectedDataWillBecomeUnavailable:
– applicationProtectedDataDidBecomeAvailable:
再查看BIDApplicationDelegate.m,应用程序委托的实现。代码如下所示:
#import "BIDAppDelegate.h"
#import "BIDViewController.h"
@implementation BIDAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
可以看到,应用程序委托实现了文档中列出的一些协议方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
首先创建一个窗口,然后通过夹在nib文件创建一个控制器类的实例。之后就把控制器的视图作为子视图添加到应用程序窗口中,这样视图就可以显示出来了。我们设计的视图就是以这种方式显示给用户的。
官方指南
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/
分享到:
相关推荐
The UIApplicationDelegate protocol declares methods that are implemented by the delegate of the singleton UIApplication object. These methods provide you with information about key events in an ...
UIApplicationDelegate提供了两种方法,可在用户打开应用程序时用来运行代码: applicationWillEnterForeground和applicationDidBecomeActive 。 但是以下两种方法都不适合这种情况: 首次启动应用程序时未调用...
为iOS 11编写的Uber克隆应用程序。模仿Uber的许多功能,以了解源代码以及如何实现各种功能。 了解与Firebase服务器进行交互,通过通知和地理地图功能在用户和驱动程序之间进行通信。 安装和构建应用程序的步骤 在...
添加“应用程序传输安全设置”,并为此添加“允许任意加载”,将其设置为true 对AppDelegate.swift文件进行以下更改:*添加导入TVMLKit。 *将类声明更改为AppDelegate类:UIResponder,UIApplicationDelegate,...
但是,仍然可以通过将新的跟踪属性设置为网站而不是移动应用程序来使用此库(请参阅 )。 建立 可与Carthage&CocoaPods一起使用,或仅将GoogleReporter.swift放入您的项目中。 从您的AppDelegate的...
在Swift中实现Model View Controller模式的iOS应用程序模板。 iOS具有其特点:应用程序界面由单独的屏幕(场景)组成,这就是为什么它具有情节提要功能的原因。 有一个UIViewController类看起来像MVC模式中的控制器...
SQLite.viewer 一个优雅的库,用于在iOS应用程序中查看,编辑或调试sqlite数据库。 受启发。特征列出可用的数据库列表表运行原始查询去做 插入行 编辑行 删除行 新的用户界面和用户体验用法在AppDelegate.swift文件...
iOS 12 CarPlay示例在iOS 12及更高版本中使用CarPlay框架的... 连接是通过从UIApplicationDelegate继承的CPApplicationDelegate委托完成的,因此您通常将其作为主app-delegate类的一部分进行处理。 连接后,您可以像使
在应用程序启动期间在UIApplicationDelegate中设置核心数据存储: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [CDMDataManager ...
CVKHierarchySearcher 是一个小型实用程序类,用于遍历 iOS 应用程序的视图控制器层次结构并检索经常需要的最顶层视图控制器。 现在它提供了最顶层、最顶层的非模态和最顶层的UINavigationController 。 用法 使用...
###Airplay 故事板 演示如何使用带有故事板的外部显示器(Airplay 等)。 每个屏幕都有一个窗口,每个窗口都有一个根视图控制器。 当加载故事板时,任何设置... 此时,应用程序可以决定哪些窗口应该附加到哪些屏幕上。
Guppy是一个专注于HTTP请求的日志记录工具,可以轻松地将其插入任何iOS应用程序中。 Guppy将拦截并记录从应用发送的每个网络请求。 您可以轻松访问任何Guppy日志并将其与您团队的其他成员共享。 用法 在模拟器中时...
最近在工作中遇到了这么一个需求:如何实现 Android 应用前后台切换的监听?下面来一起看看详细的介绍: iOS 内边是可以实现的,AppDelegate 给了一个回调监听: @UIApplicationMain class AppDelegate: ...
Seru的设计轻巧快捷,适合所有类型的应用程序。 使用示例:用1行代码Seru()设置Core Data,然后在后台插入对象。 let seru = Seru ()seru. performBackgroundSave ({ context in let person = Person ( managed...
需要在合同中签名的应用程序,瞧! 不再疯狂, Contract让你更容易签下你的灵魂。用法 import UIKit@UIApplicationMainclass AppDelegate : UIResponder , UIApplicationDelegate , ...
6 UIApplication and UIApplicationDelegate 7 UIView and UIViewController 8 UITabBar and UITabBarController 9 UINavigationBar and UINavigationController 10 Tables Using UITableView and ...
我们在集成sdk前需要到微信开放平台注册自己的应用,然后拿到对应的key,其实这个一般都是后台来做,只要问后台拿到key就行 使用详情参照 微信支付集成SDK介绍cocopods集成SDK和手动集成SDK 一、cocopods集成SDK 1....
@interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end // // AppDelegate.m // Hello World_Code // #import "AppDelegate.h" @...
**[1412:60b] You've implemented -[<UIApplicationDelegate> application:performFetchWithCompletionHandler:], but you still need to add fetch to the list of your supported UIBackgroundModes in your Info....