Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from ToeJamson/1.bash
Last active November 15, 2017 21:05
Show Gist options
  • Select an option

  • Save domadev812/733baa5dbbf8ddbb56a5e67146ec373c to your computer and use it in GitHub Desktop.

Select an option

Save domadev812/733baa5dbbf8ddbb56a5e67146ec373c to your computer and use it in GitHub Desktop.
Tutorial: Share Your Current Location From Your iOS Device in Real-Time With PubNub
$ pod init
$ {your favorite editor} Podfile
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("didFailWithError: \(error.description)")
let errorAlert = UIAlertView(title: "Error", message: "Failed to Get Your Location", delegate: nil, cancelButtonTitle: "Ok")
errorAlert.show()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newLocation = locations.last as CLLocation
println("current position: \(newLocation.coordinate.longitude) , \(newLocation.coordinate.latitude)")
}
let configuration = PNConfiguration(publishKey: "demo", subscribeKey: "demo")
private var channel = PNChannel()
self.client.addListener(self)
self.client.subscribeToChannels(["my_channel1","my_channel2"], withPresence: false)
// Handle new message from one of channels on which client has been subscribed.
func client(_ client: PubNub, didReceiveMessage message: PNMessageResult) {
// Handle new message stored in message.data.message
if message.data.channel != message.data.subscription {
}
else {
}
print("Received message: \(message.data.message) on channel \(message.data.channel) " +
"at \(message.data.timetoken)")
}
// Handle subscription status change.
func client(_ client: PubNub, didReceive status: PNStatus) {
if status.operation == .subscribeOperation {
if status.category == .PNConnectedCategory || status.category == .PNReconnectedCategory {
let subscribeStatus: PNSubscribeStatus = status as! PNSubscribeStatus
if subscribeStatus.category == .PNConnectedCategory {
}
else {
}
}
else if status.category == .PNUnexpectedDisconnectCategory {
}
else {
let errorStatus: PNErrorStatus = status as! PNErrorStatus
if errorStatus.category == .PNAccessDeniedCategory {
}
else {
}
}
}
else if status.operation == .unsubscribeOperation {
if status.category == .PNDisconnectedCategory {
}
}
else if status.operation == .heartbeatOperation {
if !status.isError { /* Heartbeat operation was successful. */ }
else { /* There was an error with the heartbeat operation, handle here. */ }
}
}
class YourViewController: UIViewController, CLLocationManagerDelegate, PNDelegate {
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newLocation = locations.last as CLLocation
println("current position: \(newLocation.coordinate.longitude) , \(newLocation.coordinate.latitude)")
let message = "{\"lat\":\(newLocation.coordinate.latitude),\"lng\":\(newLocation.coordinate.longitude), \"alt\": \(newLocation.altitude)}"
PubNub.sendMessage(message, toChannel: self.channel, compressed: true)
}
platform :ios, '8.0' # (or '9.0' or '10.0')
target "YourProject" do
pod "PubNub", "~> 4"
end
target "YourProjectTests" do
end
$ pod install
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PNImports.h"
#import <PubNub/PubNub.h>
#endif
import UIKit
import PubNub
import CoreLocation
private var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.requestAlwaysAuthorization()
class YourViewController: UIViewController, CLLocationManagerDelegate {
<key>NSLocationAlwaysUsageDescription</key>
<string>This application needs access to your location information</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This application needs access to your location information</string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment