iOS开发-UIApplication和App启动状态

简介:
+关注继续查看

UIApplication简单从字面上了解就是应用程序,开发的时候有的时候会根据需要调用其中的方法,看起来不起眼,实际在iOS开发UIApplication提供了iOS程序运行期间的控制和协作工作。每一个应用程序在运行期必须有且仅有一个UIApplication(或则其子类)的一个实例,就是设计模式中常说的单例模式,通过sharedApplication获取单例对象,不需要另外的init一个UIApplication。

UIApllication概念

经常程序可能编译的时候没错,运行的时候报错,通常都是停留在main.m中,这是程序的入口点,也从这里创建了UIApplication单例实例。

1
2
3
4
5
int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nilNSStringFromClass([AppDelegate class]));
    }
}

UIIApplication一般主要用于处理用户事件,处理的时候会新建一个队列,将所有用户事件都放入队列,逐一处理,在处理的时候,它会发送当前事件 到一个合适的处理事件的目标控件。同时UIApplication实例还维护一个在本应用中打开的window列表(UIWindow实例),这样UIApplication可以获取应用中的任何一个UIView对象。UIApplication实例会被赋予一个代理对象,以处理应用程序的生命周期事件(比如程序启动和关闭),系统事件(比如电话、短信)等等。具体看下具体应用,

故事板中新建一个按钮,之后的画获取点击事件,加入以下两行代码:

1
2
3
UIApplication *application=[UIApplication sharedApplication];
 //设置图标上的更新数字
 application.applicationIconBadgeNumber=25;

在iOS8.0之前会看到App上面有红色的数字显示,前几个月微信头像整人事件,不少人被坑了,强迫自己几天没看微信,看到小红点就想点,控制台会提示以下信息:Attempting to badge the application icon but haven't received permission from the user to badge the application

具体解决办法很简单,就是获取以下机器的版本号,然后根据需要通知用户选择设置(个人觉得更人性化,手机上要是满屏的小红点,想想就。。);

1
2
3
4
5
6
7
8
9
float versionNumber=[[[UIDevice currentDevice] systemVersion] floatValue];
 
if (versionNumber >= 8.0) {
    NSLog(@"%@",[NSString stringWithFormat:@"当前的系统为%f",versionNumber]);
    //通知设置
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    //接收用户的选择
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

 UIApplication可以做一些其他的事情,比如其中的openURL就比较实用:

1
2
3
4
5
6
7
8
//电话
[application openURL:[NSURL URLWithString:@"tel://10010"]];
//短信
[application openURL:[NSURL URLWithString:@"sms://10010"]];
//邮件
[application openURL:[NSURL URLWithString:@"mailto://10000@qq.com"]];
//URL
[application openURL:[NSURL URLWithString:@"http://www.cnblogs.com/xiaofeixiang/"]];

UIApplicationDelegate

UIApllicationDelegate协议中定义的方法有有一部分是和应用程序的状态是相关的,新建的项目默认的都有AppDelegate.h和AppDelegate.m文件,可以先参考以下默认生成的AppDelegate.m代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
//  AppDelegate.m
//  UIApplicationDemo
//
//  Created by keso on 15/2/5.
//  Copyright (c) 2015年 keso. All rights reserved.
//
 
#import "AppDelegate.h"
 
@interface AppDelegate ()
 
@end
 
@implementation AppDelegate
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"application-程序完成加载");
    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.
    NSLog(@"applicationWillResignActive-注销激活");
}
 
- (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.
    NSLog(@"applicationDidEnterBackground-进入后台");
}
 
- (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(@"applicationWillEnterForeground-进入前台");
}
 
- (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(@"applicationDidBecomeActive-成为激活状态");
}
 
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
      NSLog(@"applicationWillTerminate-程序终止状态");
}
 
@end

最开始main函数中其实是将AppDelegate实例化交给UIApplication去代理执行其中的方法,如果你喜欢这个AppDelegate文件,完成有必须要根据需要新建一个自己的类文件,只要声明UIApplicationDelegate即可。关于上面的状态我们可以先看一张苹果官网的图:

 

其中方法苹果默认有解释,不过大概写下个人的理解:

 application:didFinishLaunchingWithOptions:—应用展示给用户之前完成最终的初始化工作

