ios swift_使用Swift的iOS联网

ios swift

Clients can connect to the related server by using URL which contains;

客户端可以使用包含以下内容的URL连接到相关服务器:

https:// — SCHEME = specifies how to connection will happen. * HTTP is a protocol for transferring hypertext documents on the World Wide Web. HTTPS is secure(encrypted data flow) version of HTTP.

https ://:// — SCHEME =指定如何进行连接。 * HTTP是用于在万维网上传输超文本文档的协议。 HTTPS是HTTP的安全(加密数据流)版本。

medium.com/ — HOST = gets our request to the right computer

medium.com / — 主机 =将我们的请求发送到正确的计算机

@vuralkaan/PATH = our end point

@ vuralkaan /PATH =我们的终点

But the URL can contain below items;

但是URL可以包含以下项目;

scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]

方案:[// [用户[:密码] @]主机[:端口]] [/路径] [?查询] [#fragment]

Therefore, DNS will help to determine the actual IP address of the server.

因此,DNS将有助于确定服务器的实际IP地址。

Let’s say we are going to use Dog API in our application. And we have

假设我们要在应用程序中使用Dog API 。 我们有

let urlString: = “https://dog.ceo/api/breeds/image/random"

urlString:=“ https://dog.ceo/api/breeds/image/random

URL to get a random image. We need to get our string to be converted into an URL. In swift, we are able to do that any string by using

获取随机图像的URL。 我们需要将字符串转换为URL。 快速地,我们可以通过使用

let url = URL(string: urlString)

url = URL(string: urlString )

But more precisely, We need to use URLComponents as below;

但更确切地说,我们需要如下使用URLComponents

var urlComponents = URLComponents()

var urlComponents = URLComponents()

urlComponents.scheme = “https”

urlComponents .scheme =“ https”

  • We can add all items as we mentioned above. (host, path, query…)

    如上所述,我们可以添加所有项目。 (主机,路径,查询...)

We may also use add items to any url by using appendPathComponent() method. URLComponent is able to separate url as in query, host, path …

我们还可以通过使用appendPathComponent()方法将添加项添加到任何url。 URLComponent能够像查询,主机,路径中一样分隔URL。

To make a network request we use URLSession object which provides shared property. (singleton)

为了发出网络请求,我们使用提供共享属性的URLSession对象。 ( 单身人士 )

URLSession calls ‘request’ : TASK

URLSession调用“请求”: 任务

  • DataTask: Returns data from the network directly into memory as instance of the datastruct.

    DataTask :作为数据结构的实例,将数据从网络直接返回到内存中。

  • DownloadTask: Works like DataTask except the data is stored directly in a file on the device.

    DownloadTask :与DataTask相似,但数据直接存储在设备上的文件中。

  • UploadTask: To upload a file to any server.

    UploadTask :将文件上传到任何服务器。

  • StreamTask: To get a continuous stream of data from a server.

    StreamTask :从服务器获取连续的数据流。

After creating task we need to start this task by calling resume()

创建任务后,我们需要通过调用resume()来启动此任务

If we are going to use data coming from HTTP; App Transport Security blocks data coming from HTTP. In reality, some servers still use HTTP.

如果我们要使用来自HTTP的数据; App Transport Security阻止来自HTTP的数据。 实际上,某些服务器仍使用HTTP。

We need to create a key as below in info.plist

我们需要在info.plist中创建一个密钥,如下所示

Image for post

API gives you the map of what the method looks like. Web service can contain multiple APIs.

API为您提供了该方法的外观图。 Web服务可以包含多个API。

JSON is a data format often used to send data to and from web service.

JSON是一种数据格式,通常用于与Web服务之间来回发送数据。

There are 2 ways to handle JSON data in iOS.

有两种在iOS中处理JSON数据的方法。

  • JSON Serialization

    JSON序列化
  • Codable

    可编码

JSON序列化 (JSON Serialization)

Converts JSON data to and from swift dictionaries. The JSON object is treated as a dictionary.

在快速字典之间来回转换JSON数据。 JSON对象被视为字典。

  • You need to map all the keys manually

    您需要手动映射所有键
  • You need to translate all to fit into a dictionary

    您需要翻译所有内容以适合字典

可编码 (Codable)

The data is converted into a struct where you can then extract individual values. Codable is a way more modern approach. Codable even lets you convert between swift types and other formats such as XML or a custom type.

数据将转换为结构,然后可以在其中提取单个值。 可编码是一种更现代的方法。 Codable甚至允许您在快速类型和​​其他格式(例如XML或自定义类型)之间进行转换。

  • Directly matching

    直接配对
  • If the JSON’s and our struct’s property names are not equal(like if we want to use in a different name), we need to use CodingKeys to map all parameters to the related field.

    如果JSON和结构的属性名称不相等( 例如,如果要使用其他名称 ),则需要使用CodingKeys将所有参数映射到相关字段。

翻译自: https://medium.com/@vuralkaan/ios-networking-with-swift-374e0993d937

ios swift

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值