所以我正在创建一个iOS应用程序,它允许用户能够接收推送通知。我的appDelegate didFinishLaunchingWithOptions中已经有以下代码。
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
到目前为止这么好,但是,我想创建一个初始视图控制器,只在第一次启动时显示,它将有希望解释并证明用户为什么需要能够发送用户推送通知,他们可以按继续,它将通过执行上面的代码询问他们。希望这只会在第一次发布时发生。有什么建议吗?
由于
答案 0 :(得分:0)
这是微不足道的。
将一个布尔标志hasDisplayedFirstVC添加到NSUserDefaults。在启动时阅读它。如果它是假的,你需要第一次显示VC。
在这种情况下,如果您正在使用故事板,请使用instantiateViewControllerWithIdentifier:
从故事板中加载视图控制器。如果您正在使用笔尖,请使用initWithNibName:bundle:
创建并配置视图控制器后,使用`presentViewController:animated:completion:'将其显示在标准界面VC的顶部。但是动画= NO。这样,当应用程序启动时,它将显示在您的正常界面之上。 (或者将其设置在屏幕上,无论您喜欢哪种。)
答案 1 :(得分:0)
在 NSUserDefault 中添加状态(Bool),并在Firsttime检查密钥是否存在。 如果没有值,则显示您的初始viewcontroller并将状态更改为yes(可能)。 下次正常页面将加载。
答案 2 :(得分:0)
我认为你可以做这样的事情
- (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)];
希望这能帮到你