ios开发

swift或OC中NSURLConnection finishe

2020-06-08  本文已影响0人  苍眸之宝宝

1.NSURLConnection报错1002

1002说明该请求的url无效。url由于分为远程url(及http或者https请求链接地址)和本地url(及获取的本地文件的链接地址),导致1002报错由于不同的url造成的原因不同。

2.http或者https请求链接地址1002报错

如果是http请求,则是从iOS9后,默认只支持https请求,如果要支持http请求需要设置;在OC项目的Info.plist中添加App Transport Security Settings设置,并将Allow Arbitrary Loads设置为YES。


008DBBF9-A6CF-4EFC-83FD-A5E2F8038058.png

3.获取的本地文件的链接地址1002报错

获取本地文件的方法用了获取http的方法。

        guard let booksFilePath = Bundle.main.path(forResource: "Books", ofType: "plist") else { return [] }
        guard let url = URL.init(string: booksFilePath) else {
            return []
        }
        let booksDicList = NSArray.init(contentsOf: url)

将 URL.init(string: booksFilePath) 换为 URL.init(fileURLWithPath: booksFilePath) 即可。

        guard let booksFilePath = Bundle.main.path(forResource: "Books", ofType: "plist") else { return [] }
        let booksDicList = NSArray.init(contentsOf: URL.init(fileURLWithPath: booksFilePath))
上一篇下一篇

猜你喜欢

热点阅读