-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update 240728_Live_Activity,_App_Extension,_Tuist,_Widget.md
- Loading branch information
Showing
3 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
.../AriNote/TIL.docc/SwiftUI/240728_Live_Activity,_App_Extension,_Tuist,_Widget.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
# 240728 Live Activity, App Extension, Tuist, Widget | ||
|
||
|
||
๊ธฐ์กด UIKit ํ๋ก์ ํธ์ Live Activity๋ฅผ ์ถ๊ฐํด๋ณด์! | ||
|
||
7์ 26์ผ (๊ธ) | ||
|
||
|
||
# ํ์ต๋ด์ฉ | ||
|
||
|
||
- Tuist์ App Extension ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ | ||
- Live Activity ์ด๊ธฐ ๊ตฌํ | ||
|
||
|
||
# ๊ณ ๋ฏผํ ์ / ํด๊ฒฐ๋ฐฉ๋ฒ | ||
|
||
## Tuist๋ก ๊ด๋ฆฌํ๋ ํ๋ก์ ํธ์ ์์ ฏ์ ์ถ๊ฐํ๊ธฐ ์ํ Extension ์ค์ | ||
|
||
1. ๊ธฐ์กด ํ๋ก์ ํธ ํ๊ฒ์ ์๋์ ๊ฐ์ด ์ถ๊ฐํด์ค๋ค. | ||
|
||
```swift | ||
let targets: [Target] = [ | ||
// ๊ธฐ์กด ํ๋ก์ ํธ ํ๊ฒ... | ||
.target( | ||
name: "LiveActivity", | ||
destinations: [.iPhone, .iPad], | ||
product: .appExtension, | ||
bundleId: "$(PRODUCT_BUNDLE_IDENTIFIER).WidgetExtension", | ||
deploymentTargets: .iOS("15.0"), | ||
infoPlist: .file(path: "LiveActivity/Info.plist"), | ||
sources: [ | ||
"LiveActivity/Sources/**" | ||
], | ||
resources: .resources(["LiveActivity/Resources/**"], | ||
dependencies: [], | ||
settings: .settings( | ||
configurations: [ | ||
.debug(name: .debug, xcconfig: .relativeToRoot("Project/Configurations/LiveActivity.xcconfig")), | ||
.release(name: .release, xcconfig: .relativeToRoot("Project/Configurations/LiveActivity.xcconfig")) | ||
], | ||
defaultSettings: .none | ||
) | ||
) | ||
] | ||
``` | ||
|
||
2. ํ๋ก์ ํธ Dependencies์ ์๋ก ๋ง๋ App Extension ํ๊ฒ์ ์ถ๊ฐํด์ค๋ค. | ||
|
||
```swift | ||
let dependencies: [TargetDependency] = [ | ||
// .... | ||
.target(name: "LiveActivity") | ||
] | ||
``` | ||
|
||
|
||
## Live Activity ์ด๊ธฐ ์ค์ ๊ตฌํํด๋ณด๊ธฐ | ||
|
||
Tuist์์๋ ํ๊ฒ์ ์ถ๊ฐํ๋ค๊ณ ํด์ ์ด๊ธฐ .swift ํ์ผ์ ์์ฑํด์ฃผ์ง ์๊ธฐ ๋๋ฌธ์, Xcode์์ ์ง์ ํ๊ฒ์ ์ถ๊ฐํด์ ํด๋น ํ์ผ์ ํ์ฉํ์๋ค. | ||
|
||
![](https://github.com/user-attachments/assets/74a8ccdd-7c4c-491f-8ccd-4936f870668f) | ||
|
||
![](https://github.com/user-attachments/assets/c8b95428-e917-42cd-8ad4-478ee06dff83) | ||
![](https://github.com/user-attachments/assets/d3bb67af-39d5-4aca-a581-e71fbc8bb67c) | ||
|
||
> Include Configuration App Intent: App Intent ํ๋ ์์ํฌ๋ ์ฑ์ ๋์๊ณผ ์ฝํ ์ธ ๋ฅผ Siri, Spotlight, Widget, Control ๋ฑ์ ํฌํจํ ์ฌ๋ฌ ํ๋ซํผ์ ์์คํ ํ๊ฒฝ๊ณผ ๊ธด๋ฐํ๊ฒ ํตํฉํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. Apple Intelligence์ ํฅ์๋ App Intent๋ฅผ ํตํด Siri๋ ์ฌ๋๋ค์ด ์ฑ์ ๊ธฐ๋ฅ์ ๋ฐ๊ฒฌํ ์ ์๋๋ก ์ฑ์ ๋์์ ์ ์ํ๊ณ ์ฑ ๋ด์์ ๊ทธ๋ฆฌ๊ณ ์ฑ ์ ๋ฐ์์ ๋์์ ์ํํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. | ||
|
||
|
||
๊ธฐ๋ณธ ๊ตฌํ์ ์์ ฏ๊น์ง ๊ตฌํ๋์ด์์ง๋ง, ๋๋ Live Activity๋ง์ ๊ตฌํํ ๊ฑฐ๋ผ ๊ตฌํ๋ ์์ ฏ ์ฝ๋๋ ๋ชจ๋ ์ ๊ฑฐํด์ฃผ๊ณ Live Activity๋ฅผ ๊ตฌํํ๊ธฐ ์ํด ์๋์ ๊ฐ์ด ํ์ํ ์ฝ๋๋ง ๋จ๊ฒจ๋์๋ค. | ||
|
||
```swift | ||
import ActivityKit | ||
import SwiftUI | ||
import WidgetKit | ||
|
||
// Target Membership for this file: UIKit App + Widget Exension | ||
@available(iOS 16.2, *) | ||
struct Attributes: ActivityAttributes { | ||
public struct ContentState: Codable, Hashable { | ||
// Dynamic stateful properties about your activity go here! | ||
var emoji: String | ||
} | ||
|
||
// Fixed non-changing properties about your activity go here! | ||
var name: String | ||
} | ||
``` | ||
|
||
์ฌ๊ธฐ์ Attributes๋ UIKit์ ์ฌ์ฉํ๊ณ ์๋ ์ฑ ํ๊ฒ์์ ์ฌ์ฉํด์ผํ๊ธฐ ๋๋ฌธ์ Target membership์ ๋๋ค ์ถ๊ฐํด์ฃผ์๋ค. | ||
์ฃผ๋ก Live Activity๋ฅผ ํ์ฑํ/๋นํ์ฑํ, ๊ทธ๋ฆฌ๊ณ ์ ๋ฐ์ดํธ ํด์ค ๋ ์ฌ์ฉ๋๋ ํ์ ์ด๋ค. | ||
|
||
```swift | ||
import SwiftUI | ||
import WidgetKit | ||
|
||
// Target Membership for this file: Widget Exension | ||
@available(iOS 16.2, *) | ||
@main | ||
struct Bundle: WidgetBundle { | ||
var body: some Widget { | ||
LiveStreamNotificationLiveActivity() | ||
} | ||
} | ||
|
||
@available(iOS 16.2, *) | ||
struct LiveActivity: Widget { | ||
var body: some WidgetConfiguration { | ||
ActivityConfiguration(for: LiveStreamNotificationAttributes.self) { context in | ||
// Lock screen/banner UI goes here | ||
VStack { | ||
Text("Hello \(context.state.emoji)") | ||
} | ||
.activityBackgroundTint(Color.cyan) | ||
.activitySystemActionForegroundColor(Color.black) | ||
|
||
} dynamicIsland: { context in | ||
DynamicIsland { | ||
// Expanded UI goes here. Compose the expanded UI through | ||
// various regions, like leading/trailing/center/bottom | ||
DynamicIslandExpandedRegion(.leading) { | ||
Text("Leading") | ||
} | ||
DynamicIslandExpandedRegion(.trailing) { | ||
Text("Trailing") | ||
} | ||
DynamicIslandExpandedRegion(.bottom) { | ||
Text("Bottom \(context.state.emoji)") | ||
// more content | ||
} | ||
} compactLeading: { | ||
Text("L") | ||
} compactTrailing: { | ||
Text("T \(context.state.emoji)") | ||
} minimal: { | ||
Text(context.state.emoji) | ||
} | ||
.widgetURL(URL(string: "http://www.apple.com")) | ||
.keylineTint(Color.red) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
์ ์ฝ๋๋ ์์ ฏ์ UI๋ฅผ ๊ทธ๋ ค์ฃผ๋ ์ฝ๋์ด๋ค. Xcode์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํด์ฃผ๋ ์ํ ์ฝ๋๋ค. | ||
|
||
๊ทธ๋ฆฌ๊ณ UIKit ์ฑ ๋ด์์๋ ์๋์ ๊ฐ์ด ์ฃผ๋ก ์ฌ์ฉํ๋ค. | ||
|
||
```swift | ||
// ๋ผ์ด๋ธ ์กํฐ๋นํฐ ํ์ฑํํ๊ธฐ | ||
let attributes = Attributes(name: "test") | ||
let contentState = Attributes.ContentState(emoji: "๐ท") | ||
|
||
do { | ||
let activity = try ActivityKit.Activity<Attributes>.request( | ||
attributes: attributes, | ||
content: .init(state: contentState, staleDate: nil), | ||
pushType: nil | ||
) | ||
print(activity) | ||
} catch { | ||
print("start Activity From App: \(error)") | ||
} | ||
``` | ||
|
||
```swift | ||
// ๋ผ์ด๋ธ ์กํฐ๋นํฐ ์ ๋ฐ์ดํธ | ||
Task { | ||
let updateContentState = Attributes.ContentState(emoji: state.emoji) | ||
for activity in ActivityKit.Activity<Attributes>.activities { | ||
await activity.update(.init(state: updateContentState, staleDate: nil)) | ||
} | ||
} | ||
``` | ||
|
||
```swift | ||
// ๋ผ์ด๋ธ ์กํฐ๋นํฐ ๋นํ์ฑํ | ||
Task { | ||
for activity in ActivityKit.Activity<Attributes>.activities { | ||
await activity.end(nil, dismissalPolicy: .immediate) | ||
} | ||
} | ||
``` | ||
|
||
# Trouble shooting | ||
|
||
|
||
## Live Acticity UI๊ฐ ๊ทธ๋ ค์ง์ง ์๋ ๋ฌธ์ | ||
|
||
### ๋ฌธ์ | ||
|
||
๊ธฐ์กด UIKit ์ฑ์ด ์ค์น๋์ด์๋ ์ํฉ์์ Live Activity ์ด๊ธฐ ์ค์ ์ ๊ตฌํํ๊ณ , ๋น๋๋ฅผ ๋๋ ธ๋๋ฐ, ์๋ฎฌ๋ ์ดํฐ์ ๋๋ฐ์ด์ค ๋ชจ๋ Live Activity๋ฅผ ํ์ฑํํ๊ณ , ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋ค์ด๋๋ฏน ์์ผ๋๋๋ฅผ ํด๋ฆญํ๋ฉด ์ฑ์ผ๋ก ์ง์ ์ ๋์ ํ์ฑํ๋ ๋ ๋ฏ ํ๋, UI๊ฐ ์ ํ ๋ณด์ฌ์ง์ง ์๋ ๋ฌธ์ ์๋ค. | ||
|
||
### ํด๊ฒฐ | ||
|
||
๋ช์๊ฐ ์ฝ์งํ๋๋ฐ ๋์ ํ ์ด์ ๋ฅผ ์ ์๊ฐ ์์๋ค. | ||
ํ์๋ถ๋ค์๊ฒ ๊ณต์ ๋๋ ธ๋๋ฐ, ๊ฐ์ ์ฝ์ง์ ํ๋ ๋ด์ฉ์ ์๊ฒ๋์๋ค. | ||
ํด๊ฒฐ๋ฐฉ๋ฒ์ ๋๋ฌด ๊ฐ๋จํ๋ค. | ||
|
||
1. ๊ธฐ์กด ์ฑ์ ์ ๊ฑฐํ๋ค. | ||
2. ๋น๋๋ฅผ ๋ค์ ๋๋ ค์ ์ฑ์ ์ฌ์ค์นํ๋ค. | ||
|
||
์ฑ ์ ๊ฑฐํด๋ ๋ํ๋์ง ์์ผ๋ฉด ์ฑ ์ ๊ฑฐํ ๋๋ฐ์ด์ค๋ฅผ ์ฌ์์ํ์ฌ ์ฑ์ ๋ค์ ์ค์นํ๋ฉด ๋๋ค๊ณ ํ๋ค. | ||
|
||
์ ๋ฐฉ๋ฒ์ผ๋ก ๋๋ฌด๋๋ ์์ฝ๊ฒ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์๋ค... Xcode ๋ด์์ ๋ญ๊ฐ ์ถฉ๋์ด ๋ฐ์ํ ๋ฌธ์ ์ธ๊ฑธ๊น...? | ||
|
||
|
||
--- | ||
|
||
|
||
# ์ฐธ๊ณ ๋งํฌ | ||
|
||
- [https://developer.apple.com/documentation/widgetkit/making-a-configurable-widget](https://developer.apple.com/documentation/widgetkit/making-a-configurable-widget) | ||
- [https://github.com/tuist/tuist/blob/main/fixtures/ios_app_with_extensions/Project.swift](https://github.com/tuist/tuist/blob/main/fixtures/ios_app_with_extensions/Project.swift) | ||
- [https://developer.apple.com/documentation/AppIntents](https://developer.apple.com/documentation/AppIntents) | ||
- [https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities](https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities) | ||
- [https://developer.apple.com/documentation/activitykit/starting-and-updating-live-activities-with-activitykit-push-notifications](https://developer.apple.com/documentation/activitykit/starting-and-updating-live-activities-with-activitykit-push-notifications) |