Skip to content

Commit 793adc3

Browse files
authored
Refactor v1 (#5)
* Initialization Strategy and Unregistering from Push (#2) * Starting on initial refactor * initial refactor * Implemented shared Knock instance * added device unregistration functionality * Added more safety around the userId checks * Cleanup * Make KnockError a LocalizedError * Updated KnockOptions init * updates * Matgar26 kno 5091 modules (#3) * Starting on initial refactor * initial refactor * Implemented shared Knock instance * added device unregistration functionality * Added more safety around the userId checks * Cleanup * Make KnockError a LocalizedError * Updated KnockOptions init * updates * Introduce Modules and Services for independent responsibilities * Authentication * fixes * set version on api base path * Fixes * fix * implementation fix * Updated logging * Working on tests * created shared Knock instance * refactored logging * refactored logging * Adding more tests * [KNO-5189]: Token management and doc updates (#4) * Documentation updates and refactor KnockEnvironment to be and actor to avoid race conditions * adding token handling * readme updates * Added migration guide * Small fixes * Added additional props to FeedItem * Cleanup * final cleanup * spelling fix * Cleanup based on PR feedback
1 parent b403ffe commit 793adc3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2553
-962
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/Knock.xcscheme

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES"
3030
shouldAutocreateTestPlan = "YES">
31+
<Testables>
32+
<TestableReference
33+
skipped = "NO">
34+
<BuildableReference
35+
BuildableIdentifier = "primary"
36+
BlueprintIdentifier = "KnockTests"
37+
BuildableName = "KnockTests"
38+
BlueprintName = "KnockTests"
39+
ReferencedContainer = "container:">
40+
</BuildableReference>
41+
</TestableReference>
42+
</Testables>
3143
</TestAction>
3244
<LaunchAction
3345
buildConfiguration = "Debug"

Knock.docc/Knock.md

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,103 @@
1-
# ``Knock``
1+
# Offical Knock iOS SDK
22

3-
<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@-->
3+
[![GitHub Release](https://img.shields.io/github/v/release/knocklabs/knock-swift?style=flat)](https://github.com/knocklabs/knock-swift/releases/latest)
4+
[![CocoaPods](https://img.shields.io/cocoapods/v/Knock.svg?style=flat)](https://cocoapods.org/)
5+
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
6+
[![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-4BC51D.svg?style=flat)](https://swift.org/package-manager/)
47

5-
## Overview
8+
![min swift version is 5.3](https://img.shields.io/badge/min%20Swift%20version-5.3-orange)
9+
![min ios version is 16](https://img.shields.io/badge/min%20iOS%20version-16-blue)
10+
[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/knocklabs/ios-example-app/blob/main/LICENSE)
611

7-
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
812

9-
## Topics
13+
~
1014

11-
### <!--@START_MENU_TOKEN@-->Group<!--@END_MENU_TOKEN@-->
15+
---
1216

13-
- <!--@START_MENU_TOKEN@-->``Symbol``<!--@END_MENU_TOKEN@-->
17+
Knock is a flexible, reliable notifications infrastructure that's built to scale with you. Use our iOS SDK to engage users with in-app feeds, setup push notifications, and manage notification preferences.
18+
19+
---
20+
21+
## Documentation
22+
23+
See the [documentation](https://docs.knock.app/notification-feeds/bring-your-own-ui) for usage examples.
24+
25+
## Installation
26+
27+
### Swift Package Manager
28+
29+
There are two ways to add this as a dependency using the Swift Package Manager:
30+
31+
1. Using Xcode
32+
2. Manually via `Package.swift`
33+
34+
#### Using Xcode
35+
36+
1. Open your Xcode project and select `File` -> `Add Packages...`
37+
38+
<img width="422" alt="Screenshot 2023-06-27 at 19 41 32" src="https://github.com/knocklabs/knock-swift/assets/952873/31bb67de-5272-445a-a5c4-5df3bcfa3c8b">
39+
40+
2. Search for `https://github.com/knocklabs/knock-swift.git` and then click `Add Package`
41+
*Note: We recommend that you set the Dependency Rule to Up to Next Major Version. While we encourage you to keep your app up to date with the latest SDK, major versions can include breaking changes or new features that require your attention.*
42+
43+
<img width="900" alt="Screenshot 2023-06-27 at 19 42 09" src="https://github.com/knocklabs/knock-swift/assets/952873/d947cc7f-8da6-4814-aa75-3e41ffe72ff4">
44+
45+
#### Manually via `Package.swift`
46+
47+
If you are managing dependencies using the `Package.swift` file, just add this to you dependencies array:
48+
49+
``` swift
50+
dependencies: [
51+
.package(url: "https://github.com/knocklabs/knock-swift.git", .upToNextMajor(from: "1.0.0"))
52+
]
53+
```
54+
55+
### Cocoapods
56+
57+
Add the dependency to your `Podfile`:
58+
59+
```
60+
platform :ios, '16.0'
61+
use_frameworks!
62+
63+
target 'MyApp' do
64+
pod 'Knock', '~> 0.2.0'
65+
end
66+
```
67+
68+
### Carthage
69+
70+
1. Add this line to your Cartfile:
71+
72+
```
73+
github "knocklabs/knock-swift" ~> 0.2.0
74+
```
75+
76+
### Manually
77+
78+
As a last option, you could manually copy the files inside the `Sources` folder to your project.
79+
80+
## Import and start using the SDK
81+
82+
You can now start using the SDK:
83+
84+
``` swift
85+
import Knock
86+
87+
// Setup the shared Knock instance as soon as you can.
88+
try? Knock.shared.setup(publishableKey: "your-pk", pushChannelId: "user-id")
89+
90+
// Once you know the Knock UserId, sign the user into the shared Knock instance.
91+
await Knock.shared.signIn(userId: "userid", userToken: nil)
92+
93+
```
94+
95+
## How to Contribute
96+
97+
Community contributions are welcome! If you'd like to contribute, please read our [contribution guide](CONTRIBUTING.md).
98+
99+
## License
100+
101+
This project is licensed under the MIT license.
102+
103+
See [LICENSE](LICENSE) for more information.

Knock.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = "Knock"
3-
spec.version = "0.2.1"
3+
spec.version = "1.0.0"
44
spec.summary = "An SDK to build in-app notifications experiences in Swift with Knock.."
55

66
spec.description = <<-DESC
@@ -16,6 +16,6 @@ Pod::Spec.new do |spec|
1616
spec.author = { "Knock" => "support@knock.app" }
1717
spec.source = { :git => "https://github.com/knocklabs/knock-swift.git", :tag => "#{spec.version}" }
1818
spec.ios.deployment_target = '16.0'
19-
spec.swift_version = '5.0'
19+
spec.swift_version = '5.3'
2020
spec.source_files = "Sources/**/*"
2121
end

MIGRATIONS.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Migration Guide
2+
3+
## Upgrading to Version 1.0.0
4+
5+
Version 1.0.0 of our Swift SDK introduces significant improvements and modernizations, including the adoption of Async/Await patterns for more concise and readable asynchronous code. While maintaining backward compatibility with completion handlers for all our APIs, we've also introduced several enhancements to optimize and streamline the SDK's usability.
6+
7+
### Key Enhancements:
8+
9+
- **Refined Initialization Process**: We've redesigned the initialization process for the Knock instance, dividing it into two distinct phases. This change offers greater flexibility in integrating our SDK into your projects.
10+
11+
#### Previous Initialization Approach:
12+
```swift
13+
let client = try! Knock(publishableKey: publishableKey, "your-pk": "user-id", hostname: "hostname")
14+
```
15+
16+
#### New in Version 1.0.0:
17+
```swift
18+
// Step 1: Early initialization. Ideal place: AppDelegate.
19+
try? Knock.shared.setup(publishableKey: "your-pk", pushChannelId: "apns-channel-id", options: nil)
20+
21+
// Step 2: Sign in the user. Ideal timing: as soon as you have the userId.
22+
await Knock.shared.signIn(userId: "userid", userToken: nil)
23+
```
24+
25+
- **KnockAppDelegate for Simplified Notification Management**: The introduction of `KnockAppDelegate` allows for effortless integration of push notification handling and token management, reducing boilerplate code and simplifying implementation.
26+
27+
- **Enhanced User Session Management**: New functionalities to sign users out and unregister device tokens have been added, providing more control over user sessions and device management.
28+
29+
- **Centralized Access with Shared Instance**: The SDK now utilizes a shared instance for the Knock client, facilitating easier access and interaction within your app's codebase.

Package.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ let package = Package(
2323
.target(
2424
name: "Knock",
2525
dependencies: ["SwiftPhoenixClient"],
26-
path: "Sources"
27-
),
26+
path: "Sources"),
27+
28+
.testTarget(
29+
name: "KnockTests",
30+
dependencies: ["Knock"]),
2831
]
2932
)

README.md

Lines changed: 53 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
# Swift SDK
2-
3-
## Features
4-
5-
* Preferences
6-
* getAllUserPreferences
7-
* getUserPreferences
8-
* setUserPreferences
9-
* Channels
10-
* registerTokenForAPNS
11-
* getUserChannelData
12-
* updateUserChannelData
13-
* Messages
14-
* getMessage
15-
* updateMessageStatus
16-
* deleteMessageStatus
17-
* batchUpdateStatuses
18-
* Users
19-
* getUser
20-
* updateUser
211

22-
## Installation
2+
# Official Knock iOS SDK
3+
4+
[![GitHub Release](https://img.shields.io/github/v/release/knocklabs/knock-swift?style=flat)](https://github.com/knocklabs/knock-swift/releases/latest)
5+
[![CocoaPods](https://img.shields.io/cocoapods/v/Knock.svg?style=flat)](https://cocoapods.org/)
6+
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
7+
[![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-4BC51D.svg?style=flat)](https://swift.org/package-manager/)
8+
9+
![min swift version is 5.3](https://img.shields.io/badge/min%20Swift%20version-5.3-orange)
10+
![min ios version is 16](https://img.shields.io/badge/min%20iOS%20version-16-blue)
11+
[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/knocklabs/ios-example-app/blob/main/LICENSE)
12+
13+
14+
15+
---
16+
17+
Knock is a flexible, reliable notifications infrastructure that's built to scale with you. Use our iOS SDK to engage users with in-app feeds, setup push notifications, and manage notification preferences.
18+
19+
---
2320

24-
You can include the SDK in a couple of ways:
21+
## Documentation
2522

26-
1. Swift Package Manager
27-
2. Carthage
28-
3. Cocoapods
29-
4. Manually
23+
See the [documentation](https://docs.knock.app/sdks/ios/overview) for full documentation.
24+
25+
## Migrations
26+
27+
See the [Migration Guide](https://github.com/knocklabs/knock-swift/blob/main/MIGRATIONS.md) if upgrading from a previous version.
28+
29+
## Example App
30+
31+
See the [iOS Example App](https://github.com/knocklabs/ios-example-app) for more examples.
32+
33+
## Installation
3034

3135
### Swift Package Manager
3236

@@ -42,59 +46,20 @@ There are two ways to add this as a dependency using the Swift Package Manager:
4246
<img width="422" alt="Screenshot 2023-06-27 at 19 41 32" src="https://github.com/knocklabs/knock-swift/assets/952873/31bb67de-5272-445a-a5c4-5df3bcfa3c8b">
4347

4448
2. Search for `https://github.com/knocklabs/knock-swift.git` and then click `Add Package`
49+
*Note: We recommend that you set the Dependency Rule to Up to Next Major Version. While we encourage you to keep your app up to date with the latest SDK, major versions can include breaking changes or new features that require your attention.*
4550

4651
<img width="900" alt="Screenshot 2023-06-27 at 19 42 09" src="https://github.com/knocklabs/knock-swift/assets/952873/d947cc7f-8da6-4814-aa75-3e41ffe72ff4">
4752

48-
3. Ensure that the Package is selected and click `Add Package`
49-
50-
<img width="900" alt="Screenshot 2023-06-27 at 19 42 23" src="https://github.com/knocklabs/knock-swift/assets/952873/c6053b06-73dc-43c8-8a68-40fbc2298f7c">
51-
52-
4. Wait for Xcode to fetch the dependencies and you should see the SDK on your Package Dependencies on the sidebar
53-
54-
<img width="505" alt="Screenshot 2023-06-27 at 19 42 45" src="https://github.com/knocklabs/knock-swift/assets/952873/9f314c9d-2525-4357-8da0-6ce4508b6db0">
55-
5653
#### Manually via `Package.swift`
5754

5855
If you are managing dependencies using the `Package.swift` file, just add this to you dependencies array:
5956

6057
``` swift
6158
dependencies: [
62-
.package(url: "https://github.com/knocklabs/knock-swift.git", .upToNextMajor(from: "0.2.1"))
59+
.package(url: "https://github.com/knocklabs/knock-swift.git", .upToNextMajor(from: "1.0.0"))
6360
]
6461
```
6562

66-
### Carthage
67-
68-
1. Add this line to your Cartfile:
69-
70-
```
71-
github "knocklabs/knock-swift" ~> 0.2.0
72-
```
73-
74-
2. Run `carthage update`. This will fetch dependencies into a Carthage/Checkouts folder, then build each one or download a pre-compiled framework.
75-
3. Open your application targets’ General settings tab. For Xcode 11.0 and higher, in the "Frameworks, Libraries, and Embedded Content" section, drag and drop each framework you want to use from the Carthage/Build folder on disk. Then, in the "Embed" section, select "Do Not Embed" from the pulldown menu for each item added. For Xcode 10.x and lower, in the "Linked Frameworks and Libraries" section, drag and drop each framework you want to use from the Carthage/Build folder on disk.
76-
4. On your application targets’ Build Phases settings tab, click the + icon and choose New Run Script Phase. Create a Run Script in which you specify your shell (ex: /bin/sh), add the following contents to the script area below the shell:
77-
```
78-
/usr/local/bin/carthage copy-frameworks
79-
```
80-
5. Create a file named `input.xcfilelist` and a file named output.xcfilelist
81-
6. Add the paths to the frameworks you want to use to your input.xcfilelist. For example:
82-
```
83-
$(SRCROOT)/Carthage/Build/iOS/Knock.framework
84-
```
85-
7. Add the paths to the copied frameworks to the `output.xcfilelist`. For example:
86-
```
87-
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Result.framework
88-
```
89-
8. Add the `input.xcfilelist` to the "Input File Lists" section of the Carthage run script phase
90-
9. Add the `output.xcfilelist` to the "Output File Lists" section of the Carthage run script phase
91-
92-
This script works around an [App Store submission bug](http://www.openradar.me/radar?id=6409498411401216) triggered by universal binaries and ensures that necessary bitcode-related files and dSYMs are copied when archiving.
93-
94-
With the debug information copied into the built products directory, Xcode will be able to symbolicate the stack trace whenever you stop at a breakpoint. This will also enable you to step through third-party code in the debugger.
95-
96-
When archiving your application for submission to the App Store or TestFlight, Xcode will also copy these files into the dSYMs subdirectory of your application’s .xcarchive bundle.
97-
9863
### Cocoapods
9964

10065
Add the dependency to your `Podfile`:
@@ -108,6 +73,14 @@ target 'MyApp' do
10873
end
10974
```
11075

76+
### Carthage
77+
78+
1. Add this line to your Cartfile:
79+
80+
```
81+
github "knocklabs/knock-swift" ~> 0.2.0
82+
```
83+
11184
### Manually
11285

11386
As a last option, you could manually copy the files inside the `Sources` folder to your project.
@@ -119,47 +92,24 @@ You can now start using the SDK:
11992
``` swift
12093
import Knock
12194

122-
knockClient = try! Knock(publishableKey: "your-pk", userId: "user-id")
95+
/*
96+
Setup the shared Knock instance as soon as you can.
97+
Note: pushChannelId is required if you want to use our KnockAppDelegate helper.
98+
Otherwise, this field is optional.
99+
*/
100+
try? Knock.shared.setup(publishableKey: "your-pk", pushChannelId: "apns-push-channel-id")
123101

124-
knockClient.getUser{ result in
125-
switch result {
126-
case .success(let user):
127-
print(user)
128-
case .failure(let error):
129-
print(error.localizedDescription)
130-
}
131-
}
132-
```
102+
// Once you know the Knock UserId, sign the user into the shared Knock instance.
103+
await Knock.shared.signIn(userId: "userid", userToken: nil)
133104

134-
## Using the SDK
135-
136-
The functions of the sdk are encapsulated and managed in a client object. You first have to instantiate a client with your public key and a user id. If you are running on production with enhanced security turned on (recommended) you have to also pass the signed user token to the client constructor.
137-
138-
``` swift
139-
import Knock
140-
141-
knockClient = try! Knock(publishableKey: "your-pk", userId: "user-id")
142-
143-
// on prod with enhanced security turned on:
144-
knockClient = try! Knock(publishableKey: "your-pk", userId: "user-id", userToken: "signed-user-token")
145105
```
146106

147-
## Notes for publishing
148-
149-
When releasing a new version of this SDK, please note:
150-
151-
* You should update the version in a couple of places:
152-
* in the file `Sources/KnockAPI.swift`: `clientVersion = "..."`
153-
* in the file `Knock.podspec`: `spec.version = "..."`
154-
* in this `README.md`, in the installation instructions for all the package managers
155-
* in git, add a tag, preferably to the commit that includes this previous changes
156-
157-
158-
159-
160-
161-
107+
## How to Contribute
162108

109+
Community contributions are welcome! If you'd like to contribute, please read our [contribution guide](CONTRIBUTING.md).
163110

111+
## License
164112

113+
This project is licensed under the MIT license.
165114

115+
See [LICENSE](LICENSE) for more information.

0 commit comments

Comments
 (0)