[FAQ] 지도/로컬 API 문의 전 꼭 읽어 주세요.
일단 코드는 이런식으로 작성했습니다.
import SwiftUI
import KakaoMapsSDK
@main
struct KakaoMapTutorialApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear(perform: {
guard let apiKey = Bundle.main.apiKey else {
print("API 키 로드 실패!!")
return
}
SDKInitializer.InitSDK(appKey: apiKey)
// print(SDKInitializer.GetAppKey())
})
}
}
}
import SwiftUI
import KakaoMapsSDK
extension Bundle {
var apiKey: String? {
return infoDictionary?["API_KEY"] as? String
}
}
struct ContentView: View {
@State private var draw: Bool = false
var body: some View {
MapView(draw: $draw).onAppear(perform: {
self.draw = true
}).onDisappear(perform: {
self.draw = false
}).frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
import Foundation
import SwiftUI
import KakaoMapsSDK
struct MapView: UIViewRepresentable {
@Binding var draw: Bool
func makeUIView(context: Context) -> KMViewContainer {
let view: KMViewContainer = KMViewContainer()
view.sizeToFit()
context.coordinator.createController(view)
context.coordinator.controller?.prepareEngine()
return view
}
func updateUIView(_ uiView: KMViewContainer, context: Context) {
if draw {
context.coordinator.controller?.activateEngine()
} else {
context.coordinator.controller?.resetEngine()
}
}
func makeCoordinator() -> KMCoordinator {
return KMCoordinator()
}
static func dismantleUIView(_ uiView: KMViewContainer, coordinator: KMCoordinator) {
}
class KMCoordinator: NSObject, MapControllerDelegate {
var controller: KMController?
var first: Bool
override init() {
first = true
super.init()
}
func createController(_ view: KMViewContainer) {
controller = KMController(viewContainer: view)
controller?.delegate = self
}
func addViews() {
let defaultPosition: MapPoint = MapPoint(longitude: 14135167.020272, latitude: 4518393.389136)
let mapviewInfo: MapviewInfo = MapviewInfo(viewName: "mapview", viewInfoName: "map", defaultPosition: defaultPosition)
controller?.addView(mapviewInfo)
}
func addViewSucceeded(_ viewName: String, viewInfoName: String) {
print("OK")
}
func addViewFailed(_ viewName: String, viewInfoName: String) {
print("Failed")
}
func authenticationSucceeded() {
print("auth succeed!!")
}
func authenticationFailed(_ errorCode: Int, desc: String) {
print("auth failed")
print("error code: \(errorCode)")
print(desc)
// print(controller?.getStateDescMessage())
}
func containerDidResized(_ size: CGSize) {
let mapView: KakaoMap? = controller?.getView("mapview") as? KakaoMap
mapView?.viewRect = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: size)
if first {
let cameraUpdate: CameraUpdate = CameraUpdate.make(
target: MapPoint(longitude: 14135167.020272, latitude: 4518393.389136),
zoomLevel: 10,
mapView: mapView!
)
mapView?.moveCamera(cameraUpdate)
first = false
}
}
}
}
AppKey 인증 후 .GetAppKey() 로 확인해 보면 AppKey 인증은 정상적으로 된걸로 나옵니다.
그런데 ContentView 에서 MapView 불러오면 401 에러가 나면서 인증이 안 되고 연두색화면만 나옵니다.
AppKey 그대로 복사해 붙여넣었고 플랫폼에 번들id 도 그대로 넣어놨습니다.
소스 코드도 사실상 공식문서에 있는 예제 거의 그대로구요.
어디서 문제가 있어서 이렇게 되는지 잘 모르겠네요.
참고로 AppKey를 xcconfig 파일 안 쓰고 직접 하드코딩해봐도 마찬가지입니다.
아래는 실행 시 콘솔창에 뜨는 로그 내역입니다.
[I][KMSDK] [MTLMapContainer(5b0acd0)] initializeView
[I][KMSDK] [MTLMapContainer(5b0acd0)] Not first run.
[I][KMSDK] ASSET ROOT PATH: /Users/chakan/Library/Developer/CoreSimulator/Devices/F1644494-E11F-4036-A99C-F2AB36FDE8A4/data/Containers/Bundle/Application/CC16160B-706E-467B-8E7A-053738BD18D6/KakaoMapTutorial.app/KakaoMapsSDK-SPM_KakaoMapsSDK-SPM.bundle/assets/
[I][KMSDK] ASSET ROOT PATH:
[I][KMSDK] Version : 2.10.5-b4
[I][K3fCore] app diplay scale: 1.500000
[I][KMSDK] ASSET ROOT PATH: /Users/chakan/Library/Developer/CoreSimulator/Devices/F1644494-E11F-4036-A99C-F2AB36FDE8A4/data/Containers/Bundle/Application/CC16160B-706E-467B-8E7A-053738BD18D6/KakaoMapTutorial.app/KakaoMapsSDK-SPM_KakaoMapsSDK-SPM.bundle/assets/
[I][DiskCache] DiskCache instance count : 1
[I][DiskCache] DB multithreading mode set to SQLITE_CONFIG_SINGLETHREAD
[I][DiskCache] DiskCache check init state..
[I][DiskCache] Dao initialize..
[I][DiskCache] DiskCache db(/Users/chakan/Library/Developer/CoreSimulator/Devices/F1644494-E11F-4036-A99C-F2AB36FDE8A4/data/Containers/Data/Application/46842D6C-734F-4143-B3B3-6D535395510A/Library/Caches/vectormap/cache/db/cache.db) opened.
[I][DiskCache] Buffer initialize lock waiting : 0
[I][DiskCache] Buffer initialize elapsed time : 0, buffer size : 0
[I][DiskCache] DiskCache initialized
[I][K3fCore] DownloadManager initialized
[I][K3fCore] >>> Initialize appEngine[RELEASE]{1721133788}
[I][K3fCore] >>> Start appEngine[RELEASE]{1721133788}
[I][KMSDK] [MTLMapContainer(6bdf2a78)] StartEngine
[I][K3fCore] Engine{1721133788} stopped.
[I][KMSDK] [MTLMapContainer(6bdf2b48)] StopEngine
[I][KMSDK] [MTLMapContainer(5b0acd0)] layoutSubviews
[I][KMSDK] [MTLMapContainer(5b0acd0)] layoutSubviews
[I][Auth] Auth result Received
[I][Auth] Authentication Failed.[401]
auth failed
error code: 401
Unauthorized