ios UIApplication简单使用

阅读更多

1,一个程序只有一个UIApplication

//获取
UIApplication *app = [UIApplication sharedApplication];

//错误获取
UIApplication *app = [[UIApplication alloc] init];
//UIApplication是单例的,如果试图在程序中新建一个UIApplication对象,那么将提示报错

 2,使用获取的UIApplication,可以进行一些应用级别操作(app图标的提醒框,联网状态显示,打电话,打开网页,控制状态栏

//app图标的提醒框(图标上提示有信息的小红点)
app.applocationIconBadgeNumber = 10;  //10为提醒数字

//实现上述方法,必须注册用户通知:

//创建用户通知
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
//注册用户的通知
[app registerUserNotificationSettings: settings];

 

//设置联网状态
app.networkActivityIndicatorVisible = YES;

 

//打开网页
NSURL *url = [NSURL URLWithString: @"http://www.baidu.com"];
[[UIApplication sharedApplication] openURL: url];

//协议头 :http,https,file,tel

 

//控制状态栏

//ios7以后,状态栏默认由控制器决定,所以:

//  (1)将info.plist文件的View controller-based status bar appearance设置为NO时,在控制器中对状态栏的控制都将无效

//隐藏状态栏
[app setStatusBarHidden: YES]; 
  
//设置状态栏的字体颜色模式
[app setStatusBarStyle:UIStatusBarStyleLightContent];

//设置隐藏状态栏动画
[app setStatusBarHidden: YES withAnimation: UIStatusBarAnimationSlide];





//  (2)将info.plist文件的View controller-based status bar appearance设置为YES时,UIApplication修改控制栏无效,需要在控制器(ViewController)中添加方法

//是否隐藏状态栏
- (BOOL)prefersStatusBarHidden {
          return YES;
}
//设置状态栏字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
          return UIStatusBarStyleLightContent;
}

 3,UIApplicationDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
       //程序启动完成时调用
}

- (void)applicationWillResignActive:(UIApplication *)application {
       //程序失去焦点时调用  意味着不能和用户交互
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
       //程序进入后台时调用
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
       //程序即将进入前台时调用
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
       //程序获取焦点时调用  可以和用户交互
}

- (void)applicationWillTerminate:(UIApplication *)application {
       //程序被关闭时调用  此方法无法验证
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
       //程序接收到内存警告时调用
}


/*
程序运行时,调用顺序为:
     1  application:didFinishLaunchingWithOptions:
     2  applicationDidBecomeActive:
先进入程序,然后获取焦点,此时用户可以进行点击等交互操作
*/

/*
按压Home键:
     1  applicationWillResignActive:
     2  applicationDidEnterBackground:
先失去焦点,不能交互,然后程序进入后台
*/

/*
再次进入程序:
     1  applicationWillEnterForeground:
     2  applicationDidBecomeActive:
先调用即将进入前台方法,之后进入程序,获取焦点
*/

 

你可能感兴趣的:(ios)