{"id":25731175,"url":"https://github.com/zetapush/zetapush-swift","last_synced_at":"2025-08-12T15:06:10.494Z","repository":{"id":56931507,"uuid":"88141460","full_name":"zetapush/zetapush-swift","owner":"zetapush","description":"ZetaPush Client for Swift","archived":false,"fork":false,"pushed_at":"2018-09-26T09:11:39.000Z","size":310,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-02T02:01:51.852Z","etag":null,"topics":["ios","ios-sdk","ios-swift","swift","zetapush"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zetapush.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-13T08:15:51.000Z","updated_at":"2018-09-18T08:38:24.000Z","dependencies_parsed_at":"2022-08-21T05:50:16.944Z","dependency_job_id":null,"html_url":"https://github.com/zetapush/zetapush-swift","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zetapush%2Fzetapush-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zetapush%2Fzetapush-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zetapush%2Fzetapush-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zetapush%2Fzetapush-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zetapush","download_url":"https://codeload.github.com/zetapush/zetapush-swift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240782840,"owners_count":19856771,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","ios-sdk","ios-swift","swift","zetapush"],"created_at":"2025-02-26T02:37:04.328Z","updated_at":"2025-02-26T02:37:04.905Z","avatar_url":"https://github.com/zetapush.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Swift client for ZetaPush\n\n[![Version](https://img.shields.io/cocoapods/v/ZetaPushSwift.svg?style=flat)](http://cocoapods.org/pods/ZetaPushSwift)\n[![License](https://img.shields.io/cocoapods/l/ZetaPushSwift.svg?style=flat)](http://cocoapods.org/pods/ZetaPushSwift)\n[![Platform](https://img.shields.io/cocoapods/p/ZetaPushSwift.svg?style=flat)](http://cocoapods.org/pods/ZetaPushSwift)\n\n## Getting Started\n\nThese instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.\n\n### Prerequisites\n\nYou must have [Cocoapods](https://cocoapods.org/) installed and running\n\n```\nsudo gem install cocoapods\n```\n\n### Installing\n\nFirst create a new project with XCode.\nIn the project directory launch the command\n\n```console\npod init\n```\n\nA PodFile is created, edit it and add ZetaPushSwift pod\n\n```\n# Uncomment the next line to define a global platform for your project\n# platform :ios, '9.0'\n\ntarget 'ZetaPushTestPod' do\n  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks\n  use_frameworks!\n\n  # Pods for ZetaPushTestPod\n  pod \"ZetaPushSwift\"\n\nend\n\n```\n\nFinally, run pod install to install libraries\n\n```console\npod install\n```\n\nYou can now open the newly generated workspace (.xcworkspace) in XCode\n\n:warning: **Close the previously openned project and open the workspace instead.**\n\n### Pre-requisit\n\nYou must have an account on [ZetaPush](https://zetapush.com) and a valid SandboxId. FYI, on ZetaPush a SandboxId identify your hosted application on ZetaPush cloud.\n\nFor the examples above, you must have deployed an authentication ( [Weak](https://ref.zpush.io/#it_weak) at least) and an [Echo](https://ref.zpush.io/#it_echo) service\n\nYou can read the [full documentation](https://doc.zetapush.com/how/introduction/) for more information\n\n## Any questions?\n\n* [Frequently Asked Questions](./FAQ.md)\n\n## Code your first client\n\n### Connecting to ZetaPush\n\nYou must import the library into your swift file\n```swift\nimport ZetaPushSwift \n\n```\n\nCreate a ZetaPushWeakClient object. This object will do all the hard work for you to connect to ZetaPush with a weak authentication\n\n```swift\nimport UIKit\nimport ZetaPushSwift\n\nclass ViewController: UIViewController {\n\n    let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: \"YOUR_SANDBOX_ID\", weakDeploymentId: \"YOUR_WEAK_DEPLOYMENT_ID\")\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        // Connect to ZetaPush\n        zetaPushWeakClient.connect()\n    }\n}\n\n```\n\nThat's it. You're connected!\n\nTo verify that you're really connected, let's add a callback to be warned when the connection is established.\n\n```swift\nimport UIKit\nimport ZetaPushSwift\n\nclass ViewController: UIViewController, ClientHelperDelegate {\n\n    let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: \"YOUR_SANDBOX_ID\", weakDeploymentId: \"YOUR_WEAK_DEPLOYMENT_ID\")\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        // Handle lifecycle events\n        zetaPushWeakClient.delegate = self\n\n        // Connect to ZetaPush\n        zetaPushWeakClient.connect()\n    }\n\n    // Callback fired when a successful handshake occurs\n    func onSuccessfulHandshake(_ client:ClientHelper){\n        // The connection to ZetaPush is a success\n        print(\"ViewController SuccessfulHandshake\")\n    }\n}\n\n```\n\n### Call your first service\n\nNow we can call a useless Echo Service that will \"echo\" ie send back everything we send to him.\n\nIf you're not familiar with the publish-subscribe paradigm, you can get more information on our [documentation website](https://doc.zetapush.com/how/principles/)\n\nTo summarize, when you send a message to ZetaPush (Publish), you get the response only if you've have previously asked for it (Subscribe).\n\nFor the Echo service, you publish a message on the verb \"echo\" and you get the response on the verb \"echo\". The [reference site](https://ref.zpush.io/#it_echo) describe all the verb you can publish and the corresponding subscription verb.\n\nBack to the code !\n\n```swift\nimport UIKit\nimport ZetaPushSwift\n\nclass ViewController: UIViewController, ClientHelperDelegate {\n\n    let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: \"YOUR_SANDBOX_ID\", weakDeploymentId: \"YOUR_WEAK_DEPLOYMENT_ID\")\n    \n    // Declare a new service\n    var zetaPushServiceEcho : ZetaPushService?\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \n        // Handle lifecycle events\n        zetaPushWeakClient.delegate = self\n\n        // Create an echo service with the corresponding DeploymentId\n        zetaPushServiceEcho = ZetaPushService(zetaPushWeakClient, deploymentId: \"YOUR_ECHO_DEPLOYMENT_ID\")\n\n        // Subscribe to the echo verb\n        _ = zetaPushServiceEcho?.subscribe(verb: \"echo\", block: { (messageDict) in\n            print(\"ViewController zetaPushServiceEcho?.subscribe\", messageDict)\n        })\n\n        // Connect to ZetaPush\n        zetaPushWeakClient.connect()\n    }\n\n    // Callback fired when a successful handshake occurs\n    func onSuccessfulHandshake(_ client:ClientHelper){\n        // The connection to ZetaPush is a success\n        print(\"ViewController SuccessfulHandshake\")\n    }\n\n    // Just call the echo service when we click on a button\n    @IBAction func OnMyFirstTestDown(_ sender: Any) {\n        zetaPushServiceEcho?.publish(verb: \"echo\", parameters: [\"hello\": \"world\" as NSObject])\n    }\n\n}\n\n```\n\nThat's it, you've called your fist ZetaPush service.\n\nRemenber that a lot of services are available right out of the box on ZetaPush. You can view a full description on our [reference website](https://ref.zpush.io)\n\n## Much more power with Macros \n\n### ZMS overview\n\nYou can read an overview of ZMS (ZetaPush Macro Scripts) on our [documentation website](https://doc.zetapush.com/how/zms-language/introduction/)\n\nYou can also follow the [QuickStart](https://doc.zetapush.com/quickstart/) to install the minimum software to create your first macro.\n\n### Our first macro\n\nLes create a simple macro with Eclipe. This macro is the default one when you create a new project (with a return on channel __selfName).\n\n```\n/**\n * Takes a message as input, and returns it, with a server message\n */\nmacroscript welcome(/** message from the client */ string message = \"Hello\") {\n\t// ...\n} return {clientMessage : message, serverMessage : WELCOME_MESSAGE} on channel __selfName\n\n```\n\nNow, let's call it in our iOS project. There's two way to call a macro: with a publish-subscribe style or with a promise.\n\n### Call a macro with Publish-Subscribe\n\nBack to code !\n\n```swift\nimport UIKit\nimport ZetaPushSwift\n\nclass ViewController: UIViewController, ClientHelperDelegate {\n\n    let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: \"YOUR_SANDBOX_ID\", weakDeploymentId: \"YOUR_WEAK_DEPLOYMENT_ID\")\n\n    // Declare a new Macro Service\n    var zetaPushMacroService: ZetaPushMacroService?\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \n        // Handle lifecycle events\n        zetaPushWeakClient.delegate = self\n\n        // Create a new Macro Service (with default deployementId: \"macro_0\")\n        zetaPushMacroService = ZetaPushMacroService(zetaPushWeakClient)\n\n        // Subscribe to the welcome verb\n        _ = zetaPushMacroService?.subscribe(verb: \"welcome\", block: { (messageDic) in\n            print(\"ViewController zetaPushMacroService?.subscribe\", messageDic)\n        })\n\n        // Connect to ZetaPush\n        zetaPushWeakClient.connect()\n    }\n\n    // Callback fired when a successful handshake occurs\n    func onSuccessfulHandshake(_ client:ClientHelper){\n        // The connection to ZetaPush is a success\n        print(\"ViewController SuccessfulHandshake\")\n    }\n\n    // Just call the macro service when we click on a button\n    @IBAction func OnMyFirstTestDown(_ sender: Any) {\n        zetaPushMacroService?.call(verb: \"welcome\", parameters: [\"message\": \"hello world\" as NSObject])\n    }\n\n}\n```\n\n### Call a macro with a promise\n\nPromise a really usefull in asynchronious macro call. For more information you can read more about promise on [wikipedia](https://en.wikipedia.org/wiki/Futures_and_promises)\n\nWith ZetaPush IOs SDK, you can call a macro in a \"nearly\" synchronous way thanks to promises. \n\n```swift\nimport UIKit\nimport ZetaPushSwift\n\nclass ViewController: UIViewController {\n\n    let zetaPushWeakClient:ZetaPushWeakClient = ZetaPushWeakClient(sandboxId: \"YOUR_SANDBOX_ID\", weakDeploymentId: \"YOUR_WEAK_DEPLOYMENT_ID\")\n\n    // Declare a new Macro Service\n    var zetaPushMacroService: ZetaPushMacroService?\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \n        // Handle lifecycle events\n        zetaPushWeakClient.delegate = self\n\n        // Create a new Macro Service (with default deployementId: \"macro_0\")\n        zetaPushMacroService = ZetaPushMacroService(zetaPushWeakClient)\n\n        // Connect to ZetaPush\n        zetaPushWeakClient.connect()\n    }\n\n    // Callback fired when a successful handshake occurs\n    func onSuccessfulHandshake(_ client:ClientHelper){\n        // The connection to ZetaPush is a success\n        print(\"ViewController SuccessfulHandshake\")\n    }\n\n    // Just call the macro service when we click on a button\n    @IBAction func OnMyFirstTestDown(_ sender: Any) {\n        zetaPushMacroService?.call(verb: \"welcome\", parameters: [\"message\": \"hello world\" as NSObject])\n            .then{ result in\n               print (\"call result\", result)\n            }\n            .catch { error in\n                print (\"call error\", error)\n            }\n    }\n\n}\n```\n\nThat's it, you are able to call a service and a macro from ZetaPush. \n\n## Auto-generated code with ZetaPush CLI\n\nWith ZetaPush command line, you can generate a swift code based on zms language. This generated code will give you everything you need to use your macors.\n\nThis generated code will create 4 files:\n\n### Structure file\n\nIn this file, you will get all the structures (classes) needed to call your macro: the input classes, the output clases and the completion classes.\n\n### Async API\n\nIn this file, a class is generated with all the methods needed to call the macros.\n\n### Promise API\n\nIn this file, a class is generated with all the methods needed to call the macros. The main difference between the Async API is that the methods return a promise.\n\n### AsyncAPIListener\n\nIn this file, a class is generated with all the method needed to listen to the result of the macros.\n\n## How to use auto-generated code\n\n### Create a class that inherit from AsyncAPIListener\n\nIn this class, you will override the methods that fits your needs.\n```swift\nopen class MyAPIListener: MacrosAsyncApiListener {\n    // Only override the method i'm interesting in\n    open override func sendMessage(_ parameter: SendMessageCompletion){\n        print(\"sendMessage\")\n    }\n}\n```\n\nWhen you will create a MyAPIListener object, the sendMessage function will be called each time the corresponding macro is called.\n\n### Use the AsyncApi or PromiseAPI objects\n\n```swift\n// Create the objects with a ZetaPushClient parameter\n\twebRTCAsyncApi = WebRTCAsyncApi(zetaPushClient)\n        webRTCPromiseApi = WebRTCPromiseApi(zetaPushClient)\n\t\n\t//To call a promise API\n\t\n\twebRTCPromiseApi?.createPublicRoom(parameters: _createPublicRoomInput)\n            .then { result -\u003e Void in\n                \n                print (\"createPublicRoom\", result.result.room!)\n                \n            }\n            .catch { error in\n                print (\"createPublicRoom error\", error)\n        }\n\t\n\t// To call the same api with the asyncAPI\n\twebRTCAsyncApi?.createPublicRoom(parameters: _createPublicRoomInput)\n\t// The result will be returned to the AsyncAPIListener\n\t\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzetapush%2Fzetapush-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzetapush%2Fzetapush-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzetapush%2Fzetapush-swift/lists"}