Skip to content

Commit d9ef1ff

Browse files
committed
Add iOS example app
1 parent ed805f5 commit d9ef1ff

File tree

22 files changed

+958
-17
lines changed

22 files changed

+958
-17
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,6 @@ fastlane/test_output
9090
iOSInjectionProject/
9191

9292
.swiftpm/
93+
94+
Pods/
95+
.DS_Store

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ let package = Package(
1414
name: "PodcastAPI",
1515
targets: ["PodcastAPI"]),
1616
.executable(
17-
name: "Example",
18-
targets: ["Example"]),
17+
name: "ExampleCommandLineApp",
18+
targets: ["ExampleCommandLineApp"]),
1919
],
2020
dependencies: [
2121
.package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"),
@@ -30,7 +30,7 @@ let package = Package(
3030
name: "PodcastAPITests",
3131
dependencies: ["PodcastAPI"]),
3232
.target(
33-
name: "Example",
33+
name: "ExampleCommandLineApp",
3434
dependencies: ["PodcastAPI"]),
3535
]
3636
)

README.md

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ If you have any questions, please contact [[email protected]](hello@listenno
1818
[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate PodcastAPI into your Xcode project using CocoaPods, specify it in your `Podfile`:
1919

2020
```ruby
21-
pod 'PodcastAPI', '~> 1.0.1'
22-
```
23-
24-
### Carthage
25-
26-
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate PodcastAPI into your Xcode project using Carthage, specify it in your `Cartfile`:
27-
28-
```ogdl
29-
github "ListenNotes/PodcastAPI" ~> 1.0.1
21+
pod 'PodcastAPI'
3022
```
3123

3224
### Swift Package Manager
@@ -62,7 +54,7 @@ let apiKey = ProcessInfo.processInfo.environment["LISTEN_API_KEY", default: ""]
6254
// the program would exit before requests return any response
6355
let client = PodcastAPI.Client(apiKey: apiKey, synchronousRequest: true)
6456

65-
// By default, we do asynchronous requests.
57+
// By default, we do asynchronous requests, for GUI-based applications on iOS or macOS
6658
// let client = PodcastAPI.Client(apiKey: apiKey)
6759

6860
// All parameters are passed via this Dictionary[String: String]
@@ -91,12 +83,60 @@ client.search(parameters: parameters) { response in
9183
print(response.getFreeQuota())
9284
print(response.getUsage())
9385
print(response.getNextBillingDate())
86+
87+
// If you use PodcastAPI library in a GUI app (e.g., iOS/macOS),
88+
// You need to update UI in the main thread
89+
// DispatchQueue.main.async {
90+
// self.displayLabel.text = "some text here"
91+
// }
9492
}
9593
}
9694
```
9795

9896
If `apiKey` is an empty string "", then we'll connect to a [mock server](https://www.listennotes.com/api/tutorials/#faq0) that returns fake data for testing purposes.
9997

98+
### synchronousRequest parameter
99+
100+
A command line executable will exit without wait for http async requests to finish. Therefore, we have to make http requests synchronous. Please set the `synchronousRequest` parameter to true when instantiating a Client object:
101+
102+
```swift
103+
let client = PodcastAPI.Client(apiKey: apiKey, synchronousRequest: true)
104+
```
105+
106+
In GUI Apps (e.g., iOS / macOS), you don't need to pass a `synchronousRequest` parameter. Just do this:
107+
108+
```swift
109+
let client = PodcastAPI.Client(apiKey: apiKey)
110+
```
111+
112+
### Update UI in the main thread
113+
114+
In the completion closure of an API request, you need to update UI in the main thread (`DispatchQueue.main.async`) for GUI apps (e.g., iOS, macOS):
115+
116+
```swift
117+
client.search(parameters: parameters) { response in
118+
if let error = response.error {
119+
switch (error) {
120+
case PodcastApiError.apiConnectionError:
121+
print("Can't connect to Listen API server")
122+
case PodcastApiError.authenticationError:
123+
print("wrong api key")
124+
default:
125+
print("unknown error")
126+
}
127+
} else {
128+
if let json = response.toJson() {
129+
print(json)
130+
}
131+
132+
// If you use PodcastAPI library in a GUI app (e.g., iOS/macOS),
133+
// You need to update UI in the main thread
134+
DispatchQueue.main.async {
135+
self.displayLabel.text = "some text here"
136+
}
137+
}
138+
}
139+
```
100140

101141
### Handling errors
102142

@@ -112,3 +152,22 @@ Unsuccessful requests return errors.
112152
| serverError | something wrong on our end (unexpected server errors) |
113153

114154
All errors can be found in [this file](https://github.com/ListenNotes/podcast-api-swift/blob/main/Sources/PodcastAPI/PodcastApiError.swift).
155+
156+
### Run example apps
157+
158+
We provide two example apps: one is a command line app, and the other is an iOS app.
159+
160+
#### Run the command line example app
161+
162+
```sh
163+
164+
# From the root directory of podcast-api-swift, where Package.swift is located:
165+
166+
$ swift run
167+
```
168+
169+
You can see the code under [Sources/ExampleCommandLineApp](https://github.com/ListenNotes/podcast-api-swift/tree/main/Sources/ExampleCommandLineApp).
170+
171+
#### Run the iOS example app
172+
173+
Just open ExampleIOSApp.xcworkspace with Xcode, from the directory of [Sources/ExampleIOSApp](https://github.com/ListenNotes/podcast-api-swift/tree/main/Sources/ExampleIOSApp).

Sources/.DS_Store

6 KB
Binary file not shown.

Sources/Example/main.swift renamed to Sources/ExampleCommandLineApp/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ client.search(parameters: parameters) { response in
4141
}
4242

4343
// Some account stats
44-
print(response.getFreeQuota())
45-
print(response.getUsage())
46-
print(response.getNextBillingDate())
44+
print("Your free quota this month: \(response.getFreeQuota()) requests")
45+
print("Your usage this month: \(response.getUsage()) requests")
46+
print("Your next billing date: \(response.getNextBillingDate())")
4747
}
4848
}
4949

0 commit comments

Comments
 (0)