将php数据传输到xcode app

时间:2012-12-08 19:33:23

标签: php xcode

我试图在我的网站上制作的php页面和我编写的xcode项目中的NSData对象之间传输数据。它的页面非常简单:

<!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 echo 'Bonjour'; ?>

</body>
</html>

我希望&#39; Bonjour&#39;要转移到我的数据对象。从这个时候开始,通过$ _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];        
    }

}

我想知道为什么响应对象包含整个php页面,而不是唯一应该显示的单词,&#39; Bonjour&#39;。

感谢您的回复

1 个答案:

答案 0 :(得分:0)

如果您只想要PHP位,请删除页面上不是PHP的其他内容。您正在进行的请求将获取该HTTP请求中的所有内容,而不仅仅是PHP位。这包括您在页面上的HTML。

或者,使用JSON移动数据。来自PHP端的json_encode(),然后使用类似JSONKit的东西在Xcode端解码它。