一、ios程序启动原理
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:方法)
(1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序。
(2)每一个应用都有自己的UIApplication对象,而且是单例的,如果试图在程序中新建一个UIApplication对象,那么将报错提示。
(3)通过[UIApplicationsharedApplication]可以获得这个单例对象
(4) 一个iOS程序启动后创建的第一个对象就是UIApplication对象,且只有一个(通过代码获取两个UIApplication对象,打印地址可以看出地址是相同的)。
(5)利用UIApplication对象,能进行一些应用级别的操作
2.应用级别的操作示例:
1)设置应用程序图标右上角的红色提醒数字(如QQ消息的时候,图标上面会显示1,2,3条新信息等。)
@property(nonatomic) NSInteger applicationIconBadgeNumber;
代码实现和效果:
- (void)viewDidLoad { [super viewDidLoad]; //创建并添加一个按钮 UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 60, 30)]; [btn setTitle:@"按钮" forState:UIControlStateNormal]; [btn setBackgroundColor:[UIColor brownColor]]; [btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } -(void)onClick { NSLog(@"按钮点击事件"); //错误,只能有一个唯一的UIApplication对象,不能再进行创建 // UIApplication *app=[[UIApplication alloc]init]; //通过sharedApplication获取该程序的UIApplication对象 UIApplication *app=[UIApplication sharedApplication]; app.applicationIconBadgeNumber=123; }
2)设置联网指示器的可见性
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
代码和效果:
//设置指示器的联网动画 app.networkActivityIndicatorVisible=YES;
3)管理状态栏
从iOS7开始,系统提供了2种管理状态栏的方式
a.通过UIViewController管理(每一个UIViewController都可以拥有自己不同的状态栏).
在iOS7中,默认情况下,状态栏都是由UIViewController管理的,UIViewController实现下列方法就可以轻松管理状态栏的可见性和样式
状态栏的样式 - (UIStatusBarStyle)preferredStatusBarStyle;
状态栏的可见性 -(BOOL)prefersStatusBarHidden;
#pragma mark-设置状态栏的样式 -(UIStatusBarStyle)preferredStatusBarStyle { //设置为白色 //return UIStatusBarStyleLightContent; //默认为黑色 return UIStatusBarStyleDefault; } #pragma mark-设置状态栏是否隐藏(否) -(BOOL)prefersStatusBarHidden { return NO; }
b.通过UIApplication管理(一个应用程序的状态栏都由它统一管理)
如果想利用UIApplication来管理状态栏,首先得修改Info.plist的设置
代码:
//通过sharedApplication获取该程序的UIApplication对象 UIApplication *app=[UIApplication sharedApplication]; app.applicationIconBadgeNumber=123; //设置指示器的联网动画 app.networkActivityIndicatorVisible=YES; //设置状态栏的样式 //app.statusBarStyle=UIStatusBarStyleDefault;//默认(黑色) //设置为白色+动画效果 [app setStatusBarStyle:UIStatusBarStyleLightContent animated:YES]; //设置状态栏是否隐藏 app.statusBarHidden=YES; //设置状态栏是否隐藏+动画效果 [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
c.补充
4)openURL:方法
UIApplication有个功能十分强大的openURL:方法
- (BOOL)openURL:(NSURL*)url;
openURL:方法的部分功能有
1.简单说明
所有的移动操作系统都有个致命的缺点:app很容易受到打扰。比如一个来电或者锁屏会导致app进入后台甚至被终止。
还有很多其它类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate代理来处理这些系统事件。
// AppDelegate.m
// myWebViewSample
//
// Created by 葛志华 on 15/9/20.
// Copyright © 2015 年 gezhihua. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
// 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.
}
- ( 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:.
}