amitshinik amitshinik - 5 months ago 10
JSON Question

save login session credentials and loading throughout the app

I am new to iOS development. I am developing photo sharing app. In this , First i need to use Web service for Login page. Web service is in PHP and return response in JSON. I want to save login session throughout the app . When user launches the app then it always check whether the user is logged in or not. Please give me suitable solution for this very soon as there is deadline of my job if i don`t do this in early time. Here is my Code.


**<HomeKiddoAppDelegate.h file>**

#import <UIKit/UIKit.h>

@class HomeKiddoViewController;


@interface HomeKiddoAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) HomeKiddoViewController *viewController;

@end



**<HomeKiddoAppDelegate.m file>**


#import "HomeKiddoAppDelegate.h"

#import "HomeKiddoViewController.h"


@implementation HomeKiddoAppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[HomeKiddoViewController alloc] initWithNibName:@"HomeKiddoViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

//Register defaults
NSMutableDictionary *defaultsDictionary = [[NSMutableDictionary alloc] init];
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultsDictionary];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{

}

@end





> #import <UIKit/UIKit.h>
> #import "SignInViewController.h"
>
> @interface HomeKiddoViewController : UIViewController{
> SignInViewController *signInViewController;
> }
>
> -(IBAction)signInClicked:(id)sender;
>
> @end






**<HomekiddoViewController.m>**
> #import "HomeKiddoViewController.h"
>
> @interface HomeKiddoViewController ()
>
> @end
>
> @implementation HomeKiddoViewController
>
> - (void)viewDidLoad
> {
> [super viewDidLoad];
> }
>
> - (void)viewDidUnload {
> [super viewDidUnload]; }
>
> - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
> return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); }
>
> -(IBAction)signInClicked:(id)sender{
> if(signInViewController==nil){
> signInViewController=[[SignInViewController alloc]initWithNibName:@"SignInViewController" bundle:nil];
> }
> [self.view addSubview:signInViewController.view];
> } @end
>
>

**<SignInFormViewController.h>**
> #import <UIKit/UIKit.h>
> #import "SBJson.h"

>
> @interface SignInFormViewController : UIViewController
> <NSURLConnectionDelegate>

{
> IBOutlet UITextField *email1;
> IBOutlet UITextField *password1;
> NSURLConnection *conn;
> NSMutableData *webData;
> IBOutlet UITextView *textView;
> }

@

property (nonatomic, retain) IBOutlet UITextField *email1; @property
> (nonatomic, retain) IBOutlet UITextField *password1;

>
>

-(IBAction)btnSignInClicked:(id)sender;
> -(IBAction)backClicked:(id)sender;

>
> @end
>





import "SignInFormViewController.h"



@interface SignInFormViewController ()

@end

@implementation SignInFormViewController
@synthesize email1;
@synthesize password1;

- (void)viewDidLoad

{
[super viewDidLoad];
}

- (void)viewDidUnload

{
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(IBAction)btnSignInClicked:(id)sender{
NSString *queryUrl=[NSString stringWithFormat:@"Url of the web service with parameters",email1.text,password1.text];
NSURL *url=[NSURL URLWithString:queryUrl];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn)
{
webData=[NSMutableData data];
NSLog(@"in Connection if statement");
}
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{

[webData setLength: 0];
NSLog(@" inside didReceiveZResponse");
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data {
[webData appendData:data];
NSLog(@"inside did receive data");

}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error {

NSLog(@"in fail with error");

}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{

[email1 resignFirstResponder];
[password1 resignFirstResponder];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:email1.text forKey:@"email"];
[defaults setObject:password1.text forKey:@"password"];
[defaults synchronize];
}

-(IBAction)backClicked:(id)sender{
[self.view removeFromSuperview];
}


@end

Answer

You can save the login data in the NSUserDefaults like this.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:cookieString forKey:@"Cookie"];
[userDefaults setObject:pwString forKey:@"Password"];
[userDefaults synchronize];

Then you can load the user default from anywhere you like in the app with

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *myString = [defaults objectForKey:@"Cookie"];
Comments