applicationWillResignActive:-从激活状态进入休眠状态,突然的电话,短信可以使当前程序进入后台,不可交互

applicationDidEnterBackground-应用程序进入后台,可以保存和传输数据

applicationWillEnterForeground-可以在这里恢复数据,即将进入前台,还不是激活状态

applicationDidBecomeActive-进入前台获取获取焦点,可以交互

applicationWillTerminate-应用程序销毁的时候调用,如果挂起不调用

写到这里可以具体看一下App的启动过程:

1.main函数

2.UIApplicationMain 创建UIApplication对象,创建UIApplication的delegate对象(监听应用程序状态)

3.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard,创建UIWindow(如果没有故事板,程序启动完毕时调用代理的application:didFinishLaunchingWithOptions:方法,在application:didFinishLaunchingWithOptions:中创建UIWindow ),创建和设置UIWindow的rootViewController, 显示窗口;

本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4276169.html,如需转载请自行联系原作者

相关文章
|
3天前
|
iOS开发 开发者
📝 App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
在iOS应用程序开发过程中,进行App备案并获取公钥及证书SHA-1指纹是至关重要的步骤。本文将介绍如何通过appuploader工具获取iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹,帮助开发者更好地理解和应用该过程。
|
13天前
|
JavaScript 前端开发 PHP
用swift开发ios移动端app应用初体验
直接跟着 apple 官方的 SwiftUI 教程跑的,写惯了 javascript 奔放的代码,很多语法理解起来还是有点费劲
26 1
|
16天前
|
小程序 开发工具 Android开发
Donut多端框架小程序打包适配ios和安卓app
腾讯新出了一个 Donut 多端框架,可以直接将微信小程序转成 ios 和 安卓 app,小程序开发者工具里也集成了 app 相关升级、调试和打包的功能,终于可以一套代码开发出3个客户端了!
51 0
Donut多端框架小程序打包适配ios和安卓app
|
17天前
|
API 数据安全/隐私保护 iOS开发
 利用uni-app 开发的iOS app 发布到App Store全流程
首次提交关于App Store上架步骤流程:兼容检查 -> 账号申请 -> 证书申请 -> 发布准备 -> App Store上架 -> 审核后续提交审核iOS审核团队联系方式;
|
20天前
|
移动开发 安全 数据安全/隐私保护
iOS移动应用安全加固:保护您的App免受恶意攻击的重要步骤
本文介绍了移动应用程序(App)加固的概念和流程,以及市场上几家知名的APP加固公司。同时提供了对iOS ipa文件进行混淆保护的方案,以增强App的安全性和防范反编译和破解行为。
|
20天前
|
安全 数据安全/隐私保护 iOS开发
【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固 摘要
随着智能手机的普及,越来越多的用户使用iOS设备来处理日常任务,因此iOS应用程序的安全性变得越来越重要。为了防止应用程序被攻击或破解,开发人员需要采用一些保护措施来加固应用程序。本文将介绍一种使用ipaguard混淆加固的方法来保护iOS应用的安全。
|
23天前
|
Web App开发 安全 iOS开发
TrollStore巨魔商店永久安装APP 可实现IOS应用双开 安装任意APP
TrollStore 是一个永久签名的监禁应用程序,可以永久安装您在其中打开的任何 IPA。
|
23天前
|
Android开发 iOS开发 开发者
App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
,在appuploder直接复制IOS信息;如果还没有创建证书,请上传正确的P12苹果证书后,系统会自动解析出对应的签名和公钥信息; ——APP备案的原理是基于原有的工信部域名备案系统,如果已经有了域名备案,无需新增备案主体;只需要在之前的域名备案系统里面,新增APP信息,收集的APP信息主要包括APP包名和签名及公钥这3项;——APP备案是属于行政常规主体信息预存,和域名一样,自行决定是否备案。目前国内安卓应用商店是全面要求APP备案的,如果没有APP备案是不能通过审核发布到各大应用商店。——如看了教程,还不清楚怎么获取APP包名、安卓签名、苹果sha1签名、公钥等信息,请联系我们在线客服,
|
26天前
|
iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)(3)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)
14 0
|
26天前
|
前端开发 数据处理 iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)(2)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)
14 0
相关产品
云迁移中心
推荐文章
更多