Focus On Develop ๐ŸคŸ๐ŸคŸ

[Swift] Bento #1. iPhone ์—ฐ๋ฝ์ฒ˜ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ณธ๋ฌธ

iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!

[Swift] Bento #1. iPhone ์—ฐ๋ฝ์ฒ˜ ๊ฐ€์ ธ์˜ค๊ธฐ

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2021. 4. 27. 11:18

ใ…Žใ…Ž ๋˜ ์˜ค๋žœ๋งŒ์— ๊ธ€์„ ์จ๋ด…๋‹ˆ๋‹ค~~

์•ž์œผ๋กœ ์ข…์ข… ์—ฐ๋ฝ์ฒ˜ ๊ฐ€์ ธ์˜ค๊ธฐ, ์ด๋ฉ”์ผ๋ณด๋‚ด๊ธฐ, ์ „ํ™”๊ฑธ๊ธฐ, ์•ฑ์‹คํ–‰ํ•˜๊ธฐ, ๋กœ์ปฌNoti, ๋ณด์•ˆ ๋“ฑ๋“ฑ ์•„์ดํฐ์˜ OS๊ธฐ๋Šฅ(?)์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋“ค์— ๋Œ€ํ•ด์„œ ๋ช‡๊ฐ€์ง€๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ด์š”!!

์ด ํ•ญ๋ชฉ๋“ค์„ ๊ฐ„๋‹จํžˆ Bento ์‹œ๋ฆฌ์ฆˆ! ๋ผ๊ณ  ํ•˜๊ณ  ์‹œ์ž‘ํ•ด๋ณผ๊ฒŒ์š” ๐Ÿ‘‰

 

์ฒซ๋ฒˆ์งธ ํฌ์ŠคํŒ…์€ ์—ฐ๋ฝ์ฒ˜ ๊ฐ€์ ธ์˜ค๊ธฐ์ž…๋‹ˆ๋‹ค!


๋จผ์ € ํœด๋Œ€ํฐ์˜ ์—ฐ๋ฝ์ฒ˜์— ์ ‘๊ทผํ•˜๋ ค๋ฉด, ์‚ฌ์šฉ์ž๊ฐ€ ๊ถŒํ•œ์„ ํ—ˆ๋ฝํ•ด์•ผ ๋˜์š”!!

์•ฑ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜, ์นด๋ฉ”๋ผ๋กœ ์ฐ์œผ๋ ค๊ณ  ํ•˜๊ฑฐ๋‚˜ ๋“ฑ๋“ฑ ํ• ๋•Œ ์ด๋Ÿฐ๊ฑฐ ๋งŽ์ด ๋ณด์…จ์ฃ ~~?

์ด ๊ถŒํ•œ์„ ํš๋“ํ•˜๊ธฐ ์œ„ํ•ด, ํ”„๋กœ์ ํŠธ ์„ค์ •์„ ํ•ด์ค๋‹ˆ๋‹ค.

Info.plist์— ์ด ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!

 

์ €๋Š” ์ด๋Ÿฐ ํŠน์ • ๋ชฉ์ ์„ ๊ฐ€์ง€๋Š” ๊ธฐ๋Šฅ๋“ค์„ ํ”„๋กœํ† ์ฝœ๋กœ ๋”ฐ๋กœ ์ •์˜๋ฅผ ํ•ด๋‘ฌ์š”.

์ด๋ฒˆ์—๋Š” ์—ฐ๋ฝ์ฒ˜ ๊ด€๋ จ๋œ ๋‚ด์šฉ์ด๋‹ˆ๊นŒ ContactUsable์ด๋ผ๊ณ  ํ”„๋กœํ† ์ฝœ์„ ์ •์˜ํ•ด์„œ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค!

 

๋จผ์ € ์ „์ฒด์†Œ์Šค์—์š”.

import Contacts

protocol ContactUsable {}
extension ContactUsable {
    
