Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Last active October 18, 2017 19:38
Show Gist options
  • Select an option

  • Save ToeJamson/904c436f86647ae81ad8 to your computer and use it in GitHub Desktop.

Select an option

Save ToeJamson/904c436f86647ae81ad8 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)")
}
private var config = PNConfiguration(forOrigin:"your.company.com", publishKey: "publish_key", subscribeKey: "subscribe_key", secretKey:nil)
private var channel = PNChannel()
PubNub.setDelegate(self)
PubNub.setConfiguration(self.config)
PubNub.connect()
self.channel = PNChannel.channelWithName("Channel-Name", shouldObservePresence: false) as PNChannel
PubNub.subscribeOnChannel(self.channel)
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"
target "YourProject" do
pod 'PubNub', '3.7.2'
end
target "YourProjectTests" do
end
$ pod install
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PNImports.h"
#endif
import UIKit
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