๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS] Callkit๊ณผ PushKit์„ ์ด์šฉํ•ด์„œ ์ „ํ™” ํ‘ธ์‰ฌํ•˜๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 4. 12.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ Callkit์„ ์ด์šฉํ•ด์„œ ์ „ํ™”์˜ค๋Š” ํ™”๋ฉด์„ ๋„์›Œ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Xcode

 

ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์‹  ๋’ค Signing & Capabitities ๋กœ ์ด๋™ํ•˜์…”์„œ Background modes๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๊ณ  

 

Voice over IP๋ฅผ ์ฒดํฌํ•ด์ฃผ์„ธ์š”!

 


ViewController

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ™์—ฌ๋„ฃ์œผ์‹  ๋’ค ์‹คํ–‰์‹œ์ผœ์ฃผ์„ธ์š”. (์‹ค์ œ ๋””๋ฐ”์ด์Šค๋กœ ์‹คํ–‰์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค!)

import UIKit
import CallKit

class ViewController: UIViewController, CXProviderDelegate {

    override func viewDidLoad() {
        let provider = CXProvider(configuration: CXProviderConfiguration())
        provider.setDelegate(self, queue: nil)
        let update = CXCallUpdate()
        update.remoteHandle = CXHandle(type: .generic, value: "Fomagran")
        provider.reportNewIncomingCall(with: UUID(), update: update, completion: { error in })
    }

    func providerDidReset(_ provider: CXProvider) {
    }

    func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
        action.fulfill()
    }

    func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
        action.fulfill()
    }

}

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •ํ•ด์ค€ ์ด๋ฆ„์œผ๋กœ ์‹ค์ œ ๋””๋ฐ”์ด์Šค์— ์ „ํ™”๊ฐ€ ์˜ค๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 


์ „ํ™” ํ‘ธ์‰ฌํ•˜๊ธฐ

 

Apple Developer

 

์• ํ”Œ ๊ฐœ๋ฐœ์ž ์‚ฌ์ดํŠธ๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š”.

Register a Identifier

 

์™ผ์ชฝํƒญ์—์„œ identifier๋ฅผ ๋ˆ„๋ฅด์‹  ํ›„ ์ƒˆ๋กœ์šด identifier๋ฅผ ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”.

 

 

App IDs ๋ฅผ ๋ˆ„๋ฅด์‹œ๊ณ 

App์„ ํด๋ฆญํ•œ ๋’ค continue๋ฅผ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

 

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์˜ Bundle ID๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.

 

 

์•„๋ž˜๋กœ ๋‚ด๋ ค์„œ Push Notifications๋„ ์ฒดํฌํ•ด์ค๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•œ ๋’ค Register๋ฅผ ๋ˆŒ๋Ÿฌ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค.


Create a Certiciate

 

๊ทธ ๋‹ค์Œ์œผ๋กœ Certificate๋ฅผ ์ƒ์„ฑํ• ๊ฑด๋ฐ์š”.

 

Certificates์˜ +๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

 

 

์•„๋ž˜๋กœ ๋‚ด๋ฆฐ ๋’ค VoIP Services Certificate๋ฅผ ํด๋ฆญํ•ด์ฃผ์„ธ์š”.

 

 

๊ทธ๋ฆฌ๊ณค ์œ„์—์„œ ๋งŒ๋“ค์–ด์ฃผ์‹  identifier ์•ฑ์„ ๊ณจ๋ผ์ค๋‹ˆ๋‹ค.

 

์ด๋Œ€๋กœ ์œ ์ง€ํ•˜์‹  ๋‹ค์Œ ํ‚ค์ฒด์ธ ์ ‘๊ทผ ์•ฑ์„ ์ผœ์ฃผ์„ธ์š”!


KeyChainAccess

 

์ธ์ฆ ๊ธฐ๊ด€์—์„œ ์ธ์ฆ์„œ ์š”์ฒญ์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฉ”์ผ์„ ์ ๊ณ  ๋””์Šคํฌ์— ์ €์žฅ๋จ์„ ์ฒดํฌํ•ด์ฃผ์„ธ์š”.

 

 

์ธ์ฆ์„œ ์š”์ฒญ์„ ์ž˜ ์ €์žฅํ•ด๋‘ก๋‹ˆ๋‹ค.

 

 

 

๊ทธ๋ฆฌ๊ณค ๋‹ค์‹œ ์• ํ”Œ ๊ฐœ๋ฐœ์ž ์‚ฌ์ดํŠธ๋กœ ์™€์„œ 

 

์œ„์—์„œ ์ €์žฅํ•ด๋‘” ์ธ์ฆ์„œ ์š”์ฒญ ํŒŒ์ผ์„ Choose File์„ ์„ ํƒํ•ด ๊ณจ๋ผ์ฃผ๊ณ  Continue๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

 

๋งˆ์ง€๋ง‰์œผ๋กœ Certificate๋ฅผ Downloadํ•ด์ค๋‹ˆ๋‹ค.

 

 