    func getContacts() {
        let store = CNContactStore()
        var contacts: [Contact] = []
        
        // ์—ฐ๋ฝ์ฒ˜์— ์š”์ฒญํ•  ํ•ญ๋ชฉ
        let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey] as [CNKeyDescriptor]
        // Request ์ƒ์„ฑ
        let request = CNContactFetchRequest(keysToFetch: keys)
        request.sortOrder = CNContactSortOrder.userDefault
        
        // ๊ถŒํ•œ์ฒดํฌ
        store.requestAccess(for: .contacts) { granted, error in
            guard granted else { return }
                do {
                    // ์—ฐ๋ฝ์ฒ˜ ๋ฐ์ดํ„ฐ ํš๋“
                    try store.enumerateContacts(with: request) { (contact, stop) in
                        guard let phoneNumber = contact.phoneNumbers.first?.value.stringValue else { return }
                        let id = contact.identifier
                        let givenName = contact.givenName
                        let familyName = contact.familyName
                        
                        let contactToAdd = Contact(id: id, firstName: familyName, givenName: givenName, phoneNumber: phoneNumber)
                        contacts.append(contactToAdd)
                    }
                } catch let error {
                    print(error.localizedDescription)
                }

            DataManager.shared.contacts = contacts
        }
    }
}

 

ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณผ๊ฒŒ์š”~!

let store = CNContactStore()
var contacts: [Contact] = []

์—ฐ๋ฝ์ฒ˜ ๊พธ๋Ÿฌ๋ฏธ(?)๋ฅผ CNContactStore ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค. ๋ง ๊ทธ๋Œ€๋กœ "์—ฐ๋ฝ์ฒ˜ ์ƒ์ ", ์ฆ‰ ์—ฐ๋ฝ์ฒ˜ ๊พธ๋Ÿฌ๋ฏธ๋ž€ ์†Œ๋ฆฌ์ฃ !

Contact ํด๋ž˜์Šค๋Š” ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์คฌ์–ด์š”.

์‹ค์ œ๋กœ ์—ฐ๋ฝ์ฒ˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€๋„ ์ œ๊ฐ€ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฆ„, id(์‹๋ณ„์ •๋ณด), ํ•ธ๋“œํฐ๋ฒˆํ˜ธ ์ด์ •๋„ ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.

 

// ์—ฐ๋ฝ์ฒ˜์— ์š”์ฒญํ•  ํ•ญ๋ชฉ
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey] as [CNKeyDescriptor]
// Request ์ƒ์„ฑ
let request = CNContactFetchRequest(keysToFetch: keys)
request.sortOrder = CNContactSortOrder.userDefault

CNKeyDescriptor๋Š” ์—ฐ๋ฝ์ฒ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ• ๋•Œ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•˜๋Š” ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜์š”.

์ € ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์„ฑ, ์ด๋ฆ„, ํ•ธ๋“œํฐ๋ฒˆํ˜ธ๋งŒ ์š”์ฒญํ•˜๊ฒŒ๋” ํ•œ๊ฑฐ์ฃ .

CNContactFetchRequest() ๋Š” ๋ฐฉ๊ธˆ ์„ค์ •ํ•œ key๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹ค์ œ๋กœ ์š”์ฒญ์— ์‚ฌ์šฉํ•  ์š”์ฒญ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฑฐ์—์š”.

CNContactSortOrder๋ฅผ ํ†ตํ•ด์„œ ์ •๋ ฌ์„ ํ•ด์„œ ๋ฐ›์•„์˜ฌ ์ˆ˜๋„ ์žˆ๊ณ ์š”! ์ €์˜ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฆ„์ˆœ์œผ๋กœ ๋ฐ›์•„์˜ค๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค!

 

 // ๊ถŒํ•œ์ฒดํฌ
        store.requestAccess(for: .contacts) { granted, error in
            guard granted else { return }
                do {
                    // ์—ฐ๋ฝ์ฒ˜ ๋ฐ์ดํ„ฐ ํš๋“
                    try store.enumerateContacts(with: request) { (contact, stop) in
                        guard let phoneNumber = contact.phoneNumbers.first?.value.stringValue else { return }
                        let id = contact.identifier
                        let givenName = contact.givenName
                        let familyName = contact.familyName
                        
                        let contactToAdd = Contact(id: id, firstName: familyName, givenName: givenName, phoneNumber: phoneNumber)
                        contacts.append(contactToAdd)
                    }
                } catch let error {
                    print(error.localizedDescription)
                }

            DataManager.shared.contacts = contacts
        }

