首页 > 解决方案 > 仅为 AppDelegate.m iOS 中的首次启动设置特定方向

问题描述

我将这个库用于我的 react native 项目:

react-native-orientation-locker

我想要实现的是在除一个屏幕之外的所有屏幕上锁定方向为纵向。这个库工作正常,除非用户在横向模式下关闭应用程序,然后尝试在横向模式下打开它。应用程序在很短的时间内以横向打开,然后旋转为纵向。但我不想看到这一点,相反,我想在应用启动时以纵向模式打开应用。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

   /*if (the app has just been launched) {
        return UIInterfaceOrientationMaskPortrait;
   }*/

   // else, use the library to manage and set orientation
   return [Orientation getOrientation];
}

我不知道如何检查第一次启动以及在上面的 if 语句中放入什么

标签: iosswiftreact-nativeorientationscreen-orientation

解决方案


您可以简单地检查 UserDefaults 中的某些条目,如果它不包含您提供的对象,则它首先启动。之后,您可以将此对象放入 UserDefaults

例如

if([[NSUserDefaults standardUserDefaults] boolForKey:@"SOME_YOUR_KEY"] == nil) {
    // First faunch Code
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SOME_YOUR_KEY"];
    [[NSUserDefaults standardUserDefaults] synchronize];
} else {
    // Not first launch
}

推荐阅读