我是 iOS 开发的新手。我正在开发照片共享应用程序。在此,首先我需要为登录页面使用 Web 服务。Web 服务使用 PHP 并以 JSON 格式返回响应。我想在整个应用程序中保存登录会话。当用户启动应用程序时,它总是检查用户是否登录。如果我不及早这样做,请尽快给我合适的解决方案,因为我的工作有最后期限。这是我的代码。
**<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
>
导入“SignInFormViewController.h”
@interface SignInFormViewController ()
@结尾
@implementation SignInFormViewController @synthesize email1; @合成密码1;
- (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]; }
@结尾