iphone changing the initial scene - objective-c

In my iphone application I have two scenes, both of them are view controllers.
Normally initial scene is set to the first one. When user plays it passes to the second one.
At this point when the user closes the app and restarts it, if the user has already played, it should start with the second scene, so the initial scene should change for that user.
How can I do this?
And i dont use a navigation controller.

If you are using xibs, you set this up in the app delegates
applicationDidFinishLaunchingWithOptions.
Create a global BOOL and check it's value on applications launch, E.g:
.h
#class ViewController;
#class SecondViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) SecondViewController *SecondViewController;
.m
#import "ViewController.h"
#import "SecondViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if (isSecondViewControllerBool == YES) {
self.SecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}else{
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
For storyboards:
In the first view controllers viewDidLoad.
- (void)viewDidLoad
{
if (isSecondViewControllerBool == YES) {
UIViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:#"mySecondViewController"];
[self presentViewController:secondVC animated:NO completion:nil];
}else{
//present normally
}
}
If you are using storyboards and have decided to use my xib solution, delete the storyboard from the project, and remove it here as well:

Related

Storing User Name in Keychain in objective c

I am new IOS to Development .I want to know about the Keychain process for storing the user name .My concept is ..if User is already logged in i need to show a password screen if not i need to show a user name screen to enter .
When i need to show the user name screen means ,in two scenarios
1)Not yet user logged in -First Time Login
2)Logged in and logout from the app by pressing logout button in password screen
This much of code only i did in app delegate ,here what i am doing is i am just storing the username in NSUserDefaults and if user name exists then i am showing second screen i.e., Password screen .
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *controller = (ViewController *)self.window.rootViewController;
NSUserDefaults *User = [NSUserDefaults standardUserDefaults];
NSString *userid = [User stringForKey:#"userName"];
if (userid != nil)
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// determine the initial view controller here and instantiate it with
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"PassVC"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
else
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// determine the initial view controller here and instantiate it with
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"LoginVC"];
self.window.rootViewController = viewController;
//viewController.man
[self.window makeKeyAndVisible];
}
return YES;
}
Now , What i want is ,instead of storing userid in NSUser Defaults, i want to store userid in keychain .I don't have knowledge about the keychain .i searched in google but i didn't found anything clearly .Whether i need to include any frameworks for keychain ??Please anyone help me with the example code .Thanks in Advance !!!
Thanks of the reply .. i have created the keychain .when i tried to print the keychain object it is showing nil.i don't know why?
this is my code..
This is my ViewController.h
#import <UIKit/UIKit.h>
#import "KeychainItemWrapper.h"
#interface ViewController : UIViewController {
KeychainItemWrapper *keyChain ;}
#property (weak, nonatomic) IBOutlet UIView *sub_View;
#property (weak, nonatomic) IBOutlet UITextField *UserId_txt;
#property (weak, nonatomic) IBOutlet UIButton *clickMe_btn;
- (IBAction)btn1:(id)sender;
#end
This is my View Controller .m
#import "ViewController.h"
#import "PasswordViewController.h"
#import "KeychainItemWrapper.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.UserId_txt.delegate = self;
NSString *userid = self.UserId_txt.text;
[keyChain setObject:userid forKey:#"user_id"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (IBAction)btn1:(id)sender {
NSString *user =[keyChain objectForKey:#"user_id"];
NSLog(#" the user id is :%#",user);
PasswordViewController *PassVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PassVC"];
[self.navigationController pushViewController:PassVC animated:YES];
}
#end

Setting the view programmatically

I am trying to set my project without using storyboards in xcode and with objective c.
My appDelegate:
.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
.m
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
return YES;
}
etc...
My viewController file:
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
}
I think my code is right and we should have a red screen when I run it, but I only get a black screen. Can someone tell me if I have forgotten something or is it something to do with the project settings. Thanks.
Add
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
to your application:didFinishLaunchingWithOptions:
You are missing two steps.
Initialize the window:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
Make the window key & visible:
[self.window makeKeyAndVisible];
So all together, your code should look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize the window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// Add the view controller to the window
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// Make window key & visible
[self.window makeKeyAndVisible];
return YES;
}

Is it possible to get a NSArray from AppDelegate?

