Last active
October 18, 2017 19:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ pod init | |
| $ {your favorite editor} Podfile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| self.locationManager.desiredAccuracy = kCLLocationAccuracyBest | |
| self.locationManager.startUpdatingLocation() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { | |
| let newLocation = locations.last as CLLocation | |
| println("current position: \(newLocation.coordinate.longitude) , \(newLocation.coordinate.latitude)") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var config = PNConfiguration(forOrigin:"your.company.com", publishKey: "publish_key", subscribeKey: "subscribe_key", secretKey:nil) | |
| private var channel = PNChannel() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PubNub.setDelegate(self) | |
| PubNub.setConfiguration(self.config) | |
| PubNub.connect() | |
| self.channel = PNChannel.channelWithName("Channel-Name", shouldObservePresence: false) as PNChannel | |
| PubNub.subscribeOnChannel(self.channel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class YourViewController: UIViewController, CLLocationManagerDelegate, PNDelegate { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| platform :ios, "8.0" | |
| target "YourProject" do | |
| pod 'PubNub', '3.7.2' | |
| end | |
| target "YourProjectTests" do | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ pod install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifdef __OBJC__ | |
| #import <UIKit/UIKit.h> | |
| #import <Foundation/Foundation.h> | |
| #import "PNImports.h" | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import CoreLocation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var locationManager = CLLocationManager() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.locationManager.delegate = self | |
| self.locationManager.requestAlwaysAuthorization() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class YourViewController: UIViewController, CLLocationManagerDelegate { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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