ios - 从 URLSession 返回数据并保存在属性变量中

标签 ios swift api session

我尝试使用 URLSession.shared.dataTask 从服务器获取一些数据。 它工作正常,但我不能像类变量一样保存结果。 许多答案建议使用completion Handler,但这对我的任务没有帮助。

这是我的测试代码:

class PostForData {
func forData(completion:  @escaping (String) -> ()) {
    if let url = URL(string: "http://odnakrov.info/MyWebService/api/test.php") {
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        let postString : String = "json={\"Ivan Bolgov\":\"050-062-0769\"}"
        print(postString)
        request.httpBody = postString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) {
            data, response, error in
            let json = String(data: data!, encoding: String.Encoding.utf8)!
                completion(json)
        }
        task.resume()
    }
}
}
class ViewController: UIViewController {
var str:String?
override func viewDidLoad() {
    super.viewDidLoad()
    let pfd = PostForData()

    pfd.forData { jsonString in
        print(jsonString)
        DispatchQueue.main.async {
            self.str = jsonString
        }
    }
    print(str ?? "not init yet")
}
}

最佳答案

这个闭包是@escaping(即后面异步调用的),所以你必须把它放在闭包里面:

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    var str: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        let pfd = PostForData()

        pfd.performRequest { jsonString, error in
            guard let jsonString = jsonString, error == nil else {
                print(error ?? "Unknown error")
                return
            }

            // use jsonString inside this closure ...

            DispatchQueue.main.async {
                self.str = jsonString
                self.label.text = jsonString
            }
        }

        // ... but not after it, because the above runs asynchronously (i.e. later)
    }
}

请注意,我将您的闭包更改为返回 String?Error? 以便 View Controller 可以知道是否发生了错误(如果它关心,它可以看到发生了什么样的错误)。

请注意,我将您的 forData 重命名为 performRequest。通常,您会使用比这更有意义的名称,但方法名称(在 Swift 3 及更高版本中)通常应包含一个动词,指示正在做什么。

class PostForData {
    func performRequest(completion:  @escaping (String?, Error?) -> Void) {
        // don't try to build JSON manually; use `JSONSerialization` or `JSONEncoder` to build it

        let dictionary = [
            "name": "Ivan Bolgov",
            "ss": "050-062-0769"
        ]
        let jsonData = try! JSONEncoder().encode(dictionary)

        // It's a bit weird to incorporate JSON in `x-www-form-urlencoded` request, but OK, I'll do that.

        // But make sure to percent escape it.

        let jsonString = String(data: jsonData, encoding: .utf8)!
            .addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)!

        let body = "json=" + jsonString
        let url = URL(string: "http://odnakrov.info/MyWebService/api/test.php")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = body.data(using: .utf8)

        // It's not required, but it's good practice to set `Content-Type` (to specify what you're sending)
        // and `Accept` (to specify what you're expecting) headers.

        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")

        // now perform the prepared request

        let task = URLSession.shared.dataTask(with: request) { data, _, error in
            guard let data = data, error == nil else {
                completion(nil, error)
                return
            }
            let responseString = String(data: data, encoding: .utf8)
            completion(responseString, nil)
        }
        task.resume()
    }
}

该例程也有一些修改,具体是:

  1. 永远不要在处理服务器响应时使用 ! 强制解包。您无法控制请求是成功还是失败,强制解包运算符会使您的应用程序崩溃。您应该使用 guard letif let 模式优雅地解包这些可选值。

  2. 使用 json=... 模式非常不寻常,其中 ... 是 JSON 字符串。可以推断出您正在准备一个 application/x-www-form-urlencoded 请求,并使用 $_POST$_REQUEST 来获取与 json 键关联的值。通常你要么做真正的 JSON 请求,要么你做 application/x-www-form-urlencoded 请求,但不是两者都做。但是在一个请求中执行这两项操作会使客户端和服务器代码的工作量加倍。上面的代码遵循您原始代码片段中的模式,但我建议使用其中之一,但不要同时使用。

  3. 就我个人而言,我不会让 performRequest 返回 JSON 字符串。我建议它实际执行 JSON 的解析。但是,我再次将其保留在您的代码片段中。

  4. 我注意到您以 "Ivan Bolgov": "050-062-0769" 的形式使用了 JSON。我建议不要使用“值”作为 JSON 的键。键应该是有利地定义的常量。因此,例如,上面我使用了 "name": "Ivan Bolgov""ss": "050-062-0769",服务器知道在哪里寻找名为 namess 的键。在这里做任何您想做的事,但您的原始 JSON 请求似乎混淆了键(通常事先已知)和值(哪些值与这些键相关联)。

  5. 如果您要执行 x-www-form-urlencoded 请求,您必须对提供的值进行百分比编码,就像我在上面所做的那样。值得注意的是,诸如空格字符之类的字符在此类请求中是不允许的,因此您必须对它们进行百分比编码。不用说,如果您执行了正确的 JSON 请求,就不需要这些愚蠢的事情了。

    但请注意,当百分比编码时,不要试图使用默认的 .urlQueryAllowed 字符集,因为它会允许某些字符未转义地通过。所以我定义了一个 .urlQueryValueAllowed,它从 .urlQueryAllowed 字符集中删除了某些字符(改编自 Alamofire 中使用的模式):

    extension CharacterSet {
    
        /// Returns the character set for characters allowed in the individual parameters within a query URL component.
        ///
        /// The query component of a URL is the component immediately following a question mark (?).
        /// For example, in the URL `http://www.example.com/index.php?key1=value1#jumpLink`, the query
        /// component is `key1=value1`. The individual parameters of that query would be the key `key1`
        /// and its associated value `value1`.
        ///
        /// According to RFC 3986, the set of unreserved characters includes
        ///
        /// `ALPHA / DIGIT / "-" / "." / "_" / "~"`
        ///
        /// In section 3.4 of the RFC, it further recommends adding `/` and `?` to the list of unescaped characters
        /// for the sake of compatibility with some erroneous implementations, so this routine also allows those
        /// to pass unescaped.
    
        static var urlQueryValueAllowed: CharacterSet = {
            let generalDelimitersToEncode = ":#[]@"    // does not include "?" or "/" due to RFC 3986 - Section 3.4
            let subDelimitersToEncode = "!$&'()*+,;="
    
            var allowed = CharacterSet.urlQueryAllowed
            allowed.remove(charactersIn: generalDelimitersToEncode + subDelimitersToEncode)
            return allowed
        }()
    
    }
    

我建议更改您的 PHP 以接受 JSON 请求,例如:

<?php

    // read the raw post data

    $handle = fopen("php://input", "rb");
    $raw_post_data = '';
    while (!feof($handle)) {
        $raw_post_data .= fread($handle, 8192);
    }
    fclose($handle);

    // decode the JSON into an associative array

    $request = json_decode($raw_post_data, true);

    // you can now access the associative array how ever you want

    if ($request['foo'] == 'bar') {
        $response['success'] = true;
        $response['value']   = 'baz';
    } else {
        $response['success'] = false;
    }

    // I don't know what else you might want to do with `$request`, so I'll just throw
    // the whole request as a value in my response with the key of `request`:

    $raw_response = json_encode($response);

    // specify headers

    header("Content-Type: application/json");
    header("Content-Length: " . strlen($raw_response));

    // output response

    echo $raw_response;
?>

然后您可以简化请求的构建,消除我们必须对 x-www-form-urlencoded 请求执行的所有百分比编码的需要:

class PostForData {
    func performRequest(completion:  @escaping (String?, Error?) -> Void) {
        // Build the json body

        let dictionary = [
            "name": "Ivan Bolgov",
            "ss": "050-062-0769"
        ]
        let data = try! JSONEncoder().encode(dictionary)

        // build the request

        let url = URL(string: "http://odnakrov.info/MyWebService/api/test.php")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = data

        // It's not required, but it's good practice to set `Content-Type` (to specify what you're sending)
        // and `Accept` (to specify what you're expecting) headers.

        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")

        // now perform the prepared request

        let task = URLSession.shared.dataTask(with: request) { data, _, error in
            guard let data = data, error == nil else {
                completion(nil, error)
                return
            }
            let responseString = String(data: data, encoding: .utf8)
            completion(responseString, nil)
        }
        task.resume()
    }
}

关于ios - 从 URLSession 返回数据并保存在属性变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48890209/

相关文章:

ios - 为什么 Chrome for iOS 以不同方式处理 cookie?

swift - 在 Swift 中为 SceneKit 游戏使用方向键输入

api - 如何搜索多个api服务

android - Flutter 使用 http 响应头缓存 JSON 响应

ios - UITableViewCell 选择背景颜色更改不起作用

ios - 如果我再次单击同一个按钮,我将尝试撤消筛选结果

ios - AFHTTPRequestOperationManager POST 请求因网络连接丢失而失败 (-1005)

ios - 在 Watch 应用程序使用的文件中有条件地使用 UIKit

swift - “NSInvalidArgumentException”关闭

api - 为什么 LinkedIn 有 JavaScript 和 Rest API?