IOS AppDelegate.m 函数说明

本文深入探讨了iOS应用开发中AppDelegate类中的关键生命周期方法及其用途,包括applicationdidFinishLaunchingWithOptions、applicationWillResignActive、applicationDidEnterBackground、applicationWillEnterForeground、applicationDidBecomeActive和applicationWillTerminate。详细解释了每个方法的功能和应用场景,帮助开发者更好地掌握应用运行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面就过一下,AppDelegate.h/m 中默认的方法: 
 
1. application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 
Tells the delegate when the application has launched and may have additional launch options to handle.
 
在应用程序启动后,要执行的委托调用。 
 
 
2. applicationWillResignActive:(UIApplication *)application
 
Tells the delegate that the application is about to become inactive.This method is called to let your application know that it is about to move from the active to inactive state.After calling this method, the application also posts a UIApplicationWillResignActiveNotification notification to give interested objects a chance to respond to the transition.
 
在应用程序将要由活动状态切换到非活动状态时候,要执行的委托调用,如 按下 home 按钮,返回主屏幕,或全屏之间切换应用程序等。
 
 
 
3. applicationDidEnterBackground:(UIApplication *)application
 
Tells the delegate that the application is now in the background.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.
 
在应用程序已进入后台程序时,要执行的委托调用。
 
 
 
4. applicationWillEnterForeground:(UIApplication *)application
 
Tells the delegate that the application is about to enter the foreground.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.
 
在应用程序将要进入前台时(被激活),要执行的委托调用,刚好与 applicationWillResignActive 方法相对应。
 
 
 
5. applicationDidBecomeActive:(UIApplication *)application
 
Tells the delegate that the application has become active.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.
 
在应用程序已被激活后,要执行的委托调用,刚好与  applicationDidEnterBackground 方法相对应。
 
 
6. applicationWillTerminate:(UIApplication *)application
 
Tells the delegate when the application is about to terminate.Called when the application is about to terminate. Save data if appropriate.
 
在应用程序要完全推出的时候,要执行的委托调用。
iOS应用中,当通过Flutter框架集成到Objective-C或Swift的原生AppDelegate中时,通常你会使用`UIApplicationDelegate`协议来处理与Flutter交互的部分。Flutter作为一个跨平台UI库,允许通过方法Channel机制来传递数据给Native端(例如AppDelegate)。当你在Flutter中创建一个`MethodChannel`并调用其`invokeMethod`函数时,你可以将参数序列化为JSON格式,然后在Objective-C或Swift的对应监听方法(如`application:didFinishLaunchingWithOptions:`)中接收到这个参数。 首先,你需要在Flutter端创建一个MethodChannel: ```dart import 'package:flutter/services.dart'; ... final MethodChannel channel = MethodChannel('your/channel/name'); channel.invokeMethod('initWithArgs', [arg1, arg2]); // 参数作为List<String>或Map<String, dynamic> ``` 然后,在iOS AppDelegate中,注册接收方法调用,并在适当的地方处理它: ```swift @objc(MyAppDelegate) class AppDelegate: NSObject, FlutterApplicationDelegate { ... func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let channel = FlutterMethodChannel(name: "your/channel/name") channel.setMethodCallHandler { call, result in if let args = call.arguments as? [String: Any], let arg1 = args["arg1"] as? String, let arg2 = args["arg2"] as? Int { // 这里可以使用arg1和arg2做你想做的操作 print("arg1: \(arg1), arg2: \(arg2)") result.success(true) // 返回成功响应给Flutter } else { result.notImplemented() // 如果参数解析失败,返回错误信息 } } ... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值