readme.md 2.01 KB
Newer Older
Aral Balkan's avatar
Aral Balkan committed
1 2 3 4
# Global Touch Notifications

A singleton that broadcasts touch events from the main window as notifications. See the demo app for a use case (to find out the location of a link that was clicked in a WKWebView).

5 6
**Platform:** iOS 9.1+

Aral Balkan's avatar
Aral Balkan committed
7 8 9 10
## Installation

[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)

Aral Balkan's avatar
Aral Balkan committed
11
### 1. Update your Cartfile
Aral Balkan's avatar
Aral Balkan committed
12

Aral Balkan's avatar
Aral Balkan committed
13
**Swift 3 (latest version)**
Aral Balkan's avatar
Aral Balkan committed
14

Aral Balkan's avatar
Aral Balkan committed
15 16 17 18 19
```
git "git@source.ind.ie:project/global-touch-notifications.git" ~> 6.0.0
```

For Swift 2.3, use the swift-2.3 branch:
Aral Balkan's avatar
Aral Balkan committed
20

Aral Balkan's avatar
Aral Balkan committed
21 22 23
```
git "git@source.ind.ie:project/global-touch-notifications.git" "swift-2.3"
```
Aral Balkan's avatar
Aral Balkan committed
24

Aral Balkan's avatar
Aral Balkan committed
25
To stay on Swift 2.2, use the 5.0.0 tag:
Aral Balkan's avatar
Aral Balkan committed
26 27

```
Aral Balkan's avatar
Aral Balkan committed
28
git "git@source.ind.ie:project/global-touch-notifications.git" ~> 5.0.0
Aral Balkan's avatar
Aral Balkan committed
29 30
```

Aral Balkan's avatar
Aral Balkan committed
31 32 33
### 2. Add the framework to your Xcode project

[Follow the instructions on Carthage’s readme](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application).
Aral Balkan's avatar
Aral Balkan committed
34

Aral Balkan's avatar
Aral Balkan committed
35
## Demo
Aral Balkan's avatar
Aral Balkan committed
36

Aral Balkan's avatar
Aral Balkan committed
37
1. Run the `./install` script to install the dependencies for the demo app.
Aral Balkan's avatar
Aral Balkan committed
38
2. Run the `./dev` script and play with the iOS demo app.
Aral Balkan's avatar
Aral Balkan committed
39 40 41 42 43 44 45 46 47 48

## Usage

To provide compile-time safety and literate code, we use a notification enum idiom to define our notification names:

```swift
import GlobalTouchNotifications
import Handle

// Initialise the singleton if necessary
Aral Balkan's avatar
Aral Balkan committed
49
_ = GlobalTouchNotifications.shared
Aral Balkan's avatar
Aral Balkan committed
50 51

// Register for global touch notification
Aral Balkan's avatar
Aral Balkan committed
52
handle(GlobalTouchNotification.named(.touchesBegan))
Aral Balkan's avatar
Aral Balkan committed
53
{
Aral Balkan's avatar
Aral Balkan committed
54
  /* with */ notification in
Aral Balkan's avatar
Aral Balkan committed
55

Aral Balkan's avatar
Aral Balkan committed
56 57 58 59
  guard
    let userInfo = notification.userInfo,
    let touches = userInfo["touches"] as? Set<UITouch>,
    let event = userInfo["event"] as? UIEvent
Aral Balkan's avatar
Aral Balkan committed
60 61 62 63 64 65
  else
  {
      fatalError("Could not unwrap userInfo dictionary for global touches.")
  }

  // Do something with touches and event here…
Aral Balkan's avatar
Aral Balkan committed
66
  print("First touch: \(touches.first)")
Aral Balkan's avatar
Aral Balkan committed
67 68 69 70 71 72 73 74 75
}
```

(Above example also uses the [Handle](https://source.ind.ie/project/handle) library for elegant notification handling.)

## Credits

Copyright © Aral Balkan.
Released with ♥ by [Ind.ie](https://ind.ie) under the MIT License.