第十二章 UIApplication

UIApplication对象是一个应用的象征,而且是单例的,能进行一些应用级别的操作

  • 设置应用图标,桌面上图标唯独消息数量。

例如qq未读消息,在桌面上的显示

  • 设置当打开一个应用的时候的联网状态。

例如打开一个程序,运行商旁边的一个小菊花的刷新状态

  • 控制我们的状态栏

玩游戏的时候隐藏状态栏

// 当我们需要设置的时候首先要进入这个类查看头文件
    // 设置icon背景提示
    UIApplication * application = [UIApplication sharedApplication];
    // 创建用户通知
    UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    // 注册用户通知
    [application registerUserNotificationSettings:settings];
    application.applicationIconBadgeNumber = 10;
    // URL:资源路径
    // URL: 协议头://路径  协议头(http-打开网页 https-打开网页 file-打开文件 tel-打开电话)
    UIApplication * application = [UIApplication sharedApplication];
    [application openURL:[NSURL URLWithString:@"http://www.12306.com"]];
/**
 *  隐藏状态栏。默认是不隐藏。默认是UIViewController管理的,如果我们在info.plist文件中设置之后这里设置就不会有任何作用
 *
 *  @return 返回为YES的时候为动态隐藏
 */
- (BOOL)prefersStatusBarHidden {
    return YES;
}
/**
 *  状态栏的颜色.默认是UIViewController管理的,如果我们在info.plist文件中设置之后这里设置就不会有任何作用
 */
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
  • 设置状态栏的时候需要如果单个设置的时候需要在每个控制器内部都要调用,如果想要全局使用的时候,需要现在info.plist文件中配置一下

在info.plist文件中设置

[application setStatusBarHidden:YES];

这类方法基本废弃

UIApplicationDelegate

  • 所有的app都有一个缺点就是当收到干扰的时候:(来电),我们的app就会受到干扰。
  • app在受到干扰的时候会产生一些系统的事件,这是我们的UIApplication,会通知他的代理,delegate去处理一些事情
/**
 *  完成启动的时候调用
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}
/**
 *  当app失去焦点的时候
 */
- (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 {
    // 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 {
    // 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.
}
/**
 *  app成为活动焦点的时候调用
 */
- (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.
}
/**
 *  app被销毁的时候调用,这个方法我们没办法验证
 */
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
/**
 *  接受到内存警告的时候调用
 *  我们可以清空图片缓存
 */
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

}

  • 类名转化成字符串好处:1.防止输入错误
NSStringFromClass([AppDelegate class])
  • 字符串转换成类:
NSClassFromString(@"AppDelegate");

results matching ""

    No results matching ""