1、什么是UIApplication
2、UIApplication的常用属性
@property(nonatomic)NSIntegerapplicationIconBadgeNumber;
@property(nonatomic,getter=isNetworkActivityIndicatorVisible)BOOL networkActivityIndicatorVisible;
3、iOS7中的状态栏
-(UIStatusBarStyle)preferredStatusBarStyle;
- (BOOL)prefersStatusBarHidden;
4、openURL:
-(BOOL)openURL:(NSURL*)url;
UIApplication *app = [UIApplicationsharedApplication];
[app openURL:[NSURLURLWithString:@"tel://10086"]];
[app openURL:[NSURLURLWithString:@"sms://10086"]];
[app openURL:[NSURLURLWithString:@"mailto://12345@qq.com"]];
[app openURL:[NSURLURLWithString:@"http://ios.itcast.cn"]];
5、上述代码
- (IBAction)changeAppNum {
UIApplication *app = [UIApplication sharedApplication];
// [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
// app.statusBarHidden = YES;
// app.statusBarStyle = UIStatusBarStyleLightContent;
[app setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
// 0代表清除图标右上角的数字
// app.applicationIconBadgeNumber = 0;
// 设置显示联网状态
// app.networkActivityIndicatorVisible = YES;
// URL : 一个资源的唯一路径
// URL的组成 == 协议头://主机域名/路径
// 网络资源URL的组成 == http://www.baidu.com/1.png
// 本地文件资源URL的组成 == file:///Users/apple/Desktop/1.png
// [app openURL:[NSURL URLWithString:@"http://ios.itcast.cn"]];
// 打电话
// [app openURL:[NSURL URLWithString:@"tel://10086"]];
}
6、UIApplication和delegate
7、UIApplicationDelegate中的方法,我们可以在这些代理方法中做一些必要的操作
//
// MJAppDelegate.m
// 06-UIApplication
//
// Created by apple on 14-4-7.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJAppDelegate.h"
@implementation MJAppDelegate
/**
* app启动完毕后就会调用
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"%@", self.window);
NSLog(@"didFinishLaunchingWithOptions");
// Override point for customization after application launch.
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.
}
/**
* app进入后台的时候调用
*
* 一般在这里保存应用的数据(游戏数据,比如暂停游戏)
*/
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"applicationDidEnterBackground");
// 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.
}
/*
*app进入前台调用
*/
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"applicationWillEnterForeground");
// 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)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"applicationDidReceiveMemoryWarning");
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
8、iOS程序的启动过程
9、UIApplicationMain
main函数中执行了一个UIApplicationMain这个函数
intUIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
argc、argv:直接传递给UIApplicationMain进行相关处理即可
principalClassName:指定应用程序类名(app的象征),该类必须是UIApplication(或子类)。如果为nil,则用UIApplication类作为默认值
delegateClassName:指定应用程序的代理类,该类必须遵守UIApplicationDelegate协议
UIApplicationMain函数会根据principalClassName创建UIApplication对象,根据delegateClassName创建一个delegate对象,并将该delegate对象赋值给UIApplication对象中的delegate属性
接着会建立应用程序的Main Runloop(事件循环),进行事件的处理(首先会在程序完毕后调用delegate对象的application:didFinishLaunchingWithOptions:方法)
程序正常退出时UIApplicationMain函数才返回
#import <UIKit/UIKit.h>
#import "YYAppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
// return UIApplicationMain(argc, argv, nil, NSStringFromClass([YYAppDelegate class]));
// return UIApplicationMain(argc, argv, @"UIApplication", NSStringFromClass([YYAppDelegate class]));
/*
argc: 系统或者用户传入的参数个数
argv: 系统或者用户传入的实际参数
1.根据传入的第三个参数创建UIApplication对象
2.根据传入的第四个产生创建UIApplication对象的代理
3.设置刚刚创建出来的代理对象为UIApplication的代理
4.开启一个事件循环
*/
return UIApplicationMain(argc, argv, @"UIApplication", @"YYAppDelegate");
}
}
系统入口的代码和参数说明:
10、UIWindow介绍
在本应用中打开的UIWindow列表,这样就可以接触应用中的任何一个UIView对象
(平时输入文字弹出的键盘,就处在一个新的UIWindow中)
用来接收键盘以及非触摸类的消息事件的UIWindow,而且程序中每个时刻只能有一个UIWindow是keyWindow。如果某个UIWindow内部的文本框不能输入文字,可能是因为这个UIWindow不是keyWindow
获得某个UIView所在的UIWindow
/**
* 程序启动完毕就会调用一次
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 1.创建window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 2.设置window的背景色
self.window.backgroundColor = [UIColor whiteColor];
MjOneViewController *one = [[MjOneViewController alloc] init];
// [self.window addSubview:one.view];
self.window.rootViewController = one;
// 3.显示window
[self.window makeKeyAndVisible];
return YES;
}
11、程序启动完整步骤
程序启动的完整过程
1.main函数
2.UIApplicationMain
* 创建UIApplication对象
* 创建UIApplication的delegate对象
3.delegate对象开始处理(监听)系统事件(没有storyboard)
* 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法
* 在application:didFinishLaunchingWithOptions:中创建UIWindow
* 创建和设置UIWindow的rootViewController
* 显示窗口
3.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard)
* 创建UIWindow
* 创建和设置UIWindow的rootViewController
* 显示窗口
12、四大对象关系图