Is it possible to get a NSArray from my AppDelegate? I need to send a array to other classes. And that array is generated in my AppDelegate. Thanks!
First of all you should have to alloc and init
In AppDelegate.h file
#property(nonatomic,retain)NSArray *books;
In AppDelegate.m file your array, then You can add objects in it.
_books = [[NSArray alloc]initWithObjects:#"test",#"test", nil];
You should have to create AppDelegate instance in your BooksDetailViewController like this,
in .h file
AppDelegate *appDelegate;
and in .m file
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
Now you can access your array like this,
NSLog(#"Test Log :%#",appDelegate.books);
Output :
2016-07-14 13:04:08.211 Gridz[2490:39240] Test Log :(
test,
test
)
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Now you can use any property or method like:
[appDelegate myProperty] or [appDelegate myMethod]
Hope this helps
It is possible to get the NSArray from AppDelegate to other classes.
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
}
#property (nonatomic, strong) UIWindow *window;
#property (nonatomic, strong) ViewController *viewController; //For Xib
#property (nonatomic, strong) NSArray *arrayObjects;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize arrayObjects;
As I use Xib, I set the root view controller in below method.If you use stroy board, it is enough to add NSArray objects only.Don't need to set the root view controller.
//For XIB
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:#"Steve",#"jobs",#"Tim",#"Cook",nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[navController setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}
//For Storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:#"Steve",#"jobs",#"Tim",#"Cook",nil];
return YES;
}
Then in ViewController.m
You can also import the AppDelegate in ViewController.m
#import "ViewController.h"
#import "AppDelegate.h"
#interface ViewController ()
#end
#implementation ViewController
Now in viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(#"The app delegate NSArray Objects are - %#",delegate.arrayObjects);
}
The NSLog results are
The app delegate NSArray Objects are - (
Steve,
jobs,
Tim,
Cook
)

Impossible to presentViewController

I've created a new application view based.
Here some the main code:
// AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
MIAPreferences *preferences;
}
#property (strong, nonatomic) UINavigationController *navigationController;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) MIAPreferences *preferences;
// AppDelegate.m
#implementation AppDelegate
#synthesize window = _window;
#synthesize navigationController;
#synthesize preferences;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
// Override point for customization after application launch.
UIViewController *rootController = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
navigationController.navigationBar.hidden = YES;
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
// HomeViewController.m
-(IBAction)openLogin
{
LoginViewController *view = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
// 1
[self.navigationController presentViewController:view animated:YES completion:nil];
// 2
[self.navigationController pushViewController:view animated:YES];
// 3
[self presentViewController:view animated:YES completion:nil];
}
All 3 options returns a EXC_BAD_ACCESS (code=2, address=....)
Can you please help me understand how to solve this?
UPDATE
The problem was due to a UIButton apparence set up during AddDelegate loading....but the first button was in the UIView I was going to load... that's why it crashed -.-"
It could either mean: (1) The pointer used to point to memory that was ok, but its chunk was deallocated or (2) The pointer is corrupt. You can try to use zombie mode, if you aren't already, to get more information. In XCode press the Command, option, and I keys and a screen should pop up. Selected the Run option in the left hand side, then Diagnostics, and under memory management enable zombie objects should be checked.
Check the value of view after initWithNibName:. init'ing with a nil nib name looks wrong to me, and you may be trying to push a nil view controller onto your navigation stack.

Who's know why black area is occured in iOS Simulator Retina 4-inch?

When I test in iOS Simulator Retina 3.5-inch, it is well work.
but when in Retina 4-inch, it is occured black area like following a picture.
I can't know this reason...
Would you give me a some idea?
Following is AppDelegate.h & m
#import <UIKit/UIKit.h>
#import "MenuViewCon.h"
#class TestViewCon;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TestViewCon *viewController;
#end
- (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 = [[[TestViewCon alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[TestViewCon alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Next is TestViewCon.h & m
#import <Foundation/Foundation.h>
#interface TestViewCon : UIViewController
#end
#import "TestViewCon.h"
#implementation TestViewCon {
}
- (void)viewDidLoad {
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease];
label.text = #"Retina 4 inch testing";
[label sizeToFit];
[self.view addSubview:label];
}
#end
This is because you app runs letterboxed.
It seems, that you don't support the iphone 5 within your project.
To do that, you need to add a Default-568h#2x.png Image to your project and set it as the launchimage for the iPhone 5.