使用变量打开URL

时间:2012-11-04 08:37:43

标签: php ios objective-c xcode

所以我在我的服务器上有一个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中实现它。

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

3 个答案:

答案 0 :(得分: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连接/请求附带的服务器身份验证。 要允许任何服务器证书,您可以依赖以下解决方案(尽管这只是建议用于快速原型设计和测试): How to use NSURLConnection to connect with SSL for an untrusted cert?

希望有所帮助。

答案 1 :(得分:1)

用于此,NSMutableURLRequest。

将setHTTPMethod设置为GET方法。

对于每个值定义:

[request setValue:value forHTTPHeaderField:key];

答案 2 :(得分:1)

尝试一下:

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 />";
    }

?>