Accéder au contenu principal

Ajouter UIScene à une vielle app

 

 UIScene devient obligatoire avec IOS 27.


Plus le choix, il faut passer toutes les vielles applications à UIScene. Avec les scènes, la fenêtre principale n'est plus attachée à l'application mais à une scène. UIApplicationDelegate contient toujours la fonction de lancement didFinishLaunchingWithOptions:. C'est toujours un bon endroit pour initialiser des variables globales, et pour mettre les valeurs par défaut de NSUserDefaults.standardUserDefaults.  

On perd les fonctions applicationDidBecomeActive:, applicationWillResignActive:, applicationWillEnterForeground:, application:openURL:options:, qui se retrouvent, soit dans UISceneDelegate, soit sous forme de notification, soit les deux.
Deux fonctions s'ajoute dans AppDelegate, une pour la connection d'une scène et une pour la déconnection :

#pragma mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {

    // Called when a new scene session is being created.

    // Use this method to select a configuration to create the new scene with.

    return [[UISceneConfiguration allocinitWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];

}


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

    // Called when the user discards a scene session.

    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.

    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.

}


Ces deux fonctions ne sont pas obligatoire si le Info.plist est bien remplit.
Le "Default Configuration" doit être définit dans "Info.plist".

<key>UIApplicationSceneManifest</key>

<dict>

<key>UIApplicationSupportsMultipleScenes</key>

<false/>

<key>UISceneConfigurations</key>

<dict>

<key>UIWindowSceneSessionRoleApplication</key>

<array>

<dict>

<key>UISceneConfigurationName</key>

<string>Default Configuration</string>

<key>UISceneDelegateClassName</key>

<string>SceneDelegate</string>

<key>UISceneStoryboardFile</key>

<string>Main</string>

</dict>

</array>

</dict>

</dict>


Dans le Info.plist, on définit la UISceneDelegateClassName et le UISceneStoryboardFile.
Il faut donc, évidemment, avoir un "Main.storyboard" et un "SceneDelegate.m" (et .h).
Le UIApplicationSupportsMultipleScenes à false est important, pour éviter plusieurs scènes de la même app.

Le nouveau fichier SceneDelegate.h doit contenir la UIWindow qui disparait de AppDelegate :


@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strongnonatomicUIWindow * window;

@end


Le nouveau fichier SceneDelegate.m n'a pas besoin de contenir de fonction, à moins de vouloir gérer le cycle de vie de la scène (et donc de la fenêtre) :

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {

// Use this method to optionally configure and attach the UIWindow `window` 

// to the provided UIWindowScene `scene`.

// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.

// This delegate does not imply the connecting scene or session are new 

// (see `application:configurationForConnectingSceneSession` instead).

}

- (void)sceneDidDisconnect:(UIScene *)scene {

// Called as the scene is being released by the system.

// This occurs shortly after the scene enters the background, or when its session is discarded.

// Release any resources associated with this scene that can be re-created the next time the scene connects.

// The scene may re-connect later, as its session was not necessarily discarded

// (see `application:didDiscardSceneSessions` instead).

}

- (void)sceneDidBecomeActive:(UIScene *)scene {

// Called when the scene has moved from an inactive state to an active state.

// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.

}

- (void)sceneWillResignActive:(UIScene *)scene {

// Called when the scene will move from an active state to an inactive state.

// This may occur due to temporary interruptions (ex. an incoming phone call).

}

- (void)sceneWillEnterForeground:(UIScene *)scene {

// Called as the scene transitions from the background to the foreground.

// Use this method to undo the changes made on entering the background.

}

- (void)sceneDidEnterBackground:(UIScene *)scene {

// Called as the scene transitions from the foreground to the background.

// Use this method to save data, release shared resources, and store enough scene-specific state information

// to restore the scene back to its current state.

}


Commentaires

Posts les plus consultés de ce blog

malloc: xzm: failed to initialize deferred reclamation buffer

Lorsque je lance une de mes applications avec Xcode sur mon iPhone .... J'ai ce message de début :  malloc: xzm: failed to initialize deferred reclamation buffer (46)  Et franchement, je n'aime pas avoir des message que je ne comprends pas et qui contiennent le mot "failed" ! Mais le message n'existe pas avec mon iPad IOS16. Donc, ok, malloc je connais, on parle de la fonction de base d'allocation de mémoire système, il y a pleins d'outils pour les problèmes d'allocation mémoire, "xzm" doit être un de ces outils. Je ne trouve rien sur internet ! RIEN ! Perplexity ou Claude ne savent pas non plus ce que c'est. Je suis obligé de faire des suppositions... XZ est un format de compression conçu pour remplacer BZIP qui est conçu pour remplacer GZIP qui est conçu pour remplacer Z. Peut-être que les données de debug de Xcode transitent en étant compressé par défaut ? Allons voir dans le schéma du projet, pour voir les options concernant les mallo...

UIScene et openURLContexts

De IOS 2 à IOS 9, lorsque vous envoyiez un fichier dans une application (par exemple à partir d'un fichier attaché en mail), cela lançait l'application et la fonction openURL du délégateur de l'application : -( BOOL ) application :( UIApplication *)application            openURL :( NSURL *)url { Si vous retourniez YES, le fichier était considéré comme lu.  Ensuite de IOS 4.2 à 9.0, Apple à ajouté la possibilité de connaître l'application source du fichier. -( BOOL ) application :( UIApplication *)application            openURL :( nonnull NSURL *)url sourceApplication :( nullable NSString *)sourceApplication annotation :( nonnull id )annotation { Ensuite, avec IOS 9, Apple a ajouté des options afin d'y mettre l'application source, les annotations, mais aussi tout ce qu'ils pourrait vouloir dans le futur. On met un dictionnaire à la place de paramètre, et on évitera ainsi de multiplier les fonctions. -( BOOL ) applic...

UIMenu de UICollectionView

 Dans une UICollectionView, pour faire un menu, le plus simple est d'utiliser une des fonctions delegate standard. La création de cette fonction lancera automatiquement un gestionnaire d'appuie long, et l'affichage du menu au bon endroit. - ( UIContextMenuConfiguration *) collectionView :( UICollectionView *)collectionView   contextMenuConfigurationForItemAtIndexPath :( nonnull NSIndexPath *)indexPath point :( CGPoint )point {               return [ UIContextMenuConfiguration configurationWithIdentifier : nil          previewProvider : nil           actionProvider :^ UIMenu *( NSArray < UIMenuElement *> *suggestedActions) {            return [ UIMenu menuWithTitle : @""                           children : @[               [ UIAction actio...