RxSwiftConnect 4.0.0

RxSwiftConnect 4.0.0

Sakon Ratanamalai 维护。



 
依赖
RxSwift~> 5
RxCocoa~> 5
SwiftyGif>= 0
 

  • 作者
  • Keegan Rush

RxSwiftConnect

RxSwiftConnect,也称为 Retrofit for iOS,是一个用于防御中间人攻击(MITM)的框架。根据 https://us.norton.com/,中间人攻击就像窃听一样。这是一种攻击方式,攻击者秘密地中继并可能更改相信他们在直接相互交流的两个当事人之间的通信(维基百科)。经过多年的试验和测试,与 ReactiveX.io 和 Quicktype.io 合作,RxSwiftConnect 终于诞生。

支持 RxSwift 5

pod 'RxSwiftConnect', '~> 3.0'

下面是示例项目结果;


如何编码和使用 RxSwiftConnect 框架?
1. 设置端点
let beseUrl = "https://jsonplaceholder.typicode.com"
  1. 设置 URL API 服务
func getOtherUser() -> O<R<OtherUser,E>> {
  return requester.get(path: "posts")
}
  1. 为从 API 服务接收结果数据创建模型,使用 https://quicktype.io 自动生成代码,如图所示。
import Foundation

struct OtherUserElement: Codable {
    let userID, id: Int
    let title, body: String

    enum CodingKeys: String, CodingKey {
        case userID = "userId"
        case id, title, body
    }
}

typealias OtherUser = [OtherUserElement]
  1. 完成并运行!!
apiOther.getOtherUser()
.observeOn(MainScheduler.instance)
.subscribe(onNext:{ r in

    guard let result = r.value else{
        return self.alertPopup(initMessage: r.error!.errorFriendlyEn!)
    }
    result.forEach {
        print("User ID: \($0.id), User Title: \($0.title)")
    }
},onError:{ e in
    self.alertPopup(initMessage: "Application Error")
}).disposed(by: stepBag)

RxSwiftConnect 包含错误处理,如图所示。

               


从 Quicktype.io 生成模型有以下两种方案,最佳方案是使用 JSON Schema 生成的模型,如果您的团队正在使用 C# 开发 API 服务器,则容易生成 JSON Schema,请按照以下链接 https://blog.quicktype.io/swift-types-from-csharp 操作,它帮助 iOS 开发者提升从 API 服务器到镜像模型的效率并避免由于对象为空或复杂 JSON 结构导致的错误,如图所示。
1. 复制下面的示例 JSON Schema。
{
  "definitions": {
    "BannerInfo": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Image": {
          "type": [
            "string",
            "null"
          ]
        },
        "IdentityType": {
          "type": "integer"
        },
        "BannerType": {
          "type": [
            "string",
            "null"
          ]
        },
        "PopUpText": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "Image",
        "IdentityType",
        "BannerType",
        "PopUpText"
      ]
    }
  },
  "type": "object",
  "properties": {
    "Result": {
      "type": [
        "array",
        "null"
      ],
      "items": {
        "$ref": "#/definitions/BannerInfo"
      }
    }
  },
  "required": [
    "Result"
  ]
}

2. 将 JSON Schema 粘贴到 https://app.quicktype.io 并将源类型更改为 "JSON Schema"。


或者基本解决方案通过 JSON 数据创建模型,如图所示。
1. 直接访问 https://jsonplaceholder.typicode.com/posts

2. 复制响应结果并在 https://app.quicktype.io 上发表。

RxSwiftConnect用于防范API服务器和iOS移动应用程序之间的人肉中间人攻击(MITM)。尽管API服务器上安装了SSL证书,但这并不能保证您在线数据和个人隐私的安全性。攻击者或黑客可能通过使用PortSwigger提供的一款名为Burp的软件,在网络通信中追踪您的数据。MITM攻击被OWASP列为最常见的前十种网络攻击之一,详情参见https://www.owasp.org/index.php/Mobile_Top_10_2016-Top_10 。以下是RxSwiftConnect的安装和配置截图。
1. 在Windows服务器上运行mmc,将显示以下弹出窗口。

2. 选择“计算机账户”。

3. 选择“证书”,然后点击“确定”按钮。

4. 查找与域API相关的正确证书,并从Windows服务器导出SSL证书。

5. 选择导出私钥。

6. 选择PKCS,并将所有证书都包含在内。

7. 创建密码,将用于在iOS上生成.cer文件。

8. 将文件命名为“input.pfx”。

9. 点击“完成”按钮。

10. 复制文件“input.pfx”,并将其传输到Mac。

11. 打开Mac上的文件夹。

12. 打开终端,并按以下命令操作。

13. 将会看到一个名为“mycerts.crt”的文件。

14. 在终端中运行导出证书的命令,该证书将用于XCODE。

15. 将生成一个名为“certificate.cer”的文件。

16. 将“certificate.cer”附加到iOS项目中。

17. 将RxSwiftConnect的更改状态设置为“isPreventPinning = true”,以防护MITM攻击,并运行!!
private init() {
        let beseUrl = "https://webstarter.megazy.com"
        requester = Requester(initBaseUrl: beseUrl,timeout: 5, isPreventPinning: true, initSessionConfig: URLSessionConfiguration.default)
    }

示例代码:上传UIImage和Video的文件