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;
}
Related
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];
}
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;
}
I'm working on a very old project that was done with the bare minimum of NIBS.
I've added the new Default images so that the screen now loads to iPhone5 size as confirmed when I see the image on the iPhone5 Simulator.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
[self.window setFrame:[[UIScreen mainScreen] bounds]];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
{
if ([[standardUserDefaults stringForKey:#"user"] caseInsensitiveCompare:#"(null)"] == NSOrderedSame)
{
//NSLog(#"Setting user to default_user");
[standardUserDefaults setObject:#"default_user" forKey:#"user"];
[standardUserDefaults setObject:#"default_user" forKey:#"password"];
[standardUserDefaults setObject:#"NO" forKey:#"logged_in_status"];
[standardUserDefaults synchronize];
}
}
UINavigationController *localNavigationController;
self.tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
SettingsVC *firstViewController;
firstViewController = [[SettingsVC alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[localNavigationController.tabBarItem initWithTitle:#"Settings" image:[UIImage imageNamed:#"icn_new_request.png"] tag:1];
firstViewController.navigationItem.title=#"Settings";
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[firstViewController release];
VideoMenuVC *secondViewController;
secondViewController = [[VideoMenuVC alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[localNavigationController.tabBarItem initWithTitle:#"Video" image:[UIImage imageNamed:#"icn_existing.png"] tag:2];
secondViewController.navigationItem.title=#"Video";
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[secondViewController release];
LoginVC *thirdViewController;
thirdViewController = [[LoginVC alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
[localNavigationController.tabBarItem initWithTitle:#"Log On/Off" image:[UIImage imageNamed:#"icn_phone.png"] tag:3];
thirdViewController.navigationItem.title=#"Log On/Off";
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[thirdViewController release];
self.tabBarController.viewControllers = localControllersArray;
self.tabBarController.selectedIndex = 1;
[localControllersArray release];
self.user_name = [[NSString alloc ]init];
self.user_name = #"defaultFolder";
NSLog(#"Frame Height: %f",self.window.frame.size.height);
NSLog(#"Tab Height: %f",self.tabBarController.view.frame.size.height);
NSLog(#"Tab Origin Y: %f",self.tabBarController.view.frame.origin.y);
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
When the Launch image appears it takes up the full area of the iPhone 5 Simulator screen.
When the app loads in fully and VideoMenuVC is displayed you can see that it is not using the full area. The white area under the tabbar should not be there.
Though looking at the NSLogs of the views height they seem correct.
I've been looking at this for a couple of hours and maybe I cant see the obvious because I'm looking too hard. I'd appreciate some fresh eyes/ideas as to what I've done wrong.
Many Thanks,
Code
There is nothing found in your code which can cause this. I will recommend to check the size of the icon-images. In one of my project, I have used images of 60 * 60 or 70 * 60 pixels.
I'm working on a project which is created last year.
Recently, i would like to integrated facebook SDK.
However, when i add FacebookSDK, #import <FacebookSDK/FacebookSDK.h> to appdelegate.h and build, it show errors in ACACcountType.m file:
"pasting formed "Pasting formed '__NSi_#', an invalid preporcessing token".
please see the screenshot here:
I've studied and tried to change [C++ language dialect] and [C language dialect] in
[build settings] --> [Apple LLVM 5.0] but nothing change.
i'm got stuck here. Could you help me please. Many thanks.
There has been a lot of changes to the Facebook SDK in the recent months, the new SDK does not require you to add anything to the Appdelegate.h, if you have downloaded them look at the sample apps and follow them:
in appdelegate.m you should have something like this (note there are more things that need to be added regarding FBsession control in the other parts of the app delegate) :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FBProfilePictureView class];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *message =localNotif.alertBody;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:localNotif.alertAction
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
//facebook items deprecated all items below are obsolete
/*
facebookController = [[QRSMFacebookVC alloc] init];
facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:facebookController];
// Check and retrieve authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];}
facebookController.facebook = facebook;
facebookController.userPermissions = userPermissions;
*/
// Initialize user permissions
// userPermissions = [[NSMutableDictionary alloc] initWithCapacity:10];
// Override point for customization after application launch.
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Although the SDK attempts to refresh its access tokens when it makes API calls,
// it's a good practice to refresh the access token also when the app becomes active.
// This gives apps that seldom make api calls a higher chance of having a non expired
// access token.
[FBSession.activeSession handleDidBecomeActive];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// if the app is going away, we close the session object
[FBSession.activeSession close];
}
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
BOOL ret = [FBSession.activeSession handleOpenURL:url];
return;
}
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;
}