Creating a first launch viewcontroller - ios

I'm trying to create a "initial setup page" that is shown if the app is launched for the first time on the device.
I made it like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
NSLog(#"not first launch");
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"first launch");
}
}
Now I want to create a view controller and push to this view controller if it's the first time the app is launched.
What do I need to do?

Create a new ViewController. Import the header in appDelegate.h file also create a instance variable of that class with name initialViewController.
Change your else condition like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
NSLog(#"not first launch");
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.initialViewController = [[InitialViewController alloc] initWithNibName:#"InitialViewController" bundle:nil];
self.window.rootViewController = self.InitialViewController;
NSLog(#"first launch");
}
[self.window makeKeyAndVisible];
return YES;
}

Related

Assertion failure in -[UIApplication _runWithMainScene:transitionContext:completion:],

I am getting the following error in iOS9 only.
Here is my code:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"login_dict"])
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"isLogout"] == nil || [[[NSUserDefaults standardUserDefaults] objectForKey:#"isLogout"] integerValue]== 0)
{
self.loginDict = [[BaseViewController sharedInstance] removeNullFromDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:#"login_dict"]];
self.firstViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
}
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"isLogout"] integerValue]== 1)
{
self.firstViewController = [[WelcomeViewController alloc] initWithNibName:#"WelcomeViewController" bundle:nil];
}
NSLog(#"Userinfo = %#",self.loginDict);
}
else
{
self.firstViewController = [[WelcomeViewController alloc] initWithNibName:#"WelcomeViewController" bundle:nil];
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.navigationController = [[BufferedNavigationController alloc] initWithRootViewController:self.firstViewController];
//[window makeKeyAndVisible];
[self.window setRootViewController:self.navigationController];
}
Note : This code is working fine in Xcode 6.4 and iOS8.
Assertion failure in -[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294
I had to remove this line from application didFinishLaunchingWithOptions:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
And that fixed it for me.
Using this row solved my issue (iOS 10):
[self.window setRootViewController:self.navigationController];
Was (worked for older iOS and Xcode):
[self.window addSubview:navigationController.view];
here i got the solution by checking if navigationController is nil or not:-
if (self.navigationController== nil)
{
self.navigationController = [[BufferedNavigationController alloc] initWithRootViewController:self.firstViewController];
}
else
{
[self.navigationController setViewControllers:#[self.firstViewController] animated:NO];
}

How to handle Remote push notifications in didreceiveRemoteNotification in AppDelegate

For my Remote push notifications I have handle like this in my AppDelegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
dm=[DataManager sharedManager];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
dm.screenHight=[UIScreen mainScreen].bounds.size.height;
dm.screenWidth=[UIScreen mainScreen].bounds.size.width;
NSLog(#"^^^^^^^^^^^^^^ Screen hight ^^^^^^^^^^^^^ %i",dm.screenHight);
// for PAYPAL
[PayPalMobile initializeWithClientIdsForEnvironments:#{PayPalEnvironmentProduction : #"YOUR_CLIENT_ID_FOR_PRODUCTION",PayPalEnvironmentSandbox : #"AQhrXRAyHg6nuJCma6vkl1ZtxmWUynzuf2temitMSJEZf8n74p9iKAt6TgSf"}];
/// FOR PAYPAL
NSLog(#"%#",userInfo);
NSDictionary *dictionary=[userInfo objectForKey:#"jsonContent"];
dm.notificationDictionary=dictionary;
if ( application.applicationState == UIApplicationStateActive )
{
UIViewController *viewController1;
viewController1 = [[SplashViewController alloc] initWithNibName:#"SplashViewController" bundle:nil];
UINavigationController *aNavigationController=[[UINavigationController alloc] initWithRootViewController:viewController1];
self.navigationcontroller = aNavigationController ;
self.navigationcontroller.navigationBar.hidden=YES;
[self.window setRootViewController:self.navigationcontroller];
[self.window makeKeyAndVisible];
}
else
{
UIViewController *viewController1;
viewController1 = [[SplashViewController alloc] initWithNibName:#"SplashViewController" bundle:nil];
UINavigationController *aNavigationController=[[UINavigationController alloc] initWithRootViewController:viewController1];
self.navigationcontroller = aNavigationController ;
self.navigationcontroller.navigationBar.hidden=YES;
//[self.window addSubview:[self.navigationcontroller view]];
//[self.window setRootViewController:viewController1];
[self.window setRootViewController:self.navigationcontroller];
[self.window makeKeyAndVisible];
}
}
This is working fine. But my problem is when the app is in active state its automatically redirect to the relavent page before click the notification. But I want to keep the apps current screen as it is when it is in the forground and when only user clk the notification redirect to the relavant page. How can I do this? Please help me.
Thanks
If your app is in active state, you can show your notification details in a UIAlertView. User will perform the action through the alert. But you cannot expect the notification in this state.
UIApplicationState appState = [application applicationState];
if (appState == UIApplicationStateActive) {
NSString *cancelTitle = #"Close";
NSString *showTitle = #"Show";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#“Your Title”
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
return;
}

IOS: Load a View Controller When App is Launched from Browser using url schemes?

I am new to iPhone developing , My app is based on video conferencing, I have a task where an app should launch from browser using URL schemes. I have done it but problem is when app launched from browser it should load particular view controller. I am using Storyboard. Here is the code which I tried.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) { return NO;
}
NSString *URLString = [url absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:#"url"];
[[NSUserDefaults standardUserDefaults] synchronize];
URLParser *parser = [[URLParser alloc] initWithURLString:URLString];
username = [parser valueForVariable:#"USERNAME"];
NSLog(#"%#", username); //b
sessid = [parser valueForVariable:#"SESSION_ID"];
NSLog(#"%#", sessid); //(null)
tokenid = [parser valueForVariable:#"token"];
NSLog(#"%#", tokenid); //yes
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
return YES;
}
This is how I do it..
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//get parameters
[self goResetPassword:dict];
}
return YES;
}
- (void) goResetPassword:(NSDictionary*) dict{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *root = [[UINavigationController alloc]initWithRootViewController:[storyboard instantiateViewControllerWithIdentifier:#"resetPasswordView"]];
self.window.rootViewController= root;
ResetPasswordViewController *vc = (ResetPasswordViewController*)[[root viewControllers] objectAtIndex:0];
[vc loadData:dict];
}
hope it helps... GL HF
According to your comment your initial ViewController is NavigationController. This is how you can push the required ViewController.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
// same code
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
// Get instance of initial Viewcontroller from storyboard
UINavigationController *navController = [storyboard instantiateInitialViewController
];
// Get instance of desired viewcontroller
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
// Push ViewController on to NavigationController
[navController pushViewController:viewController animated:NO];
return YES;
}
Check It
UIStoryboard *board = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] init];
[nav setNavigationBarHidden:NO];
IntroVC *initailView = [board instantiateViewControllerWithIdentifier:#"Intro"];
[nav pushViewController:initailView animated:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication unregisterForRemoteNotifications];

application didFinishLaunchingWithOptions ignoring if statement

I have an if statement in the App Delegate's application DidFinishLaunchingWithOptions. The code in the if statement runs even if the if statement isn't true. Am I doing something wrong? It just seems to be ignoring the if statement.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSInteger i = [[NSUserDefaults standardUserDefaults] integerForKey:#"numOfLCalls"];
[[NSUserDefaults standardUserDefaults] setInteger:i+1 forKey:#"numOfLCalls"];
if (i >= 3) {
UIAlertView *alert_View = [[UIAlertView alloc] initWithTitle:#"Hey! You are still coming back!" message:#"It would mean a whole lot to me if you rated this app!" delegate:self cancelButtonTitle:#"Maybe later" otherButtonTitles: #"Rate", nil];
[alert_View show];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You're storing this value into NSUserDefaults which doesn't get cleared when you rebuild the app. In order to reset this number you will have to uninstall the app from either the simulator or the device and rebuild.
The point of NSUserDefaults is that it is truly persistent. It will remain even if your application is updated from the app store, and the only two ways of clearing it's data are to specifically and deliberately remove the key you are referencing, or by deleting the app.
Additionally, as you can see below I've made a couple of slight tweaks for you:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] integerForKey:#"numOfLCalls"]) {
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:#"numOfLCalls"];
}else{
NSInteger i = [[NSUserDefaults standardUserDefaults] integerForKey:#"numOfLCalls"];
[[NSUserDefaults standardUserDefaults] setInteger:i++ forKey:#"numOfLCalls"];
}
if (i >= 3) {
UIAlertView *alert_View = [[UIAlertView alloc] initWithTitle:#"Hey! You are still coming back!" message:#"It would mean a whole lot to me if you rated this app!" delegate:self cancelButtonTitle:#"Maybe later" otherButtonTitles: #"Rate", nil];
[alert_View show];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

How do I go to my main view for my IOS app after logging in with Facebook Connect

I have a problem with connecting to my main view after logging in with the Facebook Connect sdk with my app. After a user logs in I want it to essentially load up the main view of my application (a camera), but so far I can't figure out how to do it. At the moment it just logs in and I am taken to a blank black screen, and I can't reach my view. Before I added the Facebook functionality I could reach my main view no problem.
Below is my appDelegate.h and appDelegate.m code, I can add the viewController code and class if it will help, my xib file is called "ViewController".
appDelegate.h
#import "FBConnect.h"
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate>
{
Facebook *facebook;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (nonatomic, retain) Facebook *facebook;
#end
appDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize facebook;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
//if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// self.ViewController = [[[ViewController alloc] initWithNibName:#"nil" bundle:nil] autorelease];
//}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
facebook = [[Facebook alloc] initWithAppId:#"326350974080015" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_photos",
nil];
[facebook authorize:permissions];
[permissions release];
}
return YES;
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
self.ViewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
#end
I am also getting this error when I try and run my app:
2012-03-01 21:04:51.103 friendSpotted[619:f803] Applications are expected to have a root view controller at the end of application launch
I actually have code for this in my appDelegate.m
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Before self.window.rootViewController = self.viewController; you need to initialize the view controller self.viewController = [[UIViewController alloc] initWithNibName:#"ViewController" bundle:nil]; assuming your xib is called "ViewController.xib"
EDIT
Also, I would put all the Facebook related stuff in your view controller not the app delegate

Resources