创建仅在首次启动时显示的 ViewController
Create a ViewController that shows on First Launch Only
所以我正在创建一个 iOS 应用程序,它允许用户能够接收推送通知。我的 appDelegate didFinishLaunchingWithOptions 中已经有以下代码。
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
到目前为止一切顺利,但是,我想创建一个仅在首次启动时显示的初始视图控制器,它有望向用户解释并证明为什么它需要能够向用户发送推送通知,他们然后可以按继续,它会通过执行上面的代码来询问他们。希望这只会发生在第一次发射时。有什么建议吗?
谢谢
这很简单。
将布尔标志 haveDisplayedFirstVC 添加到 NSUserDefaults。在启动时阅读它。如果为false,需要显示第一次VC.
在这种情况下,如果您使用的是情节提要,请使用 instantiateViewControllerWithIdentifier:
从您的情节提要中加载视图控制器。如果您使用的是笔尖,请使用 initWithNibName:bundle:
创建并配置视图控制器后,使用 `presentViewController:animated:completion:' 将其显示在标准界面的 VC 之上,但使用 animated = NO。这样它就会在应用程序启动时出现在您的正常界面之上。 (或者将其动画化到屏幕上,随您喜欢。)
在 NSUserDefault 中添加状态 (Bool) 并首次检查密钥是否存在。
如果没有值,则显示您的初始 viewcontroller 并将状态更改为是(可能)。
下次将加载正常页面。
我想你可以这样做
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
BOOL userHasOnboarded = [[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstTime"];
if (userHasOnboarded) {
[self setupNormalRootViewControllerAnimated:YES];
}
else {
self.window.rootViewController = [self PushExplanation];
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)setupNormalRootViewControllerAnimated:(BOOL)animated {
UIViewController *mainVC = [UIViewController new];
mainVC.title = @"Home View Controller";
if (animated) {
[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainVC];
} completion:nil];
}
else {
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainVC];
}
}
在您的 PushExplanation 方法中,您可以显示一次您想要显示的 UIviewController,然后在同一个 UIViewController 中,您可以像这样请求推送通知
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
希望对您有所帮助
所以我正在创建一个 iOS 应用程序,它允许用户能够接收推送通知。我的 appDelegate didFinishLaunchingWithOptions 中已经有以下代码。
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
到目前为止一切顺利,但是,我想创建一个仅在首次启动时显示的初始视图控制器,它有望向用户解释并证明为什么它需要能够向用户发送推送通知,他们然后可以按继续,它会通过执行上面的代码来询问他们。希望这只会发生在第一次发射时。有什么建议吗?
谢谢
这很简单。
将布尔标志 haveDisplayedFirstVC 添加到 NSUserDefaults。在启动时阅读它。如果为false,需要显示第一次VC.
在这种情况下,如果您使用的是情节提要,请使用 instantiateViewControllerWithIdentifier:
从您的情节提要中加载视图控制器。如果您使用的是笔尖,请使用 initWithNibName:bundle:
创建并配置视图控制器后,使用 `presentViewController:animated:completion:' 将其显示在标准界面的 VC 之上,但使用 animated = NO。这样它就会在应用程序启动时出现在您的正常界面之上。 (或者将其动画化到屏幕上,随您喜欢。)
在 NSUserDefault 中添加状态 (Bool) 并首次检查密钥是否存在。 如果没有值,则显示您的初始 viewcontroller 并将状态更改为是(可能)。 下次将加载正常页面。
我想你可以这样做
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
BOOL userHasOnboarded = [[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstTime"];
if (userHasOnboarded) {
[self setupNormalRootViewControllerAnimated:YES];
}
else {
self.window.rootViewController = [self PushExplanation];
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)setupNormalRootViewControllerAnimated:(BOOL)animated {
UIViewController *mainVC = [UIViewController new];
mainVC.title = @"Home View Controller";
if (animated) {
[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainVC];
} completion:nil];
}
else {
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainVC];
}
}
在您的 PushExplanation 方法中,您可以显示一次您想要显示的 UIviewController,然后在同一个 UIViewController 中,您可以像这样请求推送通知
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
希望对您有所帮助