store.requestAccess ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ ‘๊ทผ๊ถŒํ•œ์„ ์„ค์ •ํ•˜๊ณ , ์ด๋ฏธ ์„ค์ •ํ–ˆ๋‹ค๋ฉด ๊ถŒํ•œ ์„ค์ • ์—ฌ๋ถ€๋ฅผ ๋ฐ›์•„์™€์š”.

์ €๋Š” ์—ฐ๋ฝ์ฒ˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ์œผ๋กœ ์ ‘๊ทผ๊ถŒํ•œ์„ ์š”์ฒญํ• ๊ฑฐ๋‹ˆ๊นŒ .contacts๋ฅผ ๋„ฃ์–ด์ค€ ๊ฑฐ์—์š”~!

 

๋งŒ์•ฝ ๊ถŒํ•œ์ด ์žˆ๋‹ค๋ฉด, enumerateContacts() ๋ฉ”์†Œ๋“œ๋กœ ์•„๊นŒ ๋งŒ๋“ค์—ˆ๋˜ request๋ฅผ ๋„ฃ์–ด์„œ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ์—ฐ๋ฝ์ฒ˜ ์ •๋ณด๊ฐ€ ์ค„์ค„์ด ๋ฐ›์•„์™€์ง€๊ฒŒ ๋˜๊ณ , ๊ฐ๊ฐ์— ๋Œ€ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง์„ ์ž‘์„ฑํ•ด์ฃผ์‹œ๋ฉด ๋˜์š”.

์ €๋Š” ์—ฐ๋ฝ์ฒ˜ ์ •๋ณด๋ฅผ ์•ฑ ์ „์ฒด์—์„œ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก DataManager๋ผ๋Š” ํด๋ž˜์Šค์˜ ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด์— ํ• ๋‹นํ•ด์ค€ ๊ฑฐ์—์š”.

 

์ด๋ ‡๊ฒŒ ์—ฐ๋ฝ์ฒ˜๋ฅผ ๊ฐ€์ ธ์™€๋†“์œผ๋ฉด, ์•ฑ์˜ ํ•„์š”ํ•œ ๊ณณ์—์„œ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๊ฒ ์ฃ ~~? ๐Ÿ˜Ž

 


Bento ํ”„๋กœ์ ํŠธ์ธ ๋งŒํผ ์ด๋Ÿฐ์‹์œผ๋กœ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋๋‚ด๋ดค์–ด์š”!!

์•ฑ์—์„œ ์—ฐ๋ฝ์ฒ˜ ์‚ฌ์šฉ์ด ํ•„์š”ํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜์ž๊ตฌ์š”~~๐Ÿ‘

 

 

์˜ค๋Š˜๋„ ๋ˆ„๊ตฐ๊ฐ€์—๊ฒŒ ๋„์›€์ด ๋˜์—ˆ๊ธธ ๋ฐ”๋ž˜์š” ๐Ÿ™

 

 

'iOS [Swift] > ๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift] AssociatedType  (0) 2021.08.23
[Swift] Error Handling  (0) 2021.07.27
[RxSwift] Subject  (0) 2021.03.30
[RxSwift] RxSwift ์žฅ๋‹จ์   (0) 2021.03.30
[RxSwift] RxSwift ์ž…๋ฌธํ•˜๊ธฐ  (4) 2021.03.30
Comments