UIApplication概念

main.m 解读


int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
  • 任何一个 C 语言的程序入口都是 main 函数
  • main() 函数中执行了一个 UIApplicationMain() 这个函数
/**
 This function is called in the main entry point to create the application object and the application delegate and set up the event cycle.

 - 此函数在主入口点被调用
 - 负责以下工作:
   - 创建应用程序对象 —— 当前运行的应用程序
   - 创建应用程序的代理对象 —— 监听应用程序事件
   - 设置事件循环 —— 监听程序中的所有事件

 Even though an integer return type is specified, this function never returns. When users exits an iOS application by pressing the Home button, the application moves to the background.

 - 尽管函数声明返回了一个整数值,但是此函数永远不会返回
 - 当用户按下 Home 按钮退出应用程序时,应用程序会被移动到后台

 参数说明:

 @param argc                argv 参数个数,使用 main 函数的 argc 即可
 @param argv                参数数组,使用 main 函数的 argv 即可
 @param principalClassName  - 指定应用程序类名(app的象征)
                            - 该类必须是 UIApplication(或子类)
                            - 如果为 nil,则用 UIApplication 类作为默认值
 @param delegateClassName   - 指定应用程序的代理类的名称
                            - 实现 UIApplicationDelegate 协议中定义的方法
                            - 监听应用程序事件:启动 / 进入前台 / 退出到后台
 */
int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName);

什么是 UIApplication

  • UIApplication 对象是应用程序的象征
  • 每一个应用都有自己的 UIApplication对象,而且是单例(唯一一个对象)
  • 通过 [UIApplication sharedApplication] 可以获得这个单例对象
  • 一个 iOS 程序启动后创建的第一个对象就是UIApplication 对象
  • 利用 UIApplication 对象,能进行一些应用级别的操作
    – applicationIconBadgeNumber:设置应用程序图标右上角的红色提醒数字
    –networkActivityIndicatorVisible:设置联网指示器的可见性
    –idleTimerDisabled=YES:防止屏幕睡眠
    –openURL:

    在IOS8中要想设置applicationIconBadgeNumber,需要用户的授权,在IOS8中,需要加上下面的 代码
if (version >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }

openURL:


UIApplication有个功能十分强大的openURL:方法

(BOOL)openURL:(NSURL*)url;

openURL:方法的部分功能有
//打电话
    [app openURL:[NSURL URLWithString:@"tel://10086"]];
    // 发短信
    [app openURL:[NSURL URLWithString:@"sms://10086"]];
    // 发邮件
    [app openURL:[NSURL URLWithString:@"mailto://12345@qq.com"]];
    // 打开一个网页资源
    [app openURL:[NSURL URLWithString:@"http://www.baidu.com"]];


// iOS10 使用下面的方法
[app openURL:[NSURL URLWithString:@"tel://10086"] options:@{@"value":@"key"} completionHandler:^(BOOL success) {
        NSLog(@"%d",success);
 }];

//检查能否打开某个URL,并且打开URL;这个功能可以配合应用的自定义URL功能,来检测是否安装了某个应用。比如检测是否安装了淘宝的应用,可以用下面的代码:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"taobao://item.taobao.com/item.htm?id=12688928896"]];  
// 判断当前系统是否有安装淘宝客户端  
 if ([[UIApplication sharedApplication] canOpenURL:url]) {  
        // 如果已经安装淘宝客户端,就使用客户端打开链接  
        [[UIApplication sharedApplication] openURL:url];  
 }  

常见的应用程序代理方法


/// 应用程序加载完成,如果只是从后台进入前台不会被调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSLog(@"%s", __FUNCTION__);
    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.
    // 使用此方法暂停正在执行的任务,例如:
    // - 禁用时钟
    // - 限制 OpenGL ES 刷新率
    // - 暂停游戏
    NSLog(@"%s", __FUNCTION__);
}

/// 应用程序退出到后台
- (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.
    // 如果应用程序支持后台执行
    // 当用户退出时,会调用此方法替代 `applicationWillTerminate:`
    NSLog(@"%s", __FUNCTION__);
}

/// 应用程序进入前台
- (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.
    // 从后台切换到前台时调用
    // 可以在此方法中恢复退出到后台时保存的状态
    NSLog(@"%s", __FUNCTION__);
}

/// 应用程序被激活
- (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.
    // 重新启动在 `应用程序取消激活状态时` 暂停的任务
    // 如果应用程序之前已经进入到后台,可以刷新用户界面
    NSLog(@"%s", __FUNCTION__);
}

/// 应用程序被终止
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // 应用程序被终止时调用
    // 如果需要可以在此方法中保存数据
    NSLog(@"%s", __FUNCTION__);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值