2012-03-08 5 views
0

このトピックに関する情報を見つけることができません。ここで私を助けてください。iPhone認証プロセス

POSTまたはGETメソッドを使用してWebサーバーに引数を渡して返信する必要があります。

基本的には、GETメソッドを使用している場合、私はserver.com/?user=john &パスワード=スミスような何かをして、私のPHPスクリプトで行われ、動的に生成されたHTMLコードを受信したいです。これは私のアプリでウェブブラウザを使わずに行うことができます。

通常どのように行われますか?

+8

は、これは非常に悪い考えです - そのようなユーザー名とパスワードを渡します! –

答えて

0

通常、これはNSURLConnectionを使用して行われます。 NSStringのメソッドstringWithContentsOfURLを使用することもできます。

1

NSMutableURLRequestNSURLConnectionを調べるとよいでしょう。これは、サーバーへのGETリクエストを行うよう

たとえば、あなたがそれらを使用することができます。

- (void)loginUser:(NSString *)username withPassword:(NSString *)password { 

    // GET 
    NSString *serverURL = [NSString stringWithFormat:@"http://yourserver.com/login.php?user=%@&pass=%@", username, password]; 

    NSURL *url = [NSURL URLWithString:serverURL]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self]; 

    if (connection) { 
     connectionData = [[NSMutableData alloc] init]; 
    } 
} 

これは、ユーザ名とパスワードを含むクエリ文字列を使用して、サーバーに非同期GETリクエストを送信します。

あなたはPOSTリクエストを使用して、ユーザー名とパスワードを送信したい場合は、この方法は、このようなものになります:サーバーからの応答を得るために

- (void)loginUser:(NSString *)username withPassword:(NSString *)password { 

     // POST 
     NSString *myRequestString = [NSString stringWithFormat:@"user=%@&pass=%@",username,password]; 
     NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 

     NSURL *url = [NSURL URLWithString:@"http://yourserver.com/login.php"]; 
     NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
     [req setHTTPMethod: @"POST"]; 
     [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
     [req setHTTPBody: myRequestData]; 

     NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self]; 

     if (connection) { 
      connectionData = [[NSMutableData alloc] init]; 
     } 
    } 

を、あなたはNSURLConnectionデリゲートを実装する必要があります例えば方法:

#pragma mark - 
#pragma mark NSURLConnection delegate methods 
#pragma mark - 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 

    // Called if you have an .htaccess auth. on server 
    NSURLCredential *newCredential; 
    newCredential = [NSURLCredential credentialWithUser:@"your_username" password:@"your_password" persistence:NSURLCredentialPersistenceForSession]; 
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
} 

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

    [connectionData setLength: 0]; 
} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    [connectionData appendData:data]; 
} 

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

    [connectionData release]; 
} 

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


    NSString *content = [[NSString alloc] initWithBytes:[connectionData bytes] 
                length:[connectionData length] encoding: NSUTF8StringEncoding]; 

    // This will be your server's HTML response 
    NSLog(@"response: %@",content); 

    [content release]; 
    [connectionData release]; 
} 

参照:

NSMutableURLRequest Class Reference

NSURLConnection Class Reference

希望はこのことができます:)

+0

"このようなデータに強くお勧めします"?いいえ、本当はそうではありません。 –

+0

クエリ文字列に資格情報を渡すのとは対照的に、それはです。あいまいさを取り除くために答えを編集しました。 – Mutix

関連する問題