-
-
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
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
| let configuration = PNConfiguration(publishKey: "demo", subscribeKey: "demo") | |
| 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
| 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. */ } | |
| } | |
| } |
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' # (or '9.0' or '10.0') | |
| target "YourProject" do | |
| pod "PubNub", "~> 4" | |
| 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" | |
| #import <PubNub/PubNub.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 PubNub | |
| 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