2022完整版UIApplication的介绍以及应用

简介: 2022完整版UIApplication的介绍以及应用

2022完整版UIApplication的介绍以及应用


UIApplication的介绍以及应用


UIApplication是一个系统单例,不论在程序何时创建,都只有一块内存。它的应用有:设置手机桌面上app图标上显示的消息数量提醒,可以监控联网状态,打电话,打开网页,控制状态栏等。


1,app消息提醒数字。


可以这样:[UIApplication sharedApplication].applicationIconBadgeNumber = 10;


注意:设置这个 需要app注册推送通知服务:[UIApplication sharedApplication]registerNotificationSettings:[UIUserNotifications settingForType:UIUserNotificationtypeBadge categories: nil];


2,可以打开网页 (打电话 同理)


[[UIApplication sharedApplication] openURL:[NSURL urlWithString:@"http://www.baidu.com"]]; 可以放在按钮的点击方法里,当点击了按钮,就直接打开指定的网页。


3,控制状态栏隐藏或者改变样式。


 首先注意: ios7以后,状态栏默认由控制器管理,即控制器会自动调用 preferStatusBarhidden这个方法来判断当前控制器的状态栏是否隐藏。但是每个控制器都需要设置,比较麻烦。 可以交给UIApplication 管理, 需要在info.plist 中添加 字段View Controller based status bar appearance 设置为NO,表示状态栏不需要由控制器控制。 那么就可以使用UIApplication 来管理状态栏。如下:


[[UIApplication sharedApplication] setStatusBarHidden:YES];


4,uniapplication相关方法。

/************ 运行程序时,必须执行的方法(程序入口) *****************************************/ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } /************ 当前应用程序将要进入非活动状态(进入后台)(Will, Should 将要的意思) *****************************************/ - (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. */ } /************ 当前程序已经进入后台(Did 已经的意思) *****************************************/ - (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. */ } /************ 当前程序将要进入前台 *****************************************/ - (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. */ } /************ 当前程序已经变成活动状态(进入前台) *****************************************/ - (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. */ } /************ 当前程序运行结束 *****************************************/ - (void)applicationWillTerminate:(UIApplication *)application { /* Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. */ } /************ 内存紧张 *****************************************/ - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { // try to clean up as much memory as possible. next step is to terminate app }

Main法。 int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } 作用:main 函数中只调用了一个 UIApplicationMain 的函数,整个程序的入口可以认为从 main 交给了 UIApplicationMain 函数。 函数原型: UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName); argc, argv[]:与 main 的两个参数相同,一个是整形,一个是指针数组。 principalClassName:委托方类名,这个类实时检测当前程序的运行状态,这个参数一定要是 UIApplication 类或其子类,如果参数为空 nil,默认为 UIApplication 。 delegateClassName:代理方类名,遵守 UIApplicationDelegate 协议,实现协议中的方法,当第三个参数中的委托方检测到当前程序状态改变时会委托第四个参数在状态改变时执行相应的操作。


  1. 状态栏的设置。
// 获取状态栏高度 // returns CGRectZero if the status bar is hidden,默认高度为 20.0 CGFloat height = [

   3.状态栏上网络状态风火轮的设置。

// 风火轮旋转状态设置 // YES 开始旋转,NO 停止旋转(默认),停止时自动隐藏 [UIApplication sharedA


8, 设置联网状态。

 [UIApplication sharedApplication] .networkActivityIndicatorVisible = YES; 设置这个,当程序正在联网时, 状态栏会有一个菊花在转。

相关文章
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17611 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1391 5
|
14天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2444 8
|
19天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3958 22
|
21天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。

热门文章

最新文章