将应用程序优先时弹出警报设置 Opens/Let 用户在应用程序密码中设置

Set Alert to Pop when App first Opens/Let User Set in App Password

无法在用户首次打开应用程序时弹出提醒。我希望每个视图都有不同的警报,以引导用户完成他们的第一个 运行.

我不知道我错过了什么。浏览了网站上的各种帖子,无法弄清楚。

ViewController.h

    #import <UIKit/UIKit.h>



@interface ViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate> {

}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end



@implementation ViewController


- (void)viewdDidLoad {
    [super viewDidLoad];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"AlreadyRan"] )
    {
        UIAlertController * alert=   [UIAlertController
                                      alertControllerWithTitle:@"The Key"
                                      message:@"Press The Key Hole"
                                      preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 [alert dismissViewControllerAnimated:YES completion:nil];

                             }];
        UIAlertAction* cancel = [UIAlertAction
                                 actionWithTitle:@"Cancel"
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                     [alert dismissViewControllerAnimated:YES completion:nil];

                                 }];

        [alert addAction:ok];
        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:nil];

        [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"AlreadyRan"];
    }
    return 0;
}



@end

另外,在另一个视图控制器上,我希望用户能够在应用程序首次运行时通过警报弹出窗口设置密码 运行。这是我将密码设置为标准密码的代码。密码不需要存储到钥匙串,不是那种我只是希望应用程序将其保存在本地的应用程序。

通过ViewController.m

- (IBAction)enterPassword {
    NSString *passwordString = [NSString stringWithFormat:@"1234"];

    if ([passwordField.text isEqualToString:passwordString]) {
        //Password is Correct
        NSString * storyboardName = @"Main";
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
        UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"PhotoView"];
        [self presentViewController:vc animated:YES completion:nil];

    }


    else {
        //Password is wrong
        [self dismissViewControllerAnimated:YES completion: nil];

}
}

您已经在视图控制器中实现了 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,但实际上此方法属于 UIApplicationDelegate 协议(通常位于您的 AppDelegate.m 中)。

所以iOS永远不会调用这个方法。

我想你要找的是-(void) viewDidAppear: (BOOL) animated { ... }。当视图控制器出现在屏幕上时,它会被调用。所以你的实现看起来像

-(void) viewDidAppear: (BOOL) animated {
    [super viewDidAppear: animated];
    /* All your other code that used to be in didFinishLaunchingWithOptions */
}