0

所以我在我使用的服务器上有一个 php 注册脚本。

如何输入文本框(或与这些框的内容相关的变量/指针),并将它们放入 url 以注册用户。再次,PHP 和 SQL 数据库都设置好了,我只是在 iOS 中遇到了麻烦。

例子:

VARIABLE --- PURPOSE
uname        username
pass         password
name         first name
lname        last name

现在我需要把它扔到这样的网址中

https://mywebsite/register.php?username=uname&password=pass&firstname=name&lastname=lname&sumbit=submit

该脚本运行良好,我只需要在 iOS 中实现它的帮助。

非常感谢提前。你们真棒!

4

3 回答 3

2

您可以将 NSURLConnection 与 NSMutableURLRequest 结合使用,如下所示:

//Prepare URL for post
NSURL *url = [NSURL URLWithString: @"https://mywebsite/register.php"];

//Prepare the string for your post (do whatever is necessary)
NSString *postString = [@"username=" stringByAppendingFormat: @"%@&password=%@", uname, pass];

//Prepare data for post
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

//Prepare request object
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setTimeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//send post using NSURLConnection
NSURLConnection connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

//Connection should never be nil
NSAssert(connection != nil, @"Failure to create URL connection.");

这(使用适合您需要的 postString)应该异步(在您的应用程序的后台)向您的服务器发送一个 POST 请求,并运行您服务器上的脚本。

假设你的服务器也返回了一个答案,你可以听如下:
当创建连接时NSURLConnection connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];,我们告诉连接我们处理服务器在同一个对象中返回的内容,即 self.

当连接返回答案时,将调用以下三个方法(将它们与上面的代码放在同一个类中):

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
    //Initialize received data object        
        self.receivedData = [NSMutableData data];
        [self.receivedData setLength:0];

    //You also might want to check if the HTTP Response is ok (no timeout, etc.) 
}

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{     
    [self.receivedData appendData:data];
}

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"Connection failed with err");
}

-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    //Connection finished loading. Processing the server answer goes here.

}

请记住,您必须处理 https 连接/请求附带的服务器身份验证。要允许任何服务器证书,您可以依赖以下解决方案(尽管这只是推荐用于快速原型设计和测试): 如何使用 NSURLConnection 与 SSL 连接以获得不受信任的证书?

希望有帮助。

于 2012-11-04T13:09:08.043 回答
1

用于此,NSMutableURLRequest。

将 setHTTPMethod 设置为 GET 方法。

为每个值定义:

[request setValue:value forHTTPHeaderField:key];
于 2012-11-04T08:46:46.720 回答
1

Give this a try:

url.php

<?php 
    //https://mywebsite/register.php?username=uname&password=pass&firstname=name&lastname=lname&sumbit=submit

    function make_safe_for_use($var){
        // run some validation here
        return($var);
    }


    if($_POST){
        //$url_username = $_POST['username']; // <--- this will work fine... but no validation...
        $url_username = make_safe_for_use($_POST['username']);
        $url_password = make_safe_for_use($_POST['password']);
        $url_firstname = make_safe_for_use($_POST['firstname']);
        $url_lastname = make_safe_for_use($_POST['lastname']);


        if($url_username > "" && $url_password > "" && $url_firstname > "" && $url_lastname > ""){
            // if all of the variables are set, then use them....
            $url = "register.php?username=".$url_username."&password=".$url_password."&firstname=".$url_firstname."&lastname=".$url_lastname."&sumbit=submit";
            header("Location: ".$url);
            exit(); // prevents any more code executing and redirects to the url above
        }
    }

?>
<form action="url.php" method="post">
    (Remove the value="donkeykong" part of each of the following, here for demo)<br /><br />
    Username <input type="text" name="username" id="username" value="donkeykong" /><br />
    Password <input type="password" name="password" id="password" value="cheese123" /><br />
    Firstname <input type="text" name="firstname" id="firstname" value="bruce" /><br />
    Lastname <input type="text" name="lastname" id="lastname" value="wayne" /><br />
    <input type="submit" value="go" />
</form>

register.php

<?php

    //print_r($_GET); // - for testing

    if($_GET){

        $url_username = $_GET['username'];
        $url_password = $_GET['password'];
        $url_firstname = $_GET['firstname'];
        $url_lastname = $_GET['lastname'];

        echo "username: ".$url_username."<br />";
        echo "password: ".$url_password."<br />";
        echo "firstname: ".$url_firstname."<br />";
        echo "lastname: ".$url_lastname."<br />";
    }

?>
于 2012-11-04T10:09:32.687 回答