Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving iOS 8 Permission issue #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ WorldPin

_This README was derived from [our post on Medium](https://medium.com/megabits-lab/aba8796ff48d)_.

Recently, we released [SIOSocket](https://github.com/megabits/siosocket), an open source Objective-C client for [socket.io 1.0](http://socket.io/blog/introducing-socket-io-1-0/). Our last post was all about the motivation for and implementation of SIOSocket, but in this post, we’re building a thing!
Recently, we released [SIOSocket](https://github.com/megabits/siosocket), an open source Objective-C client for [socket.io 1.0.4](http://socket.io/blog/introducing-socket-io-1-0/). Our last post was all about the motivation for and implementation of SIOSocket, but in this post, we’re building a thing!

## Node.js App ##

Expand Down Expand Up @@ -51,6 +51,7 @@ Finally, add two event listeners to handle the various events our clients will b

```js
socket.on('location', function (data) {
console.log('Location data' + socket['id'] +' has this data ' + data);
socket.broadcast.emit('update', (socket['id'] + ':' + data));
});

Expand All @@ -60,9 +61,44 @@ socket.on('disconnect', function () {
});
```

Add these two event listeners to inside io.on connect.

```js
io.sockets.on('connection', function (socket) {
socket.broadcast.emit('join', socket['id']);
console.log(socket['id'] + ' has connected!');
socket.on('location', function (data) {
console.log('Location data' + socket['id'] +' has this data ' + data);
socket.broadcast.emit('update', (socket['id'] + ':' + data));
});
socket.on('disconnect', function () {
socket.broadcast.emit('disappear', socket['id']);
console.log(socket['id'] + ' has disconnected!');
});
});
```

We’ll listen for __location__ events and establish our own custom __disconnect__ event (which occurs after socket.io’s standard __disconnect__ event, as a sort of middleware). When we receive the __location__ event, we’ll broadcast an __update__ event, with the string `"id:data"`, in response. And for the __disconnect__, we will broadcast a __disappear__ event, with the `id` as content. Clients who receive this __disappear__ event will then remove pins labeled with the `id` from the map. For debugging, again, we’ll include a console printout of the socket which has disconnected.

That’s it! With these ~15 lines of code, you’ll be handling real-time communication! To launch, simply trigger `node app.js` in the command line. (Bonus: you can use `DEBUG=socket-io* node app.js` to activate Node’s verbose debugging tools.)
That’s it! With these ~10 lines of code (shown below), you’ll be handling real-time communication! To launch, simply trigger `node app.js` in the command line. (Bonus: you can use `DEBUG=socket-io* node app.js` to activate Node’s verbose debugging tools.)

```js
var io = require('socket.io')(3000);

io.sockets.on('connection', function (socket) {
socket.broadcast.emit('join', socket['id']);
console.log(socket['id'] + ' has connected!');
socket.on('location', function (data) {
console.log('Location data' + socket['id'] +' has this data ' + data);
socket.broadcast.emit('update', (socket['id'] + ':' + data));
});
socket.on('disconnect', function () {
socket.broadcast.emit('disappear', socket['id']);
console.log(socket['id'] + ' has disconnected!');
});
});

```

## iOS App ##

Expand Down
12 changes: 6 additions & 6 deletions WorldPin/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6154.21" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6153.13"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -13,11 +14,11 @@
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" showsUserLocation="YES" translatesAutoresizingMaskIntoConstraints="NO" id="jKF-tH-kYQ">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="jKF-tH-kYQ">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<connections>
<outlet property="delegate" destination="vXZ-lx-hvc" id="wHS-iw-k4e"/>
</connections>
Expand All @@ -30,7 +31,6 @@
<constraint firstItem="jKF-tH-kYQ" firstAttribute="top" secondItem="kh9-bI-dsS" secondAttribute="top" id="eLY-IR-CS1"/>
<constraint firstItem="jKF-tH-kYQ" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" id="qO1-eD-bbX"/>
</constraints>
<simulatedOrientationMetrics key="simulatedOrientationMetrics" orientation="landscapeRight"/>
</view>
<connections>
<outlet property="mapView" destination="jKF-tH-kYQ" id="C6m-lj-V47"/>
Expand Down
4 changes: 3 additions & 1 deletion WorldPin/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>X</string>
<string>Location is required for this application.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Location is required for this application.</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
Expand Down
15 changes: 13 additions & 2 deletions WorldPin/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import <SIOSocket/SIOSocket.h>
#import "WPAnnotation.h"

@interface ViewController () <MKMapViewDelegate>
@interface ViewController () <MKMapViewDelegate, CLLocationManagerDelegate>

@property IBOutlet MKMapView *mapView;

Expand All @@ -29,8 +29,19 @@ - (void)viewDidLoad
{
[super viewDidLoad];

if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
if(IS_OS_8_OR_LATER) {
[self.locationManager requestWhenInUseAuthorization];
}

self.mapView.userTrackingMode = MKUserTrackingModeFollow;

self.pins = [NSMutableDictionary dictionary];
[SIOSocket socketWithHost: @"http://10.1.10.16:3000" response: ^(SIOSocket *socket)
[SIOSocket socketWithHost: @"http://<your ip address>:3000" response: ^(SIOSocket *socket)
{
self.socket = socket;

Expand Down
18 changes: 9 additions & 9 deletions world_pin_server/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var io = require('socket.io')(3000);
io.on('connection', function (socket) {

io.sockets.on('connection', function (socket) {
socket.broadcast.emit('join', socket['id']);
console.log(socket['id'] + ' has connected!');

socket.on('location', function (data) {
socket.broadcast.emit('update', (socket['id'] + ':' + data));
});
socket.on('disconnect', function () {
socket.broadcast.emit('disappear', socket['id']);
console.log(socket['id'] + ' has disconnected!');
});
console.log('Location data' + socket['id'] +' has this data ' + data);
socket.broadcast.emit('update', (socket['id'] + ':' + data));
});
socket.on('disconnect', function () {
socket.broadcast.emit('disappear', socket['id']);
console.log(socket['id'] + ' has disconnected!');
});
});