iOS 13中未调用应用程序委托方法 [英] App delegate methods aren't being called in iOS 13

查看:291
本文介绍了iOS 13中未调用应用程序委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 11并为iOS 13构建应用程序.在我用Xcode创建的新项目中,缺少一些UIApplicationDelegate方法,因此我将它们重新添加到了应用程序委托文件中. 单视图应用程序"项目的新模板缺少这些方法.问题在于,除了-application:didFinishLaunchingWithOptions:之外,没有其他任何委托方法被调用.这是我的应用程序代表:

I am using Xcode 11 and building an app for iOS 13. In a new project I created in Xcode some of the UIApplicationDelegate methods were missing so I added them back into the app delegate file. The new template for a "Single View App" project was missing the methods. The problem is that none of the delegate methods are getting called except -application:didFinishLaunchingWithOptions:. Here is my app delegate:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"application:didFinishLaunchingWithOptions:");
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"applicationDidEnterBackground:");
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"applicationWillEnterForeground:");
}
#pragma mark - UISceneSession lifecycle

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}

- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
}

@end

推荐答案

iOS 13提供了一种发送应用程序生命周期事件的新方式.他们不是通过UIApplicationDelegate而是通过 UIWindowSceneDelegate ,它是一个 UISceneDelegate 子协议. UISceneDelegate具有重要的委托方法.

iOS 13 has a new way of sending app lifecycle events. Instead of coming through the UIApplicationDelegate they come through the UIWindowSceneDelegate which is a UISceneDelegate sub-protocol. UISceneDelegate has the important delegate methods.

此更改是为了支持iOS 13中的多个窗口.WWDC 2019会话212中有更多信息"在14:30左右并显示由一个有着非常耀眼的高帮的男人.较短的会话258 为多个Windows构建应用也对发生了什么变化.

This change is to support multiple windows in iOS 13. There's more information in WWDC 2019 session 212 "Introducing Multiple Windows on iPad". The technical information starts at around 14:30 and is presented by a man with very sparkly high-tops. The shorter session 258 Architecting Your App for Multiple Windows also has a great introduction to what's changed.

这是它的工作方式::如果Info.plist中有"应用场景清单",而您的应用程序委托具有configurationForConnectingSceneSession方法,则不会将后台和前台生命周期消息发送给您的应用程序委托.这意味着这些方法中的代码将无法运行:

Here's how it works: If you have an "Application Scene Manifest" in your Info.plist and your app delegate has a configurationForConnectingSceneSession method, the UIApplication won't send background and foreground lifecycle messages to your app delegate. That means the code in these methods won't run:

  • applicationDidBecomeActive
  • applicationWillResignActive
  • applicationDidEnterBackground
  • applicationWillEnterForeground
  • applicationDidBecomeActive
  • applicationWillResignActive
  • applicationDidEnterBackground
  • applicationWillEnterForeground

应用程序委托仍将接收willFinishLaunchingWithOptions:didFinishLaunchingWithOptions:方法调用,因此这些方法中的任何代码都将像以前一样工作.

The app delegate will still receive the willFinishLaunchingWithOptions: and didFinishLaunchingWithOptions: method calls so any code in those methods will work as before.

如果您希望恢复以前的行为,则需要

  1. 从应用程序的Info.plist中删除应用程序场景清单"条目
  2. 注释或删除application:configurationForConnectingSceneSession:options:方法(或Swift application(_:configurationForConnecting:options:)函数)
  3. 将window属性添加回您的应用程序委托(@property (strong, nonatomic) UIWindow *window;)
  1. Delete the "Application Scene Manifest" entry from the app's Info.plist
  2. Comment or delete the application:configurationForConnectingSceneSession:options: method (or the Swift application(_:configurationForConnecting:options:)function)
  3. Add the window property back to your app delegate (@property (strong, nonatomic) UIWindow *window;)

或者,打开Xcode制作的SceneDelegate文件并在其中使用新的生命周期方法:

Alternatively, open the SceneDelegate file that Xcode made and use the new lifecycle methods in there:

- (void)sceneDidBecomeActive:(UIScene *)scene {
}
- (void)sceneWillResignActive:(UIScene *)scene {
}
... etc

可以通过在Info.plist中将启用多个Windows"("UIApplicationSupportsMultipleScenes")设置为否"来使用新的UIScene生命周期信息而不采用多窗口支持(这是新项目的默认设置).这样,您就可以在较小的步骤中开始采用新的API.

It's possible to use the new UIScene lifecycle stuff without adopting multiple window support by setting "Enable Multiple Windows" ("UIApplicationSupportsMultipleScenes") to "NO" in the Info.plist (this is the default for new projects). This way you can start adopting the new API in smaller steps.

您可以看到场景委托方法名称与应用程序委托名称紧密匹配.一个令人困惑的事情是应用程序委托方法没有被弃用,因此如果您同时拥有应用程序委托和场景委托方法,则不会收到警告,但是只会调用一个.

You can see that the scene delegate method names are a close match for the app delegate ones. One confusing thing is that the app delegate methods aren't deprecated so you won't get a warning if you have both app delegate and scene delegate methods in place but only one will be called.

UISceneDelegate接管的其他事项包括用户活动(continueUserActivity:等),状态恢复(stateRestorationActivityForScene:等),状态栏问题和打开的URL. (我不确定这些是否替换了应用程序委托方法).它还具有生命周期事件的类似通知(例如UISceneWillDeactivateNotification).

Other things that UISceneDelegate takes over are user activities (continueUserActivity: etc), state restoration (stateRestorationActivityForScene: etc), status bar questions and opening URLs. (I'm not sure if these replace the app delegate methods). It also has analogous notifications for the lifecycle events (like UISceneWillDeactivateNotification).

在WWDC会话中,有一些适合您的图像:

与Swift等效的功能:

The function equivalents for Swift:

班级职责:

这篇关于iOS 13中未调用应用程序委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