用Swift和PHP为iOS应用制作联系表单


Make contact form for iOS app with Swift and PHP

我想创建一个联系人表单,用户可以在其中给我留言(以及姓名和电话号码…),并将其发送给我。

但是我不知道怎么做,因为我从来没有处理过这样的想法。

我自己的建议是$_POST内容从TextField和TextView到我的服务器上的PHP脚本。这将处理内容。(要么用mail()发送电子邮件给我,要么将其存储在服务器上的文件中。PHP脚本对我来说没有问题。)

这样合理吗?通常的做法是什么,给开发者留下反馈或类似的东西?

注:我知道用户可以从应用内部给我发邮件,但我不喜欢这种方式。

我觉得很合理。你可以在swift中写一个函数来发送get或post参数,像这样:

func postParamRequest(resourceURL: String, postData: String, completionHandler: ((NSURLResponse?, NSData?, NSError?) -> Void)) {
    let request : NSMutableURLRequest = NSMutableURLRequest()
    request.URL = NSURL(string: resourceURL)
    request.HTTPMethod = "POST"
    request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse?, data: NSData?, error: NSError?) in
        completionHandler(response, data, error)
    })
}

然后您可以调用该函数并获得POST参数的响应、数据和任何错误的回调(postData应该是[key: value])。像这样:

postParamRequest("theUrlToYourScript", postData: "key1=value&key2=value&key3=value") { (response, data, error) in
            print(data)
}

在你的PHP脚本中,你可以这样检索数据:$_POST["paramName"]