Skip to content

Commit 339100e

Browse files
authored
Merge pull request #102 from pusher/macos
WIP: macOS
2 parents fe6563b + fccb5ee commit 339100e

File tree

13 files changed

+1303
-31
lines changed

13 files changed

+1303
-31
lines changed

PusherSwift.xcworkspace/contents.xcworkspacedata

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
[![Twitter](https://img.shields.io/badge/[email protected]?style=flat)](http://twitter.com/Pusher)
99
[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/pusher/pusher-websocket-swift/master/LICENSE.md)
1010

11+
Supports iOS, macOS (OS X) and tvOS! (Hopefully watchOS soon!)
12+
1113

1214
## I just want to copy and paste some code to get me started
1315

14-
What else would you want? Head over to the example app [ViewController.swift](https://github.com/pusher/pusher-websocket-swift/blob/master/iOS%20Example%20Swift/iOS%20Example%20Swift/ViewController.swift) to get some code you can drop in to get started. Or if you're using Objective-C, check out [ViewController.m](https://github.com/pusher/pusher-websocket-swift/blob/master/iOS%20Example%20Obj-C/iOS%20Example%20Obj-C/ViewController.m).
16+
What else would you want? Head over to one of our example apps:
17+
18+
* For iOS with Swift, see [ViewController.swift](https://github.com/pusher/pusher-websocket-swift/blob/master/iOS%20Example%20Swift/iOS%20Example%20Swift/ViewController.swift)
19+
* For iOS with Objective-C, see [ViewController.m](https://github.com/pusher/pusher-websocket-swift/blob/master/iOS%20Example%20Obj-C/iOS%20Example%20Obj-C/ViewController.m)
20+
* For macOS with Swift, see [ViewController.swift](https://github.com/pusher/pusher-websocket-swift/blob/master/macOS%20Example%20Swift/macOS%20Example%20Swift/ViewController.swift)
1521

1622

1723
## Table of Contents
@@ -748,11 +754,13 @@ You can unbind from events at both the global and per channel level. For both ob
748754
749755
## Push notifications
750756
751-
Pusher also supports push notifications. Instances of your application can register for push notifications and subscribe to "interests". Your server can then publish to those interests, which will be delivered to your application as push notifications. See [our guide to setting up push notifications for iOS](https://pusher.com/docs/push_notifications/ios) for a friendly introduction.
757+
Pusher also supports push notifications. Instances of your application can register for push notifications and subscribe to "interests". Your server can then publish to those interests, which will be delivered to your application as push notifications. See [our guide to setting up APNs push notifications](https://pusher.com/docs/push_notifications/ios) for a friendly introduction.
752758
753-
You should set up your app for push notifications in your `AppDelegate`. Start off your app in the usual way:
759+
### Initializing the Pusher object
754760
755-
#### Swift
761+
You should set up your app for push notifications in your `AppDelegate`. The setup varies slightly depending on whether you're using Swift or Objective-C, and whether you're using iOS or macOS (OS X):
762+
763+
#### Swift on iOS
756764
```swift
757765
import PusherSwift
758766
import UserNotifications
@@ -763,7 +771,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
763771
...
764772
```
765773

766-
#### Objective-C
774+
#### Objective-C on iOS
767775
```objc
768776
#import "AppDelegate.h"
769777
@import UserNotifications;
@@ -776,9 +784,23 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
776784
...
777785
```
778786
787+
#### Swift on macOS
788+
789+
```swift
790+
import Cocoa
791+
import PusherSwift
792+
793+
@NSApplicationMain
794+
class AppDelegate: NSObject, NSApplicationDelegate, PusherDelegate {
795+
let pusher = Pusher(key: "YOUR_APP_KEY")
796+
// ...
797+
```
798+
799+
### Registering with APNs
800+
779801
For your app to receive push notifications, it must first register with APNs. You should do this when the application finishes launching. Your app should register for all types of notification, like so:
780802

781-
#### Swift
803+
#### Swift on iOS
782804
```swift
783805
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
784806
let center = UNUserNotificationCenter.current()
@@ -791,7 +813,7 @@ func application(_ application: UIApplication, didFinishLaunchingWithOptions lau
791813
}
792814
```
793815

794-
#### Objective-C
816+
#### Objective-C on iOS
795817
```objc
796818
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
797819
self.pusher = [[Pusher alloc] initWithKey:@"YOUR_APP_KEY"];
@@ -806,19 +828,29 @@ func application(_ application: UIApplication, didFinishLaunchingWithOptions lau
806828
}
807829
```
808830

831+
#### Swift on macOS
832+
833+
```swift
834+
func applicationDidFinishLaunching(_ aNotification: Notification) {
835+
NSApp.registerForRemoteNotifications(matching: [NSRemoteNotificationType.alert, NSRemoteNotificationType.sound, NSRemoteNotificationType.badge])
836+
}
837+
```
838+
839+
### Receiving your APNs device token and registering with Pusher
840+
809841
Next, APNs will respond with a device token identifying your app instance. Your app should then register with Pusher, passing along its device token.
810842

811843
Your app can now subscribe to interests. The following registers and subscribes the app to the interest "donuts":
812844

813-
#### Swift
845+
#### Swift on iOS
814846
```swift
815847
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
816848
pusher.nativePusher.register(deviceToken: deviceToken)
817849
pusher.nativePusher.subscribe(interestName: "donuts")
818850
}
819851
```
820852

821-
#### Objective-C
853+
#### Objective-C on iOS
822854
```objc
823855
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
824856
NSLog(@"Registered for remote notifications; received device token");
@@ -827,23 +859,43 @@ func application(_ application: UIApplication, didRegisterForRemoteNotifications
827859
}
828860
```
829861

862+
#### Swift on macOS
863+
```swift
864+
func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
865+
self.pusher.nativePusher.register(deviceToken: deviceToken)
866+
self.pusher.nativePusher.subscribe(interestName: "donuts")
867+
}
868+
```
869+
870+
871+
### Receiving push notifications
830872

831873
When your server publishes a notification to the interest "donuts", it will get passed to your app. This happens as a call in your `AppDelegate` which you should listen to:
832874

833-
#### Swift
875+
#### Swift on iOS
834876
```swift
835877
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
836878
print(userInfo)
837879
}
838880
```
839881

840-
#### Objective-C
882+
#### Objective-C on iOS
841883
```objc
842884
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
843885
NSLog(@"Received remote notification: %@", userInfo);
844886
}
845887
```
846888

889+
#### Swift on macOS
890+
```swift
891+
func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String: Any]) {
892+
print("Received remote notification: \(userInfo.debugDescription)" )
893+
}
894+
```
895+
896+
897+
### Unsubscribing from interests
898+
847899
If at a later point you wish to unsubscribe from an interest, this works in the same way:
848900

849901
#### Swift

Source/NativePusher.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//
88

9-
#if os(iOS)
9+
#if os(iOS) || os(OSX)
1010

1111
/**
1212
An interface to Pusher's native push notification service.
@@ -164,9 +164,9 @@
164164

165165
/**
166166
Adds subscribe / unsubscribe tasts to task queue
167-
167+
168168
- parameter interestName: the name of the interest you want to interact with
169-
- parameter change: specifies whether the change is to subscribe or
169+
- parameter change: specifies whether the change is to subscribe or
170170
unsubscribe
171171

172172
*/

Source/PusherSwift.swift

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,22 @@ let CLIENT_NAME = "pusher-websocket-swift"
1616
open weak var delegate: PusherDelegate? = nil {
1717
willSet {
1818
self.connection.delegate = newValue
19-
#if os(iOS)
19+
#if os(iOS) || os(OSX)
2020
self.nativePusher.delegate = newValue
2121
#endif
2222
}
2323
}
2424
private let key: String
2525

26-
#if os(iOS)
26+
#if os(iOS) || os(OSX)
2727
public let nativePusher: NativePusher
28-
#endif
29-
30-
#if os(iOS)
3128

3229
/**
3330
Initializes the Pusher client with an app key and any appropriate options.
3431

3532
- parameter key: The Pusher app key
3633
- parameter options: An optional collection of options
37-
- parameter nativePusher: A NativePusher instance for the app that the provided
34+
- parameter nativePusher: A NativePusher instance for the app that the provided
3835
key belongs to
3936

4037
- returns: A new Pusher client instance
@@ -48,29 +45,27 @@ let CLIENT_NAME = "pusher-websocket-swift"
4845
self.nativePusher = nativePusher
4946
nativePusher.setPusherAppKey(pusherAppKey: key)
5047
}
51-
5248
#endif
5349

54-
#if os(OSX) || os(tvOS)
55-
50+
#if os(tvOS)
5651
/**
57-
Initializes the Pusher client with an app key and any appropriate options.
52+
Initializes the Pusher client with an app key and any appropriate options.
5853

59-
- parameter key: The Pusher app key
60-
- parameter options: An optional collection of options
54+
- parameter key: The Pusher app key
55+
- parameter options: An optional collection of options
6156

62-
- returns: A new Pusher client instance
63-
*/
57+
- returns: A new Pusher client instance
58+
*/
6459
public init(key: String, options: PusherClientOptions = PusherClientOptions()) {
6560
self.key = key
6661
let urlString = constructUrl(key: key, options: options)
6762
let ws = WebSocket(url: URL(string: urlString)!)
6863
connection = PusherConnection(key: key, socket: ws, url: urlString, options: options)
6964
connection.createGlobalChannel()
7065
}
71-
7266
#endif
7367

68+
7469
/**
7570
Subscribes the client to a new channel
7671

Tests/NativePusherTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//
88

9-
#if os(iOS)
9+
#if os(iOS) || os(OSX)
1010

1111
@testable import PusherSwift
1212
import XCTest

iOS Example Obj-C/iOS Example Obj-C.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
331F37341D82CAB900AE2852 /* Main.storyboard */,
8686
331F37371D82CAB900AE2852 /* Assets.xcassets */,
8787
331F37391D82CAB900AE2852 /* LaunchScreen.storyboard */,
88-
331F373C1D82CAB900AE2852 /* Info.plist */,
8988
331F372B1D82CAB900AE2852 /* Supporting Files */,
9089
);
9190
path = "iOS Example Obj-C";
@@ -94,6 +93,7 @@
9493
331F372B1D82CAB900AE2852 /* Supporting Files */ = {
9594
isa = PBXGroup;
9695
children = (
96+
331F373C1D82CAB900AE2852 /* Info.plist */,
9797
331F372C1D82CAB900AE2852 /* main.m */,
9898
3330F8821D86A58900D8DC88 /* iOS Example Obj-C.entitlements */,
9999
);

0 commit comments

Comments
 (0)