Skip to content

Commit 51973d4

Browse files
author
Adolfo Martinelli
committed
Swift 3.0 Support (aciidgh#8)
* Swift 3.0 Migration * Update README documentation * Refactor and update public interfaces * Bump major version and update podspec
1 parent 9ca83d4 commit 51973d4

27 files changed

+529
-566
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: objective-c
2-
osx_image: xcode7.3
2+
osx_image: xcode8
33
before_install:
44
- brew update
55
- brew install mosquitto
66
script:
77
- /usr/local/opt/mosquitto/sbin/mosquitto -d
8-
- xcodebuild -project SwiftMQTT/SwiftMQTT.xcodeproj -scheme SwiftMQTT -sdk iphonesimulator test
8+
- xcodebuild -project SwiftMQTT/SwiftMQTT.xcodeproj -scheme SwiftMQTT -sdk iphonesimulator -destination "name=iPhone 6s" test

README.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
# SwiftMQTT
2-
MQTT Client in pure swift ❤️
32

4-
Master:
3+
MQTT Client in pure Swift ❤️️
4+
55
[![Build Status](https://travis-ci.org/aciidb0mb3r/SwiftMQTT.svg)](https://travis-ci.org/aciidb0mb3r/SwiftMQTT)
6+
[![Version](https://img.shields.io/cocoapods/v/SwiftMQTT.svg?style=flat)](http://cocoapods.org/pods/SwiftMQTT)
7+
[![License](https://img.shields.io/cocoapods/l/SwiftMQTT.svg?style=flat)](http://cocoapods.org/pods/SwiftMQTT)
68

7-
# Info
8-
* Fully written in swift from ground up
9-
* Closures everywhere :D
9+
## Info
10+
* Fully written in Swift from ground up
11+
* Closures everywhere 😃
1012
* Includes test cases and sample project
1113
* Based on MQTT Version 3.1.1 (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718043)
1214

1315
![Sample Project Screenshot](http://i.imgur.com/9lefVmVl.png)
1416

15-
# How to use
17+
## How to use
1618

17-
## Create MQTTSession object:
19+
### Create Session
1820
```swift
1921
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
2022
```
2123

22-
## Connect
24+
### Connect
2325
```swift
2426
mqttSession.connect { (succeeded, error) -> Void in
2527
if succeeded {
@@ -28,49 +30,49 @@ mqttSession.connect { (succeeded, error) -> Void in
2830
}
2931
```
3032

31-
## Subscribe
33+
### Subscribe
3234
```swift
33-
mqttSession.subscribe("/hey/cool", qos: MQTTQoS.AtLeastOnce) { (succeeded, error) -> Void in
35+
mqttSession.subscribe(to: "/hey/cool", delivering: .atLeastOnce) { (succeeded, error) -> Void in
3436
if succeeded {
3537
print("Subscribed!")
3638
}
3739
}
3840
```
3941

40-
## Unsubscribe
42+
### Unsubscribe
4143
```swift
42-
mqttSession.unSubscribe(["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
44+
mqttSession.unSubscribe(from: ["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
4345
if succeeded {
4446
print("unSubscribed!")
4547
}
4648
}
4749
```
48-
## Publish
50+
51+
### Publish
4952
```swift
5053
let jsonDict = ["hey" : "sup"]
51-
let data = try! NSJSONSerialization.dataWithJSONObject(jsonDict, options: NSJSONWritingOptions.PrettyPrinted)
54+
let data = try! JSONSerialization.data(withJSONObject: jsonDict, options: .prettyPrinted)
5255

53-
mqttSession.publishData(data, onTopic: "/hey/wassap", withQoS: MQTTQoS.AtLeastOnce, shouldRetain: false) { (succeeded, error) -> Void in
56+
mqttSession.publish(data, in: "/hey/wassap", delivering: .atLeastOnce, retain: false) { (succeeded, error) -> Void in
5457
if succeeded {
5558
print("Published!")
5659
}
5760
}
58-
```
5961

60-
## Conform to `MQTTSessionDelegate` to receive messages
62+
```
63+
### Conform to `MQTTSessionDelegate` to receive messages
6164
```swift
6265
mqttSession.delegate = self
6366
```
6467
```swift
65-
func mqttSession(session: MQTTSession, didReceiveMessage message: NSData, onTopic topic: String) {
66-
let stringData = NSString(data: message, encoding: NSUTF8StringEncoding) as! String
67-
print("data received on topic \(topic) message \(stringData)")
68+
func mqttSession(session: MQTTSession, received message: Data, in topic: String) {
69+
let string = String(data: message, encoding: .utf8)!
6870
}
6971
```
7072

71-
# Installation
73+
## Installation
7274

73-
## CocoaPods
75+
### CocoaPods
7476

7577
Install using [CocoaPods](http://cocoapods.org) by adding the following lines to your Podfile:
7678

@@ -79,5 +81,5 @@ use_frameworks!
7981
pod 'SwiftMQTT'
8082
````
8183

82-
# License
84+
## License
8385
MIT

SwiftMQTT.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Pod::Spec.new do |s|
33

44
s.name = "SwiftMQTT"
5-
s.version = "1.0.2"
6-
s.summary = "MQTT Client in pure swift"
5+
s.version = "2.0.0"
6+
s.summary = "MQTT Client in pure Swift"
77
s.description = <<-DESC
8-
MQTT Client in pure swift
8+
MQTT Client in Swift 3.0 based on MQTT Version 3.1.1
99
DESC
1010

1111
s.homepage = "https://github.com/aciidb0mb3r/SwiftMQTT"

SwiftMQTT/SwiftMQTT.xcodeproj/project.pbxproj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
5471A8971BF228900079610F /* Products */,
114114
);
115115
sourceTree = "<group>";
116+
usesTabs = 0;
116117
};
117118
5471A8971BF228900079610F /* Products */ = {
118119
isa = PBXGroup;
@@ -202,11 +203,12 @@
202203
isa = PBXProject;
203204
attributes = {
204205
LastSwiftUpdateCheck = 0710;
205-
LastUpgradeCheck = 0710;
206+
LastUpgradeCheck = 0800;
206207
ORGANIZATIONNAME = Ankit;
207208
TargetAttributes = {
208209
5471A8951BF228900079610F = {
209210
CreatedOnToolsVersion = 7.1;
211+
LastSwiftMigration = 0800;
210212
};
211213
5471A89F1BF228900079610F = {
212214
CreatedOnToolsVersion = 7.1;
@@ -305,11 +307,12 @@
305307
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
306308
CLANG_WARN_EMPTY_BODY = YES;
307309
CLANG_WARN_ENUM_CONVERSION = YES;
310+
CLANG_WARN_INFINITE_RECURSION = YES;
308311
CLANG_WARN_INT_CONVERSION = YES;
309312
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
313+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
310314
CLANG_WARN_UNREACHABLE_CODE = YES;
311315
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
312-
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
313316
COPY_PHASE_STRIP = NO;
314317
CURRENT_PROJECT_VERSION = 1;
315318
DEBUG_INFORMATION_FORMAT = dwarf;
@@ -353,11 +356,12 @@
353356
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
354357
CLANG_WARN_EMPTY_BODY = YES;
355358
CLANG_WARN_ENUM_CONVERSION = YES;
359+
CLANG_WARN_INFINITE_RECURSION = YES;
356360
CLANG_WARN_INT_CONVERSION = YES;
357361
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
362+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
358363
CLANG_WARN_UNREACHABLE_CODE = YES;
359364
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
360-
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
361365
COPY_PHASE_STRIP = NO;
362366
CURRENT_PROJECT_VERSION = 1;
363367
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
@@ -374,6 +378,7 @@
374378
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
375379
MTL_ENABLE_DEBUG_INFO = NO;
376380
SDKROOT = iphoneos;
381+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
377382
TARGETED_DEVICE_FAMILY = "1,2";
378383
VALIDATE_PRODUCT = YES;
379384
VERSIONING_SYSTEM = "apple-generic";
@@ -397,6 +402,7 @@
397402
PRODUCT_NAME = "$(TARGET_NAME)";
398403
SKIP_INSTALL = YES;
399404
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
405+
SWIFT_VERSION = 3.0;
400406
};
401407
name = Debug;
402408
};
@@ -415,6 +421,7 @@
415421
PRODUCT_BUNDLE_IDENTIFIER = im.ankit.SwiftMQTT;
416422
PRODUCT_NAME = "$(TARGET_NAME)";
417423
SKIP_INSTALL = YES;
424+
SWIFT_VERSION = 3.0;
418425
};
419426
name = Release;
420427
};
@@ -425,6 +432,7 @@
425432
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
426433
PRODUCT_BUNDLE_IDENTIFIER = im.ankit.SwiftMQTTTests;
427434
PRODUCT_NAME = "$(TARGET_NAME)";
435+
SWIFT_VERSION = 3.0;
428436
};
429437
name = Debug;
430438
};
@@ -435,6 +443,7 @@
435443
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
436444
PRODUCT_BUNDLE_IDENTIFIER = im.ankit.SwiftMQTTTests;
437445
PRODUCT_NAME = "$(TARGET_NAME)";
446+
SWIFT_VERSION = 3.0;
438447
};
439448
name = Release;
440449
};

SwiftMQTT/SwiftMQTT.xcodeproj/xcshareddata/xcschemes/SwiftMQTT.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0710"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

SwiftMQTT/SwiftMQTT/MQTTExtensions.swift

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,78 @@
88

99
import Foundation
1010

11-
enum MQTTSessionError: ErrorType {
12-
case None
13-
case SocketError
11+
enum MQTTSessionError: Error {
12+
case none
13+
case socketError
1414
}
1515

1616
enum MQTTPacketType: UInt8 {
17-
case Connect = 0x01
18-
case Connack = 0x02
19-
case Publish = 0x03
20-
case PubAck = 0x04
21-
case PubRec = 0x05
22-
case PubRel = 0x06
23-
case PubComp = 0x07
24-
case Subscribe = 0x08
25-
case SubAck = 0x09
26-
case UnSubscribe = 0x0A
27-
case UnSubAck = 0x0B
28-
case PingReq = 0x0C
29-
case PingResp = 0x0D
30-
case Disconnect = 0x0E
31-
17+
case connect = 0x01
18+
case connAck = 0x02
19+
case publish = 0x03
20+
case pubAck = 0x04
21+
case pubRec = 0x05
22+
case pubRel = 0x06
23+
case pubComp = 0x07
24+
case subscribe = 0x08
25+
case subAck = 0x09
26+
case unSubscribe = 0x0A
27+
case unSubAck = 0x0B
28+
case pingReq = 0x0C
29+
case pingResp = 0x0D
30+
case disconnect = 0x0E
3231
}
3332

3433
public enum MQTTQoS: UInt8 {
35-
case AtMostOnce = 0x0
36-
case AtLeastOnce = 0x01
37-
case ExactlyOnce = 0x02
34+
case atMostOnce = 0x0
35+
case atLeastOnce = 0x01
36+
case exactlyOnce = 0x02
3837
}
3938

40-
enum MQTTConnackResponse: UInt8, ErrorType {
41-
case ConnectionAccepted = 0x00
42-
case BadProtocol = 0x01
43-
case ClientIDRejected = 0x02
44-
case ServerUnavailable = 0x03
45-
case BadUsernameOrPassword = 0x04
46-
case NotAuthorized = 0x05
39+
enum MQTTConnAckResponse: UInt8, Error {
40+
case connectionAccepted = 0x00
41+
case badProtocol = 0x01
42+
case clientIDRejected = 0x02
43+
case serverUnavailable = 0x03
44+
case badUsernameOrPassword = 0x04
45+
case notAuthorized = 0x05
4746
}
4847

49-
extension NSMutableData {
48+
extension Data {
5049

51-
func mqtt_encodeRemainingLength(length: Int) {
50+
mutating func mqtt_encodeRemaining(length: Int) {
5251
var lengthOfRemainingData = length
5352
repeat {
54-
var digit = UInt8(lengthOfRemainingData % 128);
55-
lengthOfRemainingData /= 128;
56-
if (lengthOfRemainingData > 0) {
57-
digit |= 0x80;
53+
var digit = UInt8(lengthOfRemainingData % 128)
54+
lengthOfRemainingData /= 128
55+
if lengthOfRemainingData > 0 {
56+
digit |= 0x80
5857
}
59-
self.appendBytes(&digit, length: 1)
60-
} while (lengthOfRemainingData > 0);
58+
append(&digit, count: 1)
59+
} while lengthOfRemainingData > 0
6160
}
6261

63-
func mqtt_appendUInt8(data: UInt8) {
62+
mutating func mqtt_append(_ data: UInt8) {
6463
var varData = data
65-
self.appendBytes(&varData, length: 1)
64+
append(&varData, count: 1)
6665
}
6766

68-
///Appends two bytes
69-
///Big Endian
70-
func mqtt_appendUInt16(data: UInt16) {
67+
// Appends two bytes
68+
// Big Endian
69+
mutating func mqtt_append(_ data: UInt16) {
7170
let byteOne = UInt8(data / 256)
7271
let byteTwo = UInt8(data % 256)
73-
self.mqtt_appendUInt8(byteOne)
74-
self.mqtt_appendUInt8(byteTwo)
72+
mqtt_append(byteOne)
73+
mqtt_append(byteTwo)
7574
}
7675

77-
func mqtt_appendData(data: NSData) {
78-
self.mqtt_appendUInt16(UInt16(data.length))
79-
self.appendData(data)
76+
mutating func mqtt_append(_ data: Data) {
77+
mqtt_append(UInt16(data.count))
78+
append(data)
8079
}
8180

82-
func mqtt_appendString(string: String) {
83-
self.mqtt_appendUInt16(UInt16(string.characters.count))
84-
self.appendData(string.dataUsingEncoding(NSUTF8StringEncoding)!)
81+
mutating func mqtt_append(_ string: String) {
82+
mqtt_append(UInt16(string.characters.count))
83+
append(string.data(using: .utf8)!)
8584
}
8685
}

0 commit comments

Comments
 (0)