2014-02-10 100 views
-1

我正在開發一個項目,我必須將用戶的詳細信息發送到服務器以在應用程序中註冊。我已經完成了用戶界面,但無法找到如何發送用戶詳細信息(如姓名,電話號碼,地址等)的解決方案。我需要有人來指導我從頭開始。後端已經寫好了,它在PHP中。iPhone Web服務

回答

0

它的工作原理100%

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",@"Raja",@"12345"]; 
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]]; 


[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:postData]; 

NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 

if(theConnection){ 
    // indicator.hidden = NO; 
    mutableData = [[NSMutableData alloc]init]; 
} 

你的PHP代碼

<?php 
    $username = $_POST['username']; 
    $password=$_POST['password']; 


    echo $username; 
?> 
+0

感謝。有效。 – Ashish

1

如果你已經寫完後端,你可以發送數據到服務器並通過NSURLSession在iOS 7中取回。下面的例子將以JSON格式發送用戶名和密碼到服務器。

NSError *error; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 
NSURL *url = [NSURL URLWithString:@"URL to which you want to send data"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:60.0]; 

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setHTTPMethod:@"POST"]; 
NSDictionary *data [email protected]{"userName":"Abc","password","xyz"} 
NSData *postData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error]; 
[request setHTTPBody:postData]; 


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
// write logic after geting response from the server 
}]; 

[postDataTask resume];