Giter Club home page Giter Club logo

swift's Introduction

PubNub Swift SDK

Platform CocoaPods Compatible Swift Package Manager compatible Carthage Compatible Build Status Codacy Coverage Grade Badge

This is the official PubNub Swift SDK repository.

PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.

Requirements

  • iOS 9.0+ / macOS 10.11+ / Mac Catalyst 13.0+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 11+
  • Swift 5+

The PubNub Swift SDK doesn't contain any external dependencies.

Get keys

You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.

Set up your project

You have several options to set up your project. We provide instructions here for Swift Package Manager, CocoaPods, and Carthage.

  1. Create or open your project inside of Xcode
  2. Navigate to File > Swift Packages > Add Package Dependency
  3. Search for PubNub and select the swift package owned by pubnub, and hit the Next button
  4. Use the Up to Next Major Version rule spanning from 4.0.0 < 5.0.0, and hit the Next button

For more information see Apple's guide on Adding Package Dependencies to Your App

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
  pod 'PubNubSwift', '~> 4.0'
end

Note: Replace YOUR_TARGET_NAME with your target's name.

In the directory containing your Podfile. execute the following:

pod install

Officially supported: Carthage 0.33 and up.

Add the following to Cartfile:

github "pubnub/swift" ~> 4.0

Then in the directory containing your Cartfile, execute the following:

carthage update

Configure PubNub

  1. Import the module named PubNub inside your AppDelegate:

    import UIKit
    import PubNub // <- Here is our PubNub module import.
  2. Create and configure a PubNub object:

    var config = PubNubConfiguration(
      publishKey: "myPublishKey",
      subscribeKey: "mySubscribeKey",
      uuid: "myUniqueUUID"
    )
    let pubnub = PubNub(configuration: config)

Add event listeners

// Create a new listener instance
let listener = SubscriptionListener()

// Add listener event callbacks
listener.didReceiveSubscription = { event in
  switch event {
  case let .messageReceived(message):
    print("Message Received: \(message) Publisher: \(message.publisher ?? "defaultUUID")")
  case let .connectionStatusChanged(status):
    print("Status Received: \(status)")
  case let .presenceChanged(presence):
    print("Presence Received: \(presence)")
  case let .subscribeError(error):
    print("Subscription Error \(error)")
  default:
    break
  }
}

// Start receiving subscription events
pubnub.add(listener)

NOTE: You can check the UUID of the publisher of a particular message by checking the message.publisher property in the subscription listener. You must also provide a default value for publisher, as the UUID parameter is optional.

Publish and subscribe

pubnub.publish(channel: "my_channel", message: "Test Message!") { result in
  switch result {
  case let .success(timetoken):
    print("The message was successfully published at: \(timetoken)")
  case let .failure(error):
    print("Handle response error: \(error.localizedDescription)")
  }
}

pubnub.subscribe(to: ["my_channel"])

Documentation

Support

If you need help or have a general question, contact [email protected].

License

The PubNub Swift SDK is released under the PubNub Software Development Kit License.

See LICENSE for details.

swift's People

Contributors

parfeon avatar jguz-pubnub avatar dependabot[bot] avatar techwritermat avatar craigln avatar davidnub avatar samiahmedsiddiqui avatar seba-aln avatar michaljolender avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.