๋‹ค์šด๋กœ๋“œํ•œ Certificate ํŒŒ์ผ์„ ๋ˆ„๋ฅด๋ฉด ํ‚ค์ฒด์ธ ์ ‘๊ทผ์•ฑ์ด ๋œฐ๊ฑฐ์—์š”.

 

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด VoIP ์ธ์ฆ์„œ๊ฐ€ ๋ณด์ผ๊ฑฐ์—์š” ์ด๊ฑธ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•ด์ฃผ์„ธ์š”.

 

 

 

๊ทธ๋ฆฌ๊ณค ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ํ•ด์ฃผ์„ธ์š”.

 

 

์ด๋ฆ„์„ ์ •ํ•ด์ฃผ์‹œ๊ณ 

 

ํŒŒ์ผ ํฌ๋งท์€ .p12๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”.

 

์•”ํ˜ธ๋ฅผ ์ •ํ•ด์ฃผ์‹œ๋ฉด 

 

์•„๋ž˜์™€ ๊ฐ™์ด p12 ํŒŒ์ผ์ด ์ €์žฅ๋œ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


Terminal

 

์ด์ œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์ค๋‹ˆ๋‹ค.

 

p12ํŒŒ์ผ์ด ์ €์žฅ๋˜์–ด ์žˆ๋Š”๊ณณ์œผ๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š” cd ๊ฒฝ๋กœ

 

๊ทธ๋ฆฌ๊ณค ์•„๋ž˜์™€ ๊ฐ™์ด p12ํŒŒ์ผ์˜ ์ด๋ฆ„๊ณผ ๋ณ€ํ™˜ํ•ด์ค„ pemํŒŒ์ผ์˜ ์ด๋ฆ„์„ ์ ์–ด์ฃผ๊ณ  ์œ„์—์„œ ์„ค์ •ํ–ˆ๋˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ ์–ด์ฃผ์„ธ์š”.

openssl pkcs12 -in ์ธ์ฆ์„œ์ด๋ฆ„.p12 -out ์ธ์ฆ์„œ์ด๋ฆ„.pem

pemํŒŒ์ผ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ •ํ•ด์ค๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด pemํŒŒ์ผ์ด ์ƒ๊ธด๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


Xcode

 

xcode๋กœ ์™€์„œ target์—์„œ Signing & Capabitities ์—์„œ Push Notifications๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ชจ๋“œ์—์„œ Remote notifications๋„ ์ฒดํฌํ•ด์ฃผ์„ธ์š”!

ViewController

 

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ์–ด์ฃผ์„ธ์š”!

import UIKit
import CallKit
import PushKit

class ViewController: UIViewController, CXProviderDelegate, PKPushRegistryDelegate {

    override func viewDidLoad() {
        let registry = PKPushRegistry(queue: nil)
        registry.delegate = self
        registry.desiredPushTypes = [PKPushType.voIP]
    }

    func providerDidReset(_ provider: CXProvider) {
    }

    func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
        action.fulfill()
    }

    func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
        action.fulfill()
    }

    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        print(pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined())
    }

    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
        let config = CXProviderConfiguration()
        config.includesCallsInRecents = false
        config.supportsVideo = true;
        let provider = CXProvider(configuration: config)
        provider.setDelegate(self, queue: nil)
        let update = CXCallUpdate()
        update.remoteHandle = CXHandle(type: .generic, value: "Fomagran")
        update.hasVideo = true
        provider.reportNewIncomingCall(with: UUID(), update: update, completion: { error in })
    }

}

 

์ด๋ ‡๊ฒŒ ์‹คํ–‰์‹œํ‚ค๊ณ  ์ „ํ™”๋ฅผ ๋ฐ›๊ฒŒ ๋˜๋ฉด ํ˜„์žฌ ๋””๋ฐ”์ด์Šค์˜ ํ† ํฐ๊ฐ’์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


Terminal

 

ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•˜์…”์„œ ํŽจํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜์‹ ๋’ค 

 

์•„๋ž˜์™€ ๊ฐ™์ด pemํŒŒ์ผ๊ณผ pemํŒŒ์ผ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ทธ๋ฆฌ๊ณ  ๋””๋ฐ”์ด์Šค ํ† ํฐ๊ฐ’์„ ๋„ฃ์–ด์ค€๋’ค enter๋ฅผ ์น˜๋ฉด

curl -v -d '{"aps":{"alert":"ํ•˜์ด"}}' --http2 --cert ์ธ์ฆ์„œ์ด๋ฆ„.pem:๋น„๋ฐ€๋ฒˆํ˜ธ https://api.development.push.apple.com/3/device/๋””๋ฐ”์ด์Šคํ† ํฐ๊ฐ’

 

์•ฑ์ด ์ผœ์ ธ์žˆ์ง€ ์•Š์•„๋„ ๋””๋ฐ”์ด์Šค ํ† ํฐ๊ฐ’์„ ๊ฐ€์ง„ ๋””๋ฐ”์ด์Šค์— ์ „ํ™”๊ฐ€ ๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

 

 


Reference

 

 

CallKit iOS Swift Tutorial for VoIP Apps (Super Easy)

If you're making a video chat or audio chat app, you can give your users a much better experience by using iOS CallKit. By the end of this Swift tutorial you'll learn how to make it so that when a user tries to start a video chat or audio chat with a frien

websitebeaver.com

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€