1

URL リクエストが成功したときに、ユーザーを定義済みの View Controller に誘導する方法を教えてください。ユーザー名とパスワードのフィールドがあるログイン ページがあります。以下は私のコードです。どんな援助でも大歓迎です。

#import "LoginTableViewController.h"

@interface LoginTableViewController ()
@property (weak, nonatomic) IBOutlet UITextField *UIEmailTextField;
@property (weak, nonatomic) IBOutlet UITextField *UIPasswordTextField;

@end

@implementation LoginTableViewController
@synthesize UIEmailTextField;
@synthesize UIPasswordTextField;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)viewDidUnload
{
[self setUIEmailTextField:nil];
[self setUIPasswordTextField:nil];
[super viewDidUnload];
}

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

#pragma mark - barButton Outlet

- (IBAction)loginButtonPressed:(UIBarButtonItem *)sender {
NSString *data = [NSString stringWithFormat:@"username=%@&password=%@",UIEmailTextField.text, UIPasswordTextField.text];
NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

// preaparing URL request to send data.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
NSString *url = [NSString stringWithFormat:@"http://www.twitter.com"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[request setTimeoutInterval:7.0];

NSURLResponse *response;
NSError *error;

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

NSLog(@"Login response:%@",str);

NSLog(@"Log In button was pressed!");
}

@end
4

3 に答える 3

0

現在の設定方法では、NSURLConnectionは終了するまでメインスレッドをブロックします。非同期NSURLConnectionを使用する場合は、そのデリゲートになり、次のように失敗と成功に適切に応答できます。

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];  //Conform to NSURLConnectionDelegate before you use this.

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
    //Got a response.  Not necessarily an error, so login might have been successful,
    //or it might not have.
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    //The connection returned data.  Really only useful for requesting files.
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    //Handle the error properly
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    //Connection's finished completely.  Probably a good time to check for an error,
    //Or change the view controller 
}

また、補足として、プロパティはObjCの大文字で始めるべきではありません。これは、クラスが使用する場合です。そして、変数名のUI-名前空間に浸らないことが常に賢明です。

于 2012-10-20T20:02:55.223 に答える
0

ログインページを作成しようとしている場合は、別のアプローチをお勧めします。ログインに成功すると最初に表示されるページを表示し、それをルートビューコントローラーにします。次に、すぐに、ログインコントローラーをモーダルコントローラーとしてプッシュします。これにより、ログインに失敗した場合でも、ログイン画面を再度押すことができます。これにより、ユーザーが自分の資格情報を保持し、起動後に再度ログインする必要がない場合(該当する場合)、コードがクリアされます。とにかく、それはよりクリーンなアプローチです。rootViewControllerレベルに戻るログイン画面はありません。

于 2012-10-20T20:03:25.160 に答える
0

LoginTableViewControllerログインに成功した後に を閉じると適切なビュー コントローラが表示されるように、ビュー コントローラをセットアップします。

例えば:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *rootViewController = [[UIViewController alloc] init];
    self.window.rootViewController = rootViewController;

    LoginTableViewController *loginViewController = [[LoginTableViewController alloc] init];
    [rootViewController presentViewController:loginViewController animated:NO completion:nil];

    return YES;
}

ログインが成功したら、次を閉じますLoginTableViewController

[self dismissViewControllerAnimated:YES completion:nil];
于 2012-10-20T20:13:54.783 に答える