How to run something once when the app is launched? - iphone

I'm trying to play an intro movie when my app is getting launch, but am totally driven crazy already, I did a lot of testing in my code, along with trying to use this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(#"App started to launch");
[self.window makeKeyAndVisible];
[self.window addSubview:_intro.view];
return YES;
}
but that's make my video run, even if I'm coming from the background.
If like pressing the middle button, then double pressing the middle button and pressing app icon, I get the movie to play again.
I forget to mention that this is my ViewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL launchedBefore = [userDefaults boolForKey:#"hasRunBefore"];
NSLog(#"value of hasRunBefore is %d",launchedBefore);
if(!launchedBefore)
{
[userDefaults setBool:1 forKey:#"hasRunBefore"];
launchedBefore = [userDefaults boolForKey:#"hasRunBefore"];
NSLog(#"value of hasRunBefore is %d",launchedBefore);
[self playvideo];
}
}

NSUserDefaults does not commit the changes to disk until you send the synchronize message.
Try adding doing this:
[userDefaults setBool:YES forKey:#"hasRunBefore"];
[userDefaults synchronize];

Related

Root View Controller not changing on BOOL

I am trying to change the Root View controller if the application has been opened once. Here is what im doing. The boolean is fully functional and i know because it prints 'Welcome Back'. Here is what im trying to do.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
// App Already launched
RootMenuController *viewController = [[RootMenuController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = viewController;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
// This is the first launch ever
}
[self.window makeKeyAndVisible];
return YES;
}
It seems like you are not setting/updating application window properly. I have created an sample project which might help. https://github.com/deepthit/UpdateRootViewController.git

NSUserDefaults - How should I fix this code?

I'm trying to make some sort of setup wizard that remembers a specific view however I have some problems with my AppDelegate.m code since I'm using ARC.
Does anyone know how to fix this, because If I compile the app crashes at the splash screen..
My AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *controllerName = [[NSUserDefaults standardUserDefaults] objectForKey:#"WIZARD_VIEW"];
if ([controllerName length]) {
Class controllerClass = NSClassFromString(controllerName);
UIViewController *controller = [[controllerClass alloc] init];
// Override point for customization after application launch.
return YES;
}
To be more clear, in the viewcontroller files I added the following code as a suggestion:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSUserDefaults standardUserDefaults] setObject:[[self class] description] forKey:#"WIZARD_VIEW"];
NSLog(#"ViewWillAppear Done.");
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"WIZARD_VIEW"];
NSLog(#"viewWillDisappear");
[[NSUserDefaults standardUserDefaults] synchronize];
}
Crashlog while compiling:
/Users/Tim/Documents/XCODE_DEV/App/AppDelegate.m:17:33: No visible #interface for 'NSUserDefaults' declares the selector 'forKey:'
/Users/Tim/Documents/XCODE_DEV/App/AppDelegate.m:20:27: Unused variable 'controller'
The first time you have to register the defaults.To register the defaults isn't enough to call the setObject:forKey method, you have to use the registerDefaults method:
- (void)registerDefaults:(NSDictionary *)dictionary;
Documentation:
NSUserDefaults
So in your case:
NSUserDefaults* standard=[NSUserDefaults standardUserDefaults];
[standard registerDefaults: #{ #"WIZARD_VIEW" : [[self class]description]} ];
You better place these lines in the initialize method, which gets called before any non-static method.If the defaults are already registred there's no problem, this will not overwrite the defaults that you have if is the 2nd+ time that the application starts.
Looking at the compiler warnings, I'm going to guess that in your AppDelegate.m, on line 17 you are calling forKey: on NSUserDefaults.
NSUserDefaults doesn't have a forKey: method, so it throws an exception.
It's probably just a square bracket in the wrong place.

How to Redirect to home tab after login Iphone App?

In my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//Get uer ID from user defaults
NSString *userid = [defaults objectForKey:#"UserId"];
if([userid isEqualToString:#""]){
login = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
[window addSubview:login.view];
} else {
[window addSubview:[rootTabBarController view]];
}
[self.window makeKeyAndVisible];
return YES;
}
and after login success i have this code
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:serverOutput forKey:#"UserId"];
//show tabbar app
NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate];
[appsDelegate.window addSubview:[appsDelegate.rootTabBarController view]];
so the question is how can i redirect to specific tab [Home tab FOR EXAMBLE]?
[appDelegate.rootTabBarController setSelectedIndex:GiveIndexOfTab]; // give index of home tab for ex. 0
If you want to jump to a specific tab without intercepting the tabBar, rather than the first default tab
use
[appsDelegate.rootTabBarController setSelectedIndex:2]; // 2 is a pseudo index i have assigned
For tabbar controller,
SelectedIndex propery helps you to manage its behavior.
Use
setSelectedIndex:Index
method. It'll solve your purpose.
Thanks.

Application windows are expected to have a root view controller at the end of application launch

I am using the facebook iOS SDK setup tutorial: https://developers.facebook.com/docs/mobile/ios/build/
After Step 4: Adding Log Out to your App,
I get a blank white screen on the 5.1 simulator (xcode 4.3.2) and the console shows a message:
Application windows are expected to have a root view controller at the end of application launch
EDIT-1
Thanks for your responses;
I chose a "Single View Application" template while creating the app. In the MainStoryBoard.storyboard, I created an object and assigned the MyGreatIOSAppAppDelegate class to it. Drag-dropped the viewController outlet of this object to the View Controller.
here is the code in MyGreatIOSAppAppDelegate.m
#import "MyGreatIOSAppAppDelegate.h"
#import "xxxViewController.h"
#implementation IJSAppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize facebook;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Add the logout button
UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logoutButton.frame = CGRectMake(40, 40, 200, 40);
[logoutButton setTitle:#"Log Out" forState:UIControlStateNormal];
[logoutButton addTarget:self action:#selector(logoutButtonClicked)
forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:logoutButton];
facebook = [[Facebook alloc] initWithAppId:#"id" 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]) {
[facebook authorize:nil];
}
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
// Method that gets called when the logout button is pressed
- (void) logoutButtonClicked:(id)sender {
[facebook logout];
}
- (void) fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]) {
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
}
#end
Check that you have the following line in your application delegate's application:didFinishLaunchingWithOptions: method:
self.window.rootViewController = self.viewController;
Make sure you set your window.rootviewcontroller = your _navigationviewcontroller. Like this: (this all happens in your AppDelegate.cs file)
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow _window;
UINavigationController _navigationcontroller;
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
_window = new UIWindow (UIScreen.MainScreen.Bounds);
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
_navigationcontroller = new UINavigationController();
_navigationcontroller.PushViewController(new SplashViewController(),false);
**_window.RootViewController = _navigationcontroller;**
// make the window visible
_window.MakeKeyAndVisible ();
return true;
}
}
}

iPhone didFinishLaunchingWithOptions Method

So i'm trying to use this method to load a certain theme based on what the user chose in the settings bundle. When I insert NSLog it will load the default them (Modern Theme), but it will never change to the Pink Theme.
Is this method loaded every time the app is launched, even if the app is still running in the background.
Otherwise, where else could I do this, if I want to use the settings bundle.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userDefaultsDefaults = [NSDictionary dictionaryWithObjectsAndKeys: #"Modern Theme", #"theme", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsDefaults];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
theme =[defaults objectForKey:#"theme"];
NSLog(#"%#", theme);
if ([theme isEqualToString:#"Modern Theme"]) {
viewController = [[viewTwo alloc] initWithNibName:#"viewTwo" bundle:nil];
}
else {
viewController = [[viewOne alloc] initWithNibName:#"viewOne" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Have you tried putting your code in applicationDidBecomeActive:? That one is guaranteed to be called whether on the initial launch or resuming from the background.
Reference docs.
The method
- (void)applicationWillResignActive:(UIApplication *)application
is fired when the app is resumed from the background.
I'd nearly load the theme in the ViewController's
-(void)viewDidAppear:(BOOL)animated
method.