Skip to content

Commit

Permalink
Create 240813_WidgetKit,_App_Groups,_Keychain_Access_Group.md
Browse files Browse the repository at this point in the history
  • Loading branch information
leeari95 committed Aug 13, 2024
1 parent 0676fda commit 4631f33
Showing 1 changed file with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# 240813 WidgetKit, App Groups, Keychain Access Group

์•ฑ๊ณผ ์œ„์ ฏ๊ฐ„์— Keychain, UserDefaults๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž!


8์›” 13์ผ (ํ™”)


# ํ•™์Šต๋‚ด์šฉ

์œ„์ ฏ์—์„œ ์•ฑ์— ์ €์žฅ๋˜์–ด์žˆ๋Š” UserDefaults์™€ Keychain์— ์ ‘๊ทผํ–ˆ๋Š”๋ฐ, ์•„๋ฌด ๋ฐ์ดํ„ฐ๋„ ์กฐํšŒ๋˜์ง€ ์•Š์•„์„œ ์ด๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด์•˜๋‹ค.


## App Groups์ด๋ž€?

App Groups์€ ํ•˜๋‚˜ ์ด์ƒ์˜ ์•ฑ์ด๋‚˜ ์œ„์ ฏ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ์•ฑ๊ณผ ์•ฑ์— ์—ฐ๊ฒฐ๋œ ์œ„์ ฏ์ด ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•  ๋•Œ App Groups๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

[์ฃผ์š” ๊ฐœ๋…](https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW19)์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค:

* App Group ์„ค์ •
* ๊ทธ๋ฃน์„ ์„ค์ •ํ•˜๋ฉด ๊ฐ™์€ ๊ฐœ๋ฐœํŒ€์—์„œ ๋งŒ๋“  ์—ฌ๋Ÿฌ ์•ฑ์ด ์ €์žฅ ๊ณต๊ฐ„์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ณต๊ฐ„์€ ์บ์‹œ๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ™์€ ์ค‘์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณณ์ด๋‹ค.
* ๊ณต์œ  ๋ฉ”์ปค๋‹ˆ์ฆ˜
* App Group์— ์†ํ•œ ์•ฑ๋“ค์€ "Mach IPC and POSIX Semaphores and Shared Memory"์™€ ๊ฐ™์€ ํŠน์ • ๊ธฐ์ˆ ์„ ํ†ตํ•ด ์„œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ธฐ์ˆ ๋“ค์€ ์•ฑ๋“ค์ด ์•ˆ์ „ํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๋„๋ก ํ•ด์ค€๋‹ค.
* ์„ค์ • ๋ฐฉ๋ฒ•
* App Group์„ ์„ค์ •ํ•˜๋ ค๋ฉด ์„ค์ • ํŒŒ์ผ์—์„œ `com.apple.security.application-groups`๋ผ๋Š” ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ด ํ‚ค๋Š” ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๋ฐฐ์—ด ์•ˆ์—๋Š” ํ•œ๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์ด ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค.
* ๋ณดํ†ต ๋ฌธ์ž์—ด์€ ๊ฐœ๋ฐœํŒ€ ID์™€ ์ž„์˜๋กœ ์ •ํ•œ ์ด๋ฆ„์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

```xml
<key>com.apple.security.application-groups</key>
<array>
<string>DG29478A379Q6483R9214.MyAppGroup1</string>
<string>DG29478A379Q6483R9214.MyAppGroup2</string>
</array>
```

## UserDefaults๋ฅผ ์•ฑ๊ณผ ์œ„์ ฏ์ด ๊ฐ™์ด ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•

๋จผ์ € ์•ฑ๊ณผ ์œ„์ ฏ Target์— App Group์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

### 1. **Xcode์—์„œ** App Group ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

* Xcode์—์„œ ํ”„๋กœ์ ํŠธ ๋‚ด ์™ผ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์—์„œ 'ํ”„๋กœ์ ํŠธ ์ด๋ฆ„'์„ ํด๋ฆญํ•œ ํ›„ Targets ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ด๋‹นํ•˜๋Š” ์•ฑ์„ ์„ ํƒํ•œ๋‹ค.
* ์ƒ๋‹จ์˜ 'Signing & Capabilities' ํƒญ์„ ํด๋ฆญํ•œ๋‹ค.
* ํ™”๋ฉด ์™ผ์ชฝ์— '+ Capability' ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ณ  `App Groups`๋ฅผ ์„ ํƒํ•œ๋‹ค.
* App Groups๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด, 'Add a new App Group' ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ƒˆ๋กœ์šด ๊ทธ๋ฃน์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
* ์ƒˆ๋กœ์šด ๊ทธ๋ฃน์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์‹๋ณ„์ž๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ๋‚˜์ค‘์— ๋ฐ์ดํ„ฐ ๊ณต์œ ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

### 2. **entitlements** ํŒŒ์ผ์—์„œ App Group ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

* ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ `.entitlements` ํŒŒ์ผ์—์„œ `<key>`์™€ `<array>` ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด App Group์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

```xml
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.domain.appName</string>
</array>
```

#

์ด๋ ‡๊ฒŒ ์•ฑ ํƒ€๊ฒŸ๊ณผ ์œ„์ ฏ ํƒ€๊ฒŸ์— App Group์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๊ณต์œ  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด File, UserDefaults, Core Data ๋“ฑ์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

์ถ”๊ฐ€ํ•œ ์•ฑ ๊ทธ๋ฃน์„ ํ†ตํ•ด UserDefaults์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค:
```swift
let userDefaults = UserDefaults(suiteName: "group.com.domain.appName") // ์•ฑ ๊ทธ๋ฃน์˜ ์‹๋ณ„์ž
```

## Keychain์„ ์•ฑ๊ณผ ์œ„์ ฏ์ด ๊ฐ™์ด ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•

๊ธฐ์กด์— ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ™์€ credentialํ•œ ์ •๋ณด๋Š” keychain์„ ํ†ตํ•ด ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.
์ด๋Ÿฐ ์ •๋ณด๊ฐ€ ์œ„์ ฏ์—์„œ๋„ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค. ๋ฐฉ๋ฒ•์€ App Groups์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฝค๋‚˜ ์œ ์‚ฌํ•˜๋‹ค.

### 1. **Xcode์—์„œ** App Group ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

* Xcode์—์„œ ํ”„๋กœ์ ํŠธ ๋‚ด ์™ผ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์—์„œ 'ํ”„๋กœ์ ํŠธ ์ด๋ฆ„'์„ ํด๋ฆญํ•œ ํ›„ Targets ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ด๋‹นํ•˜๋Š” ์•ฑ์„ ์„ ํƒํ•œ๋‹ค.
* ์ƒ๋‹จ์˜ 'Signing & Capabilities' ํƒญ์„ ํด๋ฆญํ•œ๋‹ค.
* ํ™”๋ฉด ์™ผ์ชฝ์— '+ Capability' ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ณ  `Keychain Sharing`๋ฅผ ์„ ํƒํ•œ๋‹ค.
* ๊ฐ™์€ ํ‚ค์ฒด์ธ ์•„์ดํ…œ์„ ๊ณต์œ ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชจ๋“  ์•ฑ์— ๋™์ผํ•œ ํ‚ค์ฒด์ธ ์ ‘๊ทผ ๊ทธ๋ฃน์„ ์„ค์ •ํ•œ๋‹ค.
* ํ‚ค์ฒด์ธ์˜ ์‹๋ณ„์ž์˜ ๊ฒฝ์šฐ prefix๋กœ `AppIdentifierPrefix`๊ฐ€ ํ•„์ˆ˜๋กœ ํฌํ•จ๋˜์–ด์•ผ ํ•œ๋‹ค.
* ํฌํ•จํ•˜์ง€ ์•Š์•˜๋”๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด์„œ ๋นŒ๋“œ๊ฐ€ ์•ˆ๋์—ˆ๋‹ค.
* `Provisioning profile "iOS Team Provisioning Profile: <# Team ID #>" doesn't match the entitlements file's value for the keychain-access-groups entitlement.`
* ์•ฑ์˜ ํŒ€ ID์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋™์ผํ•œ ๊ฐœ๋ฐœ์ž ํŒ€์ด ๋งŒ๋“  ์•ฑ๋“ค๋งŒ ํ‚ค์ฒด์ธ ํ•ญ๋ชฉ์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

### 2. **entitlements** ํŒŒ์ผ์—์„œ App Group ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

* ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‚ค์ฒด์ธ ์ ‘๊ทผ ๊ทธ๋ฃน์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

```xml
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.yourcompany.sharedgroup</string>
</array>
```

#

์ดํ›„ ํ‚ค์ฒด์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผ ํ•  ๋•Œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฅผ ์„ค์ •ํ•ด์„œ ์ ‘๊ทผํ•˜๋ฉด ๋œ๋‹ค.

```swift
let accessGroup = "<# Your Team ID #>.com.example.SharedItems"
let query = [kSecClass: kSecClassGenericPassword,
kSecAttrService: service,
kSecAttrAccount: username,
kSecReturnAttributes: true,
kSecAttrAccessGroup: accessGroup,
kSecReturnData: true] as [String: Any]
var item: CFTypeRef?
let readStatus = SecItemCopyMatching(query as CFDictionary, &item)
```

---


# ์ฐธ๊ณ  ๋งํฌ

- [https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW19](https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW19)
- [https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1](https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1)
- [https://developer.apple.com/documentation/security/keychain_services/keychain_items/sharing_access_to_keychain_items_among_a_collection_of_apps/](https://developer.apple.com/documentation/security/keychain_services/keychain_items/sharing_access_to_keychain_items_among_a_collection_of_apps/)

0 comments on commit 4631f33

Please sign in to comment.