ExpSwift 5.0.0

ExpSwift 5.0.0

测试已测试
语言语言 SwiftSwift
授权协议 MIT
发布时间最后发布2017年5月
SwiftSwift 版本3.0
SPM支持 SPM

Cesar Oyarzun 维护。



 
依赖
PromiseKit= 4.0.4
Alamofire= 4.4.0
Socket.IO-Client-Swift= 8.3.3
JSONWebToken= 2.0.1
 

ExpSwift 5.0.0

  • Cesar Oyarzun

[![CI Status](http://img.shields.io/travis/Cesar Oyarzun/ExpSwift.svg?style=flat)](https://travis-ci.org/Cesar Oyarzun/ExpSwift)

示例

要运行示例项目,请克隆仓库,从 Example 目录首先运行 pod install

要求

CocoaPods (https://cocoapods.org.cn/) Swift 3(需要 Xcode 8.0)

在 iOS 9.0 中,将 Transport Security 添加到 info.plist 文件中

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

安装

ExpSwift 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

use_frameworks!
pod "ExpSwift"

或者使用特定的版本:

use_frameworks!
pod "ExpSwift", , :git => 'https://github.com/ScalaInc/exp-ios-sdk.git', :tag => 'v1.0.4'

运行时

启动 SDK

ExpSwift.start(options)

启动并返回一个 SDK 实例。可以多次调用以启动多个独立的 SDK 实例。SDK 可以使用用户、设备或消费者应用凭据启动。 options 是一个对象,支持以下属性:

  • username 登录 EXP 所使用的用户名。必需的用户凭据。
  • password 用户的密码。必需的用户凭据。
  • organization 用户所在的组织。必需的用户凭据。
  • uuid 设备或消费者应用的 uuid。
  • secret 设备密钥。
  • apiKey 消费者应用 API 密钥。必需的消费者应用凭据。
  • host 用于验证的 API 服务器。默认为 https://api.goexp.io
  • enableNetwork 是否与 EXP 网络建立套接字连接。如果 false,则无法监听广播。默认为 true
import ExpSwift

# Init exp connection for device with Host,Uuid,secret. 
  ExpSwift.start(host,"74c05552-5b9f-4d06-a3f8-8299ff1e1e3a","7b674d4ab63e80c62591ef3fcb51da1505f420d2a9ffda8ed5d24aa6384ad1c1f10985a4fc858b046b065bcdacc105dd").then{ result -> Void in
            debugPrint(result)
            }.catch { error in
                debugPrint(error)
            }

# Init exp connection for user with Host,User,Password,Organization.
  ExpSwift.start(host,"[email protected]","Com5715031","scala").then{ result -> Void in
            debugPrint(result)
            }.catch { error in
                debugPrint(error)
            }
            
# Init exp connection for user with options object.
  ExpSwift.start(["host": "https://api.exp.scala.com", "username":"[email protected]", "password":"Com5715031", "organization":"scala").then{ result -> Void in
            debugPrint(result)
            }.catch { error in
                debugPrint(error)
            }

停止 SDK

ExpSwift.stop()

停止所有正在运行的 SDK 实例,取消所有监听器和网络连接。

 ExpSwift.stop()

身份验证

ExpSwift.auth

返回当前的身份验证载荷。如果尚未认证,则将为null。

#GET USERNAME
let document = ExpSwift.auth?.getDocument()
debugPrint(document!["identity"]);
debugPrint(document!["identity"]!["username"]);

ExpSwift.on("update",callback)

当身份验证载荷更新时调用回调。

ExpSwift.on("error",callback)

当SDK实例遇到关键错误且无法继续时注册回调。回调第一个参数是错误。这通常是认证失败导致的。

ExpSwift.on("error", callback: { obj -> Void in
    debugPrint("Error on EXP SDK")
})

实时通信

状态

ExpSwift.connection(name, callback)

附加连接事件的监听器。可能的事件有 online(与EXP建立连接时)和 offline(丢失与EXP的连接时)。

ExpSwift.connection("online", { obj -> Void in
            debugPrint(obj)
        })
        
ExpSwift.connection("offline", { obj -> Void in
            debugPrint(obj)
        })

ExpSwift.isConnected()

你是否已连接到网络。

频道

exp.getChannel(name, system, consumerApp)

返回具有给定名称的频道,具有两个标志:consumerAppsystem。消费设备只能监听和广播在消费频道上。系统频道是只读的,可以接收关于系统事件的通知。

    let channel = ExpSwift.getChannel("my-channel",system: false,consumerApp: true)

channel.broadcast(name, payload, timeout)

在频道上发送具有给定namepayload的广播。等待timeout毫秒的响应,并解析为响应数组。

    var payload:Dictionary<String,Any> = ["opening":"knock knock?"]
    channel.broadcast("hi", payload: payload1, timeout: "2000").then { result -> Void in
        debugPrint(result)
    }

channel.listen(name, callback)

为具有给定name的频道上的事件注册监听器回调。当回调注册且网络连接已订阅频道时解决为监听器

回调第一个参数是广播的有效载荷。调用respond方法向广播者发送响应。

    channel.listen("myEvent",  callback: { (resultListen) -> Void in
        debugPrint(resultListen)
        //respond to listen method
        ExpSwift.respond(["text":"hi to you too"])
    })

ExpSwift.respond(payload)

调用respond方法向广播者发送带有payload的响应。

    var payload:Dictionary<String,Any> = ["opening":"knock knock?"]
    ExpSwift.respond(payload).then { result -> Void in
        debugPrint(result)
    }

channel.fling(payload)

在频道上投掷应用程序启动的有效载荷。

     let payload:Dictionary<String,Any> = ["uuid":"myUuid"]
     channel1.fling(payload)

channel.identify()

请求设备在频道上监听该事件时视觉上识别自己。实现取决于设备;这是一个便利方法。

let channel = ExpSwift.getChannel("device uui",system: false,consumerApp: false)
channel1.identify()

API

设备

设备继承所有通用资源方法和属性

ExpSwift.getDevice(uuid:String)

通过UUID获取单个设备。解决为设备

ExpSwift.getDevice("8930ff64-1063-4a03-b1bc-33e1ba463d7a").then { (device: Device) -> Void  in
         debugPrint(device.get("name"))
        }.catch { error in
         debugPrint(error)
}

ExpSwift.findDevices(params:[String:Any])

查询多个设备。解决为设备数组。

ExpSwift.findDevices(["limit":10, "skip":0, "sort":"name"]).then { (devices: SearchResults<Device>) -> Void  in
            for device in devices {
                debugPrint(device.get("name"))
            }
        }.catch { error in
            debugPrint(error)
}

ExpSwift.createDevice(document:[String:Any])

解决为根据提供的文档创建的设备。

ExpSwift.createDevice( ["name":"Device Swift","subtype":"scala:device:player"] ).then { (device: Device) -> Void in
                     debugPrint(device)
        }.catch { error in
            debugPrint(error)
    }

ExpSwift.deleteDevice(uuid:String)

ExpSwift.deleteDevice(device.getUuid()).then{ () -> Void in
        debugPrint("Device Deleted!")
    }.catch { error in
        debugPrint(error)
}

device.getLocation()

解决为设备位置null

device.getZones()

解决为设备的区域数组。

device.getExperience()

解决为设备的体验null

Device.getCurrentDevice()

解决为当前设备(#devices)null

事物

事物继承所有通用资源方法和属性

ExpSwift.getThing(uuid:String)

通过UUID获取单个事物。解决为事物

ExpSwift.getThing("8930ff64-1063-4a03-b1bc-33e1ba463d7a").then { (thing: Thing) -> Void  in
                debugPrint(thing.get("name"))
            }.catch { error in
                debugPrint(error)
}

ExpSwift.findThings(params:[String:Any])

查询多个内容。解析为事物的数组。

ExpSwift.findThings(["limit":10, "skip":0, "sort":"name"]).then { (things: SearchResults<Thing>) -> Void  in
    for thing in things {
        debugPrint(thing.get("name"))
    }
}.catch { error in
    debugPrint(error)
}

Exp.createThing(document:[String:Any])

解析为根据提供的文档创建的事物。

ExpSwift.createThing( ["name","Rfid Name","subtype":"scala:thing:rfid","id","rfid id"] ).then { (thing: Thing) -> Void in
                     debugPrint(thing)
        }.catch { error in
            debugPrint(error)
    }

ExpSwift.deleteThing(uuid:String)

ExpSwift.deleteThing(thing.getUuid()).then{ () -> Void in
        debugPrint("Thing Deleted!")
    }.catch { error in
        debugPrint(error)
}

thing.getLocation()

解决为设备位置null

thing.getZones()

解决为设备的区域数组。

thing.getExperience()

解决为设备的体验null

经历

经历继承了所有通用资源方法和属性

ExpSwift.getExperience(uuid:String)

通过UUID获取单个经历。解析为经历

ExpSwift.getExperience("58dc59e4-a44c-4b6e-902b-e6744c09d933").then { (experience: Experience) -> Void  in
    debugPrint(experience.get("name"))
}.catch { error in
        debugPrint(error)
}

ExpSwift.findExperiences(params:[String:Any])

查询多个经历。解析为经历的数组。

ExpSwift.findExperiences(["limit":10, "skip":0, "sort":"name"]).then { (experiences: SearchResults<Experience>) -> Void  in
    for experience in experiences{
        debugPrint(experience.get("name"))
    }
}.catch { error in
        debugPrint(error)
}

Exp.createExperience(document:[String:Any])

解析为根据提供的文档创建的经历。

ExpSwift.createExperience( ["name","Experience Name"] ).then { (experience: Experience) -> Void in
                     debugPrint(experience)
        }.catch { error in
            debugPrint(error)
    }

ExpSwift.deleteExperience(uuid:String)

ExpSwift.deleteExperience(experience.getUuid()).then{ () -> Void in
        debugPrint("Experience Deleted!")
    }.catch { error in
        debugPrint(error)
}

experience.getDevices()

解析为设备数组,它们是此经历的一部分。

experience.getCurrentExperience()

解析为当前经历(#experiences)或null

位置

位置继承了所有通用资源方法和属性

ExpSwift.getLocation(uuid:String)

通过UUID获取单个位置。解析为位置

ExpSwift.getLocation("3e2e25df-8324-4912-91c3-810751f527a4").then { (location: Location) -> Void  in
    debugPrint(location.get("name"))
    }.catch { error in
        debugPrint(error)
}

ExpSwift.findLocations(params:[String:Any])

查询多个位置。解析为位置的数组。

ExpSwift.findLocations(["limit":10, "skip":0, "sort":"name"]).then { (locations: SearchResults<Location>) -> Void  in
    for location in locations {
        debugPrint(location.get("name"))
    }
    }.catch { error in
        debugPrint(error)
}

Exp.createLocation(document:[String:Any])

解析为根据提供的文档创建的位置。

ExpSwift.createLocation( ["name","Location Name"] ).then { (location: Location) -> Void in
                     debugPrint(location)
        }.catch { error in
            debugPrint(error)
    }

ExpSwift.deleteLocation(uuid:String)

ExpSwift.deleteLocation(location.getUuid()).then{ () -> Void in
        debugPrint("Location Deleted!")
    }.catch { error in
        debugPrint(error)
}

location.getZones()

解析为区域数组,它们是此位置的一部分。

location.getLayoutUrl()

返回指向位置布局图像的URL。

location.getDevices()

解析为设备数组,它们是此位置的一部分。

location.getCurrentLocation()

解析为当前位置(#locations)或null

location.getDevices().then { (devices: SearchResults<Device>) -> Void  in
    for device in devices {
        debugPrint(device.get("name"))
    }
    }.catch { error in
        debugPrint(error)
}

location.getThings()

解析为事物数组,它们是此位置的一部分。

location.getThings().then { (things: SearchResults<Thing>) -> Void  in
    for thing in things {
        debugPrint(thing.get("name"))
    }
    }.catch { error in
        debugPrint(error)
}

区域

区域继承了所有通用资源方法和属性

zone.key

区域的关键。

zone.name

区域名称。

zone.getCurrentZones()

解析为当前区域或空数组。

zone.getDevices()

解析为设备数组,它们是此区域的成员。

zone.getDevices().then { (devices: SearchResults<Device>) -> Void  in
    for device in devices {
        debugPrint(device.get("name"))
    }
    }.catch { error in
        debugPrint(error)
}

zone.getThings()

解析为事物数组,它们是此区域的成员。

zone.getThings().then { (things: SearchResults<Thing>) -> Void  in
    for thing in things {
        debugPrint(thing.get("name"))
    }
    }.catch { error in
        debugPrint(error)
}

zone.getLocation()

解析为区域位置

源继承了所有通用资源方法和属性

ExpSwift.getFeed(uuid:String)

通过UUID获取单个源。解析为

ExpSwift.getFeed("3e2e25df-8324-4912-91c3-810751f527a4").then { (feed: Feed) -> Void  in
        debugPrint(feed.get("name"))
    }.catch { error in
        debugPrint(error)
}

ExpSwift.findFeeds(params:[String:Any])

查询多个源。解析为的数组。

ExpSwift.findFeeds(["limit":10, "skip":0, "sort":"name"]).then { (locations: SearchResults<Feed>) -> Void  in
    for feed in feeds {
        debugPrint(feed("name"))
    }
}.catch { error in
    debugPrint(error)
}

Exp.createFeed(document:[String:Any])

解析为根据提供的文档创建的源。

ExpSwift.createFeed( ["name","My Weather Feed","subtype","scala:feed:weather","searchValue","16902"] ).then { (feed: Feed) -> Void in
                     debugPrint(feed)
        }.catch { error in
            debugPrint(error)
    }

ExpSwift.deleteFeed(uuid:String)

ExpSwift.deleteFeed(feed.getUuid()).then{ () -> Void in
        debugPrint("Feed Deleted!")
    }.catch { error in
        debugPrint(error)
}

源对象

feed.uuid

源UUID。

feed.getData()

获取源数据。解析为源查询的输出。

    feed.getData().then { (data: [Any]) -> Void in
        debugPrint(data)
    }.catch { error in
    debugPrint(error)
    }

feed.getData(query:[String:Any])

获取源动态数据。解析为带有动态参数的源查询输出。

    feed.getData(["name":"scala"]).then { (data: [Any]) -> Void in
        debugPrint(data)
    }.catch { error in
    debugPrint(error)
    }

数据

数据继承了所有通用资源方法和属性。每个数据文档的大小限制为16MB。

ExpSwift.getData(group:String, key:String)

通过分组和键来获取单个数据项。解析为Data

ExpSwift.getData("cats", "fluffbottom").then { (data: Data) -> Void  in
        debugPrint(data.get("value"))
    }.catch { error in
        debugPrint(error)
}

ExpSwift.findData(params:[String:Any])

查询多个数据项。解析为包含Data的SearchResults对象。

ExpSwift.findData(["limit":10, "skip":0, "sort":"key", "group":"cats"]).then { (data: SearchResults<Data>) -> Void  in
    for dataItem in data {
        debugPrint(dataItem.get("value"))
    }
}.catch { error in
    debugPrint(error)
}

Exp.createData(group:String, key:String, value:[String,Any])

解析为基于提供的分组、键和值创建的数据项。

 ExpSwift.createData("test",key: "datatest", document: ["name":"Device Swift","subtype":"scala:device:player"] ).then { (data:ExpSwift.Data) -> Void in
        debugPrint(data)
        }.catch { error in
            debugPrint(error)
    }

ExpSwift.deleteData(group:String,key:String)

ExpSwift.deleteData("groupname",key: "keyName").then{ () -> Void in
        debugPrint("Data Deleted!")
    }.catch { error in
        debugPrint(error)
}

内容

内容继承所有通用资源方法和属性,除了save()函数。

ExpSwift.getContent(uuid)

通过UUID获取内容节点。解析为Content。注意:'root'的UUID值将返回当前组织的根文件夹的内容。

ExpSwift.getContent("root").then { (content: Content) -> Void  in
      debugPrint(content.get("name"))
    }.catch { error in
        debugPrint(error)
    }

ExpSwift.findContent(params:[String:Any])

查询多个内容项。解析为包含Content的SearchResults对象。

ExpSwift.findContent(["limit":10, "skip":0, "sort":"name", "name":"images"]).then { (data: SearchResults<Content>) -> Void  in
    for content in data {
        debugPrint(content.get("name"))
    }
}.catch { error in
    debugPrint(error)
}

内容对象

content.uuid

内容的UUID。

content.getChildren()

获取此内容节点的直接子节点。解析为Content数组。

 content.getChildren().then { (children: [Content]) -> Void in
    for child in children{
        debugPrint(child.get("name"))
    }
    }.catch { error in
        debugPrint(error)
    }

content.getChildren(options)

解析为包含子Content的SearchResults对象。

  content.getChildren(["id":"123"]]).then { (children: SearchResults<Content>) -> Void in
    for child in children{
        debugPrint(child.get("name"))
    }
    }.catch { error in
    debugPrint(error)
    }

content.getUrl()

获取内容节点数据的绝对URL。对图像/视频标签或下载内容文件很有用。对于文件夹返回空字符串。

let url = content.getUrl();

content.getVariantUrl(name:String)

获取内容节点变体数据的绝对URL。对图像/视频缩略图或转码视频很有用。对于文件夹或内容不包含变体,则返回空字符串。

let url = content.getVariantUrl("320.png");

模型

这些方法和属性由许多抽象API资源共享。

getUuid()

返回资源的UUID。不可以设置。

let uuid:String = data.getUuid()

get(name:String)

如果属性文档中存在指定名称,则返回该对象。

let name = data.get("name")

getChannel()

返回与该资源上下文关联的渠道名称。

let channel = data.getChannel();

getChannel(system:Bool,consumer:Bool)

返回与该资源上下文关联的渠道名称,包括system和consumer选项。

let channel = data.getChannel(false,true);

getDocument()

资源的底层文档。

let document = data.getDocument();

setProperty(name:String,value:Any)

将具有名称和值的属性设置到资源中。

device.setProperty(name: "name", value: "Device Updated")

save()

保存资源并更新文档位置。返回保存操作的承诺。

ExpSwift.getDevice("8930ff64-1063-4a03-b1bc-33e1ba463d7a").then { (device: Device) -> Void in
    device.setProperty(name: "name", value: "Device Updated SDK")
    device.save().then{(device: Device) -> Void in
        }.catch { error in
           debugPrint(error)
    }
}.catch { error in
    debugPrint(error)
}

refresh()

在位置刷新资源的底层文档。返回刷新操作的承诺。

ExpSwift.getDevice("8930ff64-1063-4a03-b1bc-33e1ba463d7a").then { (device: Device) -> Void in
    device.setProperty(name: "name", value: "Device Updated SDK")
    device.save().then{(device: Device) -> Void in
        device.refresh().then { (device: Device) -> Void in
            debugPrint("Device Refresh")
            }.catch { error in
                debugPrint(error)
            }
        }.catch { error in
           debugPrint(error)
    }
}.catch { error in
    debugPrint(error)
}

LOGGING

如果您想查看ExpSwift日志,则需要在Xcode项目窗口左侧文件导航器顶部的POD项目名称上单击。选择“构建设置”选项卡,然后向下滚动到底部附近的“Swift编译器 - 自定义标志”部分。单击“其他标志”旁边的向下箭头以展开部分。单击调试行以选择它。将鼠标光标放在行的右侧,双击。将出现一个视图列表。单击列表视图左下角的加号按钮以添加值。将一个文本字段设置为活动状态。在文本字段中,输入文本-D DEBUG并按回车键以提交行。

alt tag

作者

Cesar Oyarzun, [email protected]

许可协议

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