ZestySwiftContentEndpointWrapper 0.3.2

ZestySwiftContentEndpointWrapper 0.3.2

Ronak Shah维护。



 
依赖
SwiftyJSON~> 4.0
Alamofire~> 4.7
 

ZestySwiftContentEndpointWrapper

Version License Platform

关于

ZestySwiftContentEndpointWrapper 是一个包装库,允许您轻松地从您的 Zesty.io 网站访问数据

用法

  1. 安装 ZestySwiftContentEndpointWrapper
  2. 初始化
  3. 使用基本 JSON API
  4. 使用自定义 JSON 端点
  5. 示例项目

安装 ZestySwiftContentEndpointWrapper

ZestySwiftContentEndpointWrapper 使用 Cocoapods

只需将其添加到您的 Podfile 中。

pod 'ZestySwiftContentEndpointWrapper'
pod 'SwiftyJSON' # required for custom JSON Endpoints

(示例 Podfile)

初始化

例如,为您的网站 http://burger.zesty.site 创建一个 ZestyAPI 对象

 // Create the ZestyAPI Object
 let zesty = ZestySwiftContentEndpointWrapper(url: "http://burger.zesty.site")
  • 注意:如果您的网站没有SSL证书(HTTPS),您需要配置您的应用程序以允许非HTTPS调用。如何更改此设置

使用基本JSON API

  1. 在配置中启用基本JSON API
  2. 获取您正在寻找的项目/数组的zuid
  3. 使用相应的函数(getItemgetArray

zuid的含义及使用函数

  • 6开头的任何zuid都是一个项目数组(使用getArray
  • 7开头的任何zuid是一个对象(使用getItem

获取单个项目

getItem(for: zuid: String, completionHandler: (([String:String], ZestyError?) -> Void)

获取特定zuid的[String : String] json数据。

您可以通过查看Zesty的内容标签来找到zuid。

例如,获取zuid为6-9bfe5c-ntqxrs的特定项目

 // Create the ZestyAPI Object
 let zesty = ZestySwiftContentEndpointWrapper(url: "http://burger.zesty.site")
 let zuid = "6-9bfe5c-ntqxrs"

 zesty.getItem(for: zuid, { (item) in
     print(item) // item is a [String : String] dictionary, in JSON Format
 }

获取数组

getArray(for: zuid: String, completionHandler: (([[String:String]], ZestyError?) -> Void)

获取特定zuid的[[String : String]]数组json数据。

您可以通过查看Zesty的内容标签来找到zuid。

例如,获取zuid为7-9bfe5c-ntqxrs的特定项目

 // Create the ZestyAPI Object
 let zesty = ZestySwiftContentEndpointWrapper(url: "http://burger.zesty.site")
 let zuid = "7-9bfe5c-ntqxrs"
    
 zesty.getArray(for: zuid, { (items) in
     for item in items {
         print(item) // item is a [String : String] dictionary, in JSON Format
     }
}

使用自定义JSON端点

getCustomJSONData(from: String, params: [String: String], completionHandler: ((JSON, ZestyError?) -> Void)

获取指定端点和参数的JSON对象。

  • 注意:ZestyAPI使用SwiftyJSON来处理JSON对象。务必确保将pod 'SwiftyJSON'添加到您的Podfile中,以便使用返回的数据。
  • 返回的JSON对象的引用方法可以在这里找到。
  • 如果想要使用不同类型的JSON解析,可以通过以下方法从JSON对象中提取原始数据:json.rawString(options: [.castNilToNSNull: true])。有关提取原始JSON字符串的更多信息,请参见此处

在Zesty接口中创建自定义JSON端点的完整教程可以在此处找到

示例用法

例如,使用自定义端点menulist(包括扩展仅适用于不同文件类型;.json在其他情况下是隐含的)

 // Create the ZestySwiftContentEndpointWrapper Object
 let zesty = ZestySwiftContentEndpointWrapper(url: "http://burger.zesty.site")
 let endpoint = "menulist"
 let parameters = ["location" : "San Diego"]
 getCustomJSONData(from: endpoint, params: parameters, { (json) in
     print(item) // item is a [String : JSON] dictionary of type JSON
 }

使用自定义端点(通用)

getCustomData(from: String, params: [String: String], completionHandler: ((Data, ZestyError?) -> Void)

如果您想使用非JSON数据,可以使用此函数获取任何类型的Data。返回的Data对象是Swift标准的Data对象。

示例用法

例如,使用自定义端点menulist(包括扩展仅适用于不同文件类型;.json在其他情况下是隐含的)

 // Create the ZestySwiftContentEndpointWrapper Object
 let zesty = ZestySwiftContentEndpointWrapper(url: "http://burger.zesty.site")
 let endpoint = "someplace"
 let parameters = ["location" : "San Diego"]
 getCustomData(from: endpoint, params: parameters, { (json) in
     print(item) // item is a Data object dictionary
 }

获取图片

getImage(for: String, completionHandler: (UIImage, ZestyError?) -> Void)

第一个参数是任何提供图像的兼容格式的URL(如UIImage所述)

使用zesty图像zuid

在Zesty中获取图像zuid的URL需要创建一个图像端点

图像endpoint文件

{
    {{ if {get_var.id} }}
        "url" : "{{ get_var.id.getImage()}}"
    {{ end-if}}
}

然后,您可以使用getCustomJSONData与getImage结合使用,以获取所需的照片UIImage

示例用法

在从其他数据请求中获取 image zuid 3-6a1c0cb-cgo7w 后,我们使用 getCustomJSONData 和 getImage 来获取我们的数据

代码

// Create the ZestySwiftContentEndpointWrapper Object
let zesty ZestySwiftContentEndpointWrapper(url: "http://burger.zesty.site")
let endpoint = "image" // created to look as the above code details
let parameters = ["id" : "3-6a1c0cb-cgo7w"]
zesty.getCustomJSONData(from: endpoint, params: parameters { (json, error) in
   if (error != nil) {
       // error handling
       return
   }
   let imageURLString = json["url"].stringValue
   zesty.getImage(imageURLString) { (image, error) in
       if error != nil {
          imageView.image = image // image is now a UIImage object
       }
    }
}  

示例项目

为了帮助您开始,我们还创建了一个示例项目

作者

Ronak Shah for Zesty.io

许可协议

ZestySwiftContentEndpointWrapper 在 MIT 许可协议下可用。更多信息请参阅 LICENSE 文件。