iOS学习之UIApplication及其代理

1. 什么是UIApplication

  • UIApplication 对象是应用程序的象征,不能手动创建,不能 alloc init,一个应用程序只允许 一个

  • 每个应用都有自己的 UIApplication 对象,而且是单例

  通过 [UIApplication shareApplication] 可以获取这个单例对象。  

  弄成单例的原因:

   UIApplication 对象是用来设置应用全局信息的,一个应用程序如果有很多 UIApplication 对象,都不知道听谁的。

  • 一个iOS程序启动后创建第一个对象就是 UIApplication 对象。

  • 利用 UIApplication 对象,能进行一些应用级别的操作。

2. UIApplication的作用(常用的属性和方法)

 UIApplication 一般用来做一些应用级别的操作(app提醒框,控制联网的状态,打电话,打开网页)。

 1> 设置appIcon提醒数字

  图标需要手动清除,应用程序关闭,不会自动清除。

  iOS头文件中的属性声明:

@property(nonatomic) NSInteger applicationIconBadgeNumber __TVOS_PROHIBITED;  // set to 0 to hide. default is 0. In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.

  通过注释我们可以得知:iOS8之后必须注册用户通知,注册后才能显示提醒数字

  注册用户通知方法声明:

// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.
- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

  iOS8.0推出注册用户通知的目的是为了提高用户的体验,对于有强迫症的用户可以通过这个通知将提醒数字关闭。

  实例代码:

// 通过单例方法获取 UIApplication 对象
UIApplication *app = [UIApplication sharedApplication];

// 设置appIcon提醒数字,iOS8之后必须注册用户通知
app.applicationIconBadgeNumber = 10;
// 创建用户通知
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
// 注册用户的通知
[app registerUserNotificationSettings:settings];

  用户通知如图:

  点击 " " 后就可以看到类似于QQ消息数量的提醒,如图:

  点击 " 不允许 "后就没有,如图

 2> 设置联网状态

  显示联网状态,告诉用户此应用正在联网。  

  iOS头文件中的属性声明:

@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible __TVOS_PROHIBITED; // showing network spinning gear in status bar. default is NO

  实例代码:

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

  效果图:

 3>  打开网页

  iOS头文件中的方法声明:

- (BOOL)openURL:(NSURL*)url NS_EXTENSION_UNAVAILABLE_IOS("");

 实例代码:

#pragma mark - 打开网页
- (IBAction)btnClick:(id)sender {
    
    // URL:资源路径
    // URL:协议头://域名+路径  http,https,file,tel
    // 协议头:
    // 打开网页 @"http://www.baidu.com"
    
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    
    [[UIApplication sharedApplication] openURL:url];
}

  UIApplication 打开资源的好处:不用判断用什么软件打开,系统会自动根据协议头判断。

3. UIApplication 的代理方法

 我们可以进入到 UIApplicationDelegate 的头文件看到它的代理方法有很多,常用的也就是你创建一个 Single View Application 工程中在 AppDelegate 中生成的,下面代码就是,这些代理具体如何使用详见代码注释。

 实例代码:

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

// 学习代理方法,只需要知道这个方法什么时候调用,这个方法可以用来干嘛

// 程序启动完成后调用(常用)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"%s__%d", __FUNCTION__, __LINE__);
    return YES;
}

// 当app失去焦点的时候调用
- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"%s__%d", __FUNCTION__, __LINE__);
    // 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进入后台的时候调用(常用)
// app忽然被打断的时候,在这里保存一些需要用到的数据
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"%s__%d", __FUNCTION__, __LINE__);
    // 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(@"%s__%d", __FUNCTION__, __LINE__);
    // 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获取到焦点的时候调用,意味着app可以和用户交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"%s__%d", __FUNCTION__, __LINE__);
    // 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 {
    NSLog(@"%s__%d", __FUNCTION__, __LINE__);
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

// app接受内存警告的时候被调用(常用)
// 清空图片缓存 
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    NSLog(@"%s__%d", __FUNCTION__, __LINE__);
}

@end

 

你可能感兴趣的:(iOS学习之UIApplication及其代理)