一 UIApplication简介
在前面讲 中介绍UIApplicationMain作用时,对UIApplication 简单介绍过。UIApplication对象是应用程序的象征,每一个应用都有自己的UIApplication对象(单例),iOS程序启动后创建的第一个对象就是UIApplication对象,利用UIApplication对象,能进行一些应用级别的操作。
UIApplication的核心作用是提供了iOS程序运行期间的控制和协作工作,用于处理用户事件,它会起一个队列,把所有用户事件都放入队列,逐个处理,在处理的时候,它会发送当前事件 到一个合适的处理事件的目标控件。另外UIApplication实例还维护在本应用中打开的UIWindow实例列表,可以接触应用中的任何UIView对象。UIApplication实例会被赋予一个代理对象,以处理应用程序的生命周期事件(比如程序启动和关闭)、系统事件(比如来电、记事项警告)等等。
作用:
处理用户事件
维护UIWindow实例
代理对象处理运用程序的生命周期事件
二 获取UIApplication对象
[UIApplication sharedApplication]
三 UIApplication的常用属性
3.1 设置应用程序图标右上角的红色提醒数字
@property(nonatomic) NSInteger applicationIconBadgeNumber; //设置运用程序图标icon上的数字图标,缺省为0[UIApplication sharedApplication].applicationIconBadgeNumber = 4;
见代码运行结果配图
3.2 设置联网指示器
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;//显示联网标记[UIApplication sharedApplication].networkActivityIndicatorVisible =YES
3.3 设置摇动手势的时候,是否支持redo,undo操作
@property(nonatomic) BOOL applicationSupportsShakeToEdit NS_AVAILABLE_IOS(3_0);//设置摇动手势的时候,是否支持redo,undo操作 [UIApplication sharedApplication].applicationSupportsShakeToEdit =YES;
3.4 判断程序运行状态
@property(nonatomic,readonly) UIApplicationState applicationState NS_AVAILABLE_IOS(4_0);//applicationState 对应状态typedef NS_ENUM(NSInteger, UIApplicationState) { UIApplicationStateActive, UIApplicationStateInactive, UIApplicationStateBackground} NS_ENUM_AVAILABLE_IOS(4_0);// 判断程序运行状态if([UIApplication sharedApplication].applicationState ==UIApplicationStateInactive){ NSLog(@"程序在运行状态");}
3.5.阻止屏幕变暗进入休眠状态
@property(nonatomic,getter=isIdleTimerDisabled) BOOL idleTimerDisabled; // default is NO //阻止屏幕变暗,慎重使用,缺省为no 2.0 慎重使用本功能,因为非常耗电[UIApplication sharedApplication].idleTimerDisabled =YES;
四 UIApplication openURL功能
UIApplication openURL功能强大,通过openURL可用实现很多功能
- (BOOL)openURL:(NSURL*)url;
openURL:方法的部分功能有
4.1 在Google map上显示一个地址
NSString *addressText =@"40.099871,116.288352&iwloc=A&hl=zh-CN"; //北京昌平 addressText = [addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlText]];
4.2 发送邮件
NSString *recipients =@"mailto:leigm2@163.com&subject=Hello";NSString *body =@"&body=~test!";NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplication sharedApplication]openURL:[NSURL URLWithString:email]];
4.3 打电话
//打电话[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://1860016600x"]];
4.4 发送短信
//10086[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10086"]];
4.5 打开网址
//代开网站[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://my.oschina.net/u/1032974"]];
见代码运行结果配图
五 管理状态栏
iOS7后,系统提供了2种管理状态栏是方法
5.1 通过UIViewController管理
每一个UIViewController都可以拥有自己不同的状态栏,在iOS7中,默认情况下,状态栏都是由UIViewController管理的,UIViewController实现下列方法就可以轻松管理状态栏的可见性和样式
5.1.1 状态栏的样式
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault//重写 preferredStatusBarStyle 方法-(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}//对应 UIStatusBarStyle 值typedef NS_ENUM(NSInteger, UIStatusBarStyle) { UIStatusBarStyleDefault = 0, // Dark content, for use on light backgrounds UIStatusBarStyleLightContent NS_ENUM_AVAILABLE_IOS(7_0) = 1, // Light content, for use on dark backgrounds UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1, UIStatusBarStyleBlackOpaque NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,};
5.1.2 状态栏的可见性
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO//重写 prefersStatusBarHidden 方法- (BOOL)prefersStatusBarHidden{ return YES;}
5.2 通过UIApplication管理(一个应用程序的状态栏都由它统一管理)
利用UIApplication来管理状态栏,首先得修改Info.plist的设置
六 UIApplication和delegate
移动操作系统都有个缺点,app很容易受到打扰。当一个来电或者锁屏会导致app进入后台甚至被终止,还有很多其它类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate代理来处理这些系统事件。
例如 applicationDidReceiveMemoryWarning()方法,iPhone内存有限,如果为应用程序分配了太多内存操作系统会终止应用程序的运行,但在终止之前操作系统会通过先调用委托类的 applicationDidReceiveMemoryWarning()方法警告应用程序,在UIApplication接收到这个事件后它会传递给 委托类的applicationDidReceiveMemoryWarning()方法,委托类在这个方法内可以进行释放内存的操作以防止操作系统强制 终止应用程序的运行。
//0 程序加载完毕- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions //1 当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话了- (void)applicationWillResignActive:(UIApplication *)application//2 当应用程序入活动状态执行,这个刚好跟上面那个方法相反- (void)applicationDidBecomeActive:(UIApplication *)application//3 当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可- (void)applicationDidEnterBackground:(UIApplication *)application//4 当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。- (void)applicationWillEnterForeground:(UIApplication *)application//5 当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值。- (void)applicationWillTerminate:(UIApplication *)application//6 iPhone设备只有有限的内存,如果为应用程序分配了太多内存操作系统会终止应用程序的运行,在终止前会执行这个方法,通常可以在这里进行内存清理工作防止程序被终止- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application//7 当系统时间发生改变时执行- (void)applicationSignificantTimeChange:(UIApplication*)application//8 当程序载入后执行- (void)applicationDidFinishLaunching:(UIApplication*)application//9 当StatusBar框将要变化时执行- (void)application:(UIApplication)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame//10 当StatusBar框方向将要变化时执行- (void)application:(UIApplication*)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration//11当通过url执行- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url//12 当StatusBar框方向变化完成后执行- (void)application:(UIApplication*)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation//13 当StatusBar框变化完成后执行- (void)application:(UIApplication*)application didChangeSetStatusBarFrame:(CGRect)oldStatusBarFrame
七 UIApplicationMain补充
main函数中执行了一个UIApplicationMain这个函数
int UIApplicationMain(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 Run loop(事件循环),进行事件的处理(首先会在程序完毕后调用delegate对象的application:didFinishLaunchingWithOptions:方法)程序正常退出时UIApplicationMain函数才返回。
八 演示
8.1代码
//// ViewController.m// UIApplication//#import "ViewController.h"@interface ViewController ()/** * UIImageView */@property(nonatomic,strong)UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad { //1.加载图片 [self.view addSubview:self.imageView]; //2.管理状态栏 //2.1 状态栏样式// [self preferredStatusBarStyle]; //2.2 状态栏隐藏 [self prefersStatusBarHidden]; //3 openURL //3.1 map// [self googleMapsPositioningLocation]; //3.2 发送邮件// [self sendEmail]; //3.3 打电话// [self callPhone]; //3.4 发送短信// [self sendEmail]; //3.5 打开一个WEB地址 [self openWebSite]; [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://my.oschina.net/u/1032974"]]; [super viewDidLoad];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma openURL方法-(void)googleMapsPositioningLocation{ NSString *addressText =@"40.099871,116.288352&iwloc=A&hl=zh-CN"; //北京昌平 addressText = [addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText]; [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlText]];}-(void)sendEmail{ NSString *recipients =@"mailto:leigm2@163.com&subject=Hello"; NSString *body =@"&body=~test!"; NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication]openURL:[NSURL URLWithString:email]];}-(void)callPhone{ [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://1860016600x"]];}-(void)sendSMS{ [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10086"]];}-(void)openWebSite{ [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://my.oschina.net/u/1032974"]];}#pragma UIApplication属性/** * 常用 UIApplication 属性 */-(void)commonlyUsedUIApplicationProperties{ //获取 UIApplication 对象 //UIApplication *application = [UIApplication sharedApplication]; /** * 1.UIApplication 常用属性 */ // 1 设置运用程序图标icon上的数字图标,缺省为0 [UIApplication sharedApplication].applicationIconBadgeNumber = 16; // 2 显示联网标记 [UIApplication sharedApplication].networkActivityIndicatorVisible =YES; // 3 设置摇动手势的时候,是否支持redo,undo操作 [UIApplication sharedApplication].applicationSupportsShakeToEdit =YES; // 4 判断程序运行状态 if([UIApplication sharedApplication].applicationState ==UIApplicationStateInactive) { NSLog(@"程序在运行状态"); } // 5 阻止屏幕变暗进入休眠状态 [UIApplication sharedApplication].idleTimerDisabled =YES; }#pragma mark - 隐藏状态栏//- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO/** * 状态栏不可见 * * @return <#return value description#> */- (BOOL)prefersStatusBarHidden{ return YES;}//- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault/** * 状态栏样式 * * @return <#return value description#> */-(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}/** * 懒加载 * * @return UIImageView */- (UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc] init]; // 设置图片范围 UIImage *image = [UIImage imageNamed:@"scroll.jpg"]; CGFloat imageH = image.size.height; CGFloat imageW = image.size.width; CGFloat imageX = 0; CGFloat imageY = 0; _imageView.frame = CGRectMake(imageX, imageY, imageW, imageH); _imageView.image = image; } return _imageView;}@end
8.2 结果