简体   繁体   English

仅在iOS中首次启动时显示屏幕

[英]Show screen on first launch only in iOS

Tweetbot and Clear show's on the first start of the app a small tutorial screen how the app works. Tweetbot和Clear show在应用程序的第一次启动时,一个小教程屏幕应用程序如何工作。 The screen with the small tutorial only pops up on the first start up of the app (1 time) 带有小教程的屏幕仅在应用程序首次启动时弹出(1次)

How and with what can i make a similar thing? 如何以及我能做出类似的事情? Can anyone push me in the right direction? 任何人都可以把我推向正确的方向吗?

View i mean: 查看我的意思是:

在此输入图像描述

I'm assuming by Xcode you actually mean iOS. 我假设Xcode你实际上是指iOS。

What you need to do is use the NSUserDefaults class to store a flag indicating whether the user has seen the tutorial screen before. 您需要做的是使用NSUserDefaults类来存储一个标志,指示用户之前是否已经看过教程屏幕。

When your app first loads (or at the point you want to decide whether or not to show the tutorial screen), do something like this: 当您的应用首次加载时(或者您想要决定是否显示教程屏幕时),请执行以下操作:

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"hasSeenTutorial"])
    [self displayTutorial];

This checks the saved NSUserDefaults for the current user for a value named "hasSeenTutorial", which won't exist yet. 这将为当前用户检查已保存的NSUserDefaults,以获取名为“hasSeenTutorial”的值,该值尚不存在。 Since it doesn't exist, it will call displayTutorial . 由于它不存在,它将调用displayTutorial displayTutorial refers to your method for creating the tutorial view. displayTutorial指的是创建教程视图的方法。 You can figure out that part. 你可以找出那个部分。

Then, once the user closes the tutorial screen: 然后,一旦用户关闭教程屏幕:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasSeenTutorial"];

That value will be saved for your user profile, meaning the next time it checks it, it will be true, so displayTutorial won't be called. 该值将保存为您的用户配置文件,这意味着下次检查它时,它将是真的,因此不会调用displayTutorial

In your viewDidLoad: 在你的viewDidLoad中:

if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
                                objectForKey:@"aValue"]]) {
    [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"aValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    //Action here

}

Certainly, if we would like to tell user something about features after update (not only app launched first time), the solution below could be suitable. 当然,如果我们想在更新后告诉用户一些有关功能的信息(不仅是第一次推出应用),下面的解决方案可能是合适的。

In your viewDidLoad: 在你的viewDidLoad中:

NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *previousBundleVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"PreviousBundleVersion"];

if (![currentBundleVersion isEqualToString:previousBundleVersion] ) {
     // Here you can initialize your introduction view and present it!
}

Once the user closes the intro: 一旦用户关闭介绍:

NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];    
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
    [standardUserDefaults setObject:currentBundleVersion forKey:@"PreviousBundleVersion"];
    [standardUserDefaults synchronize];
}

In this case app bundle version stored in your standardUserDefaults will be differ from current bundle version only after update and shown only once as well as at first launch. 在这种情况下,存储在standardUserDefaults中的应用程序包版本将仅在更新后与当前包版本不同,并且仅在首次启动时显示一次。

Initialise your user defaults with a BOOL, something called instructionsSeen (or whatever you want) and set it to NO in your App delegate's initialize method.. In your app, test this value and if it is NO display your tutorial screen. 有BOOL,一些所谓的初始化用户默认instructionsSeen (或任何你想要的),并将其设置为NO在应用程序委托的initialize方法。在你的应用程序,测试这个值,如果它是NO显示的指南屏幕。 As part of showing and displaying this screen, set the instructionsSeen to YES and store it in your defaults. 作为显示和显示此屏幕的一部分,将instructionsSeen设置为YES并将其存储为默认值。

This way the demo screen will only show on first launch, unless the user uninstalls and installs the app again. 这样,演示屏幕将仅在首次启动时显示,除非用户再次卸载并安装应用程序。

You could also show the demo for a small number of launches (say 3). 您还可以显示少量发布的演示(例如3)。 In this case, don't use BOOL use a number and increment it instead. 在这种情况下,不要使用BOOL使用数字而是增加它。

Xamarin.iOS Version within AppDelegate: AppDelegate中的Xamarin.iOS版本:

UIStoryboard storyboard = UIStoryboard.FromName("Main", null);

        if (NSUserDefaults.StandardUserDefaults.BoolForKey ("hasSeenTutorial") == false) {
            UIViewController vc = storyboard.InstantiateViewController ("StartPageViewController");
            this.Window.RootViewController = vc;

        } else {
            UIViewController vc = storyboard.InstantiateViewController ("NonStartPageViewController");
            this.Window.RootViewController = vc;

        }

        this.Window.MakeKeyAndVisible ();

In my StartPageViewController, I have a button which sets NSUserDefaults to true, so the next time it runs, it will start off with the NonStartPageViewController: 在我的StartPageViewController中,我有一个按钮,它将NSUserDefaults设置为true,因此下次运行时,它将从NonStartPageViewController开始:

partial void RegisterButton_TouchUpInside (UIButton sender)
    {
        NSUserDefaults.StandardUserDefaults.SetBool(true,"hasSeenTutorial");
        NSUserDefaults.StandardUserDefaults.Synchronize();
    }
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:@"firstRun"]) {
    [defaults setObject:[NSDate date] forKey:@"firstRun"];
    [self displayTutorial];
}

Swift version : Swift版本:

if !(NSUserDefaults.standardUserDefaults().boolForKey("seenTutorial")) {
            //Tutorial part
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "seenTutorial")
        }

Everyone is making this more complex and vauge than it needs to be... Simple complete solution. 每个人都在使这个变得更加复杂和虚张声势......简单完整的解决方案。

In the ViewDidLoad: 在ViewDidLoad中:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLoadKey"]) {
    self.imageView.hidden = YES;
}else{
    self.imageView.hidden = NO;
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"FirstLoadKey"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM