当前位置: 首页 > 知识库问答 >
问题:

IOS Simple rest API可以在模拟器中工作,但不能在任何设备上工作[重复]

董良策
2023-03-14

这段代码在模拟器iphone 8 Plus上按预期运行,但当运行到iphone 8 Plus或iphone 6上时,这段控制台日志将失败,导致这种奇怪的结果。Mac和两款iPhone都运行当前的操作系统版本。

import UIKit

let DomainURL = "http://museum-dev.williams.edu:3003/id?labID="

class artObject : Codable {
    var ObjectID : Int?
    var ObjectName : String?
    var Creators : String?
    var Medium : String?
    var Titles : String?
    var LabelUUID : String?

    static func fetch(withID id : String){
        let urlString = DomainURL + "\(id)"
        print("urlString is \(urlString)")

        if let url = URL(string: urlString) {
            let task = URLSession.shared.dataTask(with: url, completionHandler: { (data,response,error) in
                print(String(data: data!,encoding: .ascii) ?? "no data")

                if let newArtObj = try? JSONDecoder().decode(artObject.self, from: data!) {
                    print(newArtObj.Medium ?? "no url")
                }


            })
            task.resume()
        }
    }
}

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        artObject.fetch(withID: "E8EE71C6F5822")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

urlString为http://museum-dev.williams.edu:3003/id?labid=E8EE71C6F5822

2018-09-14 09:47:45.384385-0400 RestFul[330:16235] [] tcp_timers tcp[1] retransmit SYN 3
2018-09-14 09:47:46.389844-0400 RestFul[330:16235] [] tcp_timers tcp[1] retransmit SYN 4
2018-09-14 09:47:47.395361-0400 RestFul[330:16236] [] tcp_timers tcp[1] retransmit SYN 5
2018-09-14 09:47:49.398078-0400 RestFul[330:16234] [] tcp_timers tcp[1] retransmit SYN 6
2018-09-14 09:47:53.403567-0400 RestFul[330:16236] [] tcp_timers tcp[1] retransmit SYN 7
2018-09-14 09:48:00.098736-0400 RestFul[330:16150] Status bar could not find cached time string image. Rendering in-process.
2018-09-14 09:48:01.409005-0400 RestFul[330:16234] [] tcp_timers tcp[1] retransmit SYN 8
2018-09-14 09:48:17.412384-0400 RestFul[330:16236] [] tcp_timers tcp[1] retransmit SYN 9
2018-09-14 09:49:00.722310-0400 RestFul[330:16520] TIC TCP Conn Failed [1:0x1d4168700]: 1:50 Err(50)
2018-09-14 09:49:00.722701-0400 RestFul[330:16236] Task <5D9E2547-23CE-4959-8CF5-211114B1C533>.<1> finished with error - code: -1001
2018-09-14 09:49:00.724537-0400 RestFul[330:16520] Task <5D9E2547-23CE-4959-8CF5-211114B1C533>.<1> HTTP load failed (error code: -1009 [1:50])

共有1个答案

姚阳德
2023-03-14

尝试在info.plist中设置以下键

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

在生产中不建议这样做。它将允许您的应用程序调用不安全的URL。您需要的是有效的ssl证书。

 类似资料: