2012-12-04 64 views
0

我正在嘗試在我的網站上創建的php頁面和我寫的xcode項目中的NSData對象之間傳輸數據。 PHP頁面非常簡單,它得到:php頁面和xcode項目之間的數據傳輸

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> 
     <head> 
      <title>Mon MEGA BLOG</title> 
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
     </head> 
     <style type="text/css"> 
    form 
     { 
      text-align:center; 
     } 
    </style> 


    <body> 

<?php 'Bonjour'; ?> 

</body> 
</html> 

我想「卓悅」被轉移到我的數據對象。從現在開始,通過$ _GET方法發送特定對象是沒有問題的(儘管它已經在下面的代碼中準備好了)。我只想用echo函數測試我的php頁面的響應。我的Xcode項目進展:

if(![loginText.text isEqualToString:@""] && ![passwordText.text isEqualToString:@""]) 
    { 
     //1. creation de la request 
     NSMutableString *string = [[NSMutableString alloc] initWithCapacity:200]; 
     NSString *string01 = @"http://attheshop.fr/index.php?prof=&pass="; 

     [string appendFormat:string01]; 
     NSString *string02 = loginText.text; 
     NSString *string03 = passwordText.text; 

     [string insertString:string03 atIndex:41]; 
     [string insertString:string02 atIndex:36]; 

     request01 =[NSURLRequest requestWithURL:[NSURL URLWithString:string]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

     NSLog(@"%@", string); 
     //2.creation de la connection et start loading data 

     NSURLConnection *connection01 = [[NSURLConnection alloc] initWithRequest:request01 delegate:self]; 

     if(connection01) 
     { 
     //3.Create NSMutableData to receive data 
     receivedData = [NSMutableData data]; 
      NSLog(@"Voici ce que contient le message %@", receivedData.description); 
     } 

    } 
} 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // This method is called when the server has determined that it // has enough information to create the NSURLResponse. 
    // It can be called multiple times, for example in the case of a // redirect, so each time we reset the data. 
    // receivedData is an instance variable declared elsewhere. 
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    // Append the new data to receivedData. // receivedData is an instance variable declared elsewhere. 
    [receivedData appendData:data]; 
} 

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

    // release the connection, and the data object 
    // inform the user 

    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 

    //set an alert 
    UIAlertView *alert01 = [[UIAlertView alloc] initWithTitle:@"Probleme de connexion" message:@"La connexion a échoué" delegate:self cancelButtonTitle:@"Recommençer" otherButtonTitles:nil]; 
    alert01.alertViewStyle = UIAlertViewStyleDefault; 
    [alert01 show]; 
} 


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

    // do something with the data // receivedData is declared as a method instance elsewhere 

    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

    //6.Mettre les informations téléchargées dans un objet data envoyé à home. 

    NSData *response = [NSURLConnection sendSynchronousRequest:request01 returningResponse:nil error:nil]; 
    //NSLog(@"response %@", response.description); 

    NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
    NSLog(@"get %@", get.description); 

    NSString *string04 = @"error"; 
    NSPredicate *errorTest = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", string04]; 
    BOOL defError = [errorTest evaluateWithObject:get]; 

    NSLog(@"%i",defError); 

    if (!defError) 
    { 
     //Si pas d'erreur, on lance le segue 
     [self performSegueWithIdentifier:@"loginMainSegue" sender:self]; 
    } 

    //5.Si mauvais login ou password message alerte erreur 

    if (defError) 
    { 
     UIAlertView *alert02 = [[UIAlertView alloc] initWithTitle:@"Erreur" message:@"Votre login ou votre password est erroné" delegate:self cancelButtonTitle:@"Recommençer" otherButtonTitles:nil]; 
     alert02.alertViewStyle = UIAlertViewStyleDefault; 
     [alert02 show];   
    } 

} 

在我的輸出畫面讓我得到以下結果結束: - receivedData.description =什麼(我想有「卓悅」) - get.description =完整的PHP頁面,這表明交換髮生了。

你能幫我看看我的代碼出了什麼問題嗎?

謝謝

回答

1

簡單地調用<?php 'Bonjour' ?>不會顯示任何你需要echo的字符串。

例如

<?php echo 'Bonjour'; ?> 

,或者使用新的PHP語法

<?='Bonjour'?> 
+0

<?=是用於回波的簡寫,但不一定是新。 +雖然 –

+0

謝謝,只是拼寫錯誤。但它仍然不能正常工作 –

+0

您是否可以通過打印響應內容來驗證xcode項目在HTML中接收到的內容@Vicolapatate –