Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 위치 정보 접근 권한 허용 여부 기능 추가 #49

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Ondoset/Ondoset.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1286,13 +1286,13 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Ondoset/Preview Content\"";
DEVELOPMENT_TEAM = 8T2JAAS967;
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Ondoset/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "옷 사진 업로드 및 착장 사진 업로드 기능을 위해 카메라 접근 권한이 필요합니다.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "착장 추천 및 업로드 기능을 위해 위치 정보 접근 권한이 필요합니다.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "사진 업로드 및 착장 사진 업로드 기능을 위해 앨범 접근 권한이 필요합니다.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "착장 추천 및 업로드 기능을 위해 \n위치 정보 접근 권한이 필요합니다. \n허용하지 않으실 경우 \n서비스를 이용하실 수 없습니다.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "사진 앨범에 접근할 수 있도록 접근 권한을 허용해주세요.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down Expand Up @@ -1327,13 +1327,13 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Ondoset/Preview Content\"";
DEVELOPMENT_TEAM = 8T2JAAS967;
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Ondoset/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "옷 사진 업로드 및 착장 사진 업로드 기능을 위해 카메라 접근 권한이 필요합니다.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "착장 추천 및 업로드 기능을 위해 위치 정보 접근 권한이 필요합니다.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "사진 업로드 및 착장 사진 업로드 기능을 위해 앨범 접근 권한이 필요합니다.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "착장 추천 및 업로드 기능을 위해 \n위치 정보 접근 권한이 필요합니다. \n허용하지 않으실 경우 \n서비스를 이용하실 수 없습니다.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "사진 앨범에 접근할 수 있도록 접근 권한을 허용해주세요.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down
9 changes: 5 additions & 4 deletions Ondoset/Ondoset/Common/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import UIKit
struct Constants {

static let successResponseCode: String = "common_2000"
static var serverURL = isAvailableUnivURL ? univURL : ec2URL
static var serverURL = isDevelopURL ? developURL : ec2URL
}

// 학과서버 죽으면 false, 쓸 수 있으면 true
let isAvailableUnivURL: Bool = false
let isDevelopURL: Bool = false

let univURL = "http://ceprj.gachon.ac.kr:60019"
// 개발용
let developURL = "http://ec2-43-201-46-189.ap-northeast-2.compute.amazonaws.com:8080/member/test"
//let ec2URL = "http://ec2-43-201-46-189.ap-northeast-2.compute.amazonaws.com:8080"
/// 운영용
let ec2URL = "https://ondoset.shop"


Expand Down
71 changes: 54 additions & 17 deletions Ondoset/Ondoset/Common/Utils/LocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,64 @@ import CoreLocation

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {

private let locationManager = CLLocationManager()
@Published var currentLocation: CLLocationCoordinate2D?
public let locationManager = CLLocationManager()
@Published var currentLocation: CLLocationCoordinate2D?
@Published var authorizationStatus: CLAuthorizationStatus?
@Published var showSettingAlert = false

override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}

func requestLocation() {
locationManager.requestLocation() // 단일 위치 요청
}
func requestLocation() {
locationManager.requestLocation() // 단일 위치 요청
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
currentLocation = location.coordinate
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
currentLocation = location.coordinate
}
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Failed to find user's location: \(error.localizedDescription)")
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Failed to find user's location: \(error.localizedDescription)")
}

func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .notDetermined:
/// 아직 정해지지 않음
print("notDetermined")
case .restricted:
print("restricted")
case .denied:
/// 허용 안함
/// 사용자가 한 번 거부하면 반복적으로 물어볼 수 없습니다.
print("denied")
UserDefaults.standard.set(LocationAuthorizationType.denied.rawValue, forKey: "locationAuthorization")
showSettingAlert = true
case .authorizedAlways:
print("authorzedAlways")
case .authorizedWhenInUse:
/// 한 번 허용 & 앱을 사용하는 동안 허용
print("authorizedWhenInUse")
UserDefaults.standard.set(LocationAuthorizationType.authorizedWhenInUse.rawValue, forKey: "locationAuthorization")
@unknown default:
print("default")
}
}
}

enum LocationAuthorizationType: String {
/// 아직 정해지지 않음
case notDetermined
/// 허용 안 함
case denied
/// 앱을 사용하는 동안 허용
case authorizedWhenInUse
/// 한 번 허용
case authorizedOnce
}
2 changes: 2 additions & 0 deletions Ondoset/Ondoset/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
<string>Pretendard-SemiBold.otf</string>
<string>Pretendard-Thin.otf</string>
</array>
<key>NSPhotoLibraryUsageDescription</key>
<string>앱에서 사진을 선택하기 위해 접근 권한이 필요합니다.</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct HomeMainView: View {
.onAppear(perform: {
homeMainVM.homeViewDate = .now

locationManager.requestLocation()
// locationManager.requestLocation()

if let location = locationManager.currentLocation {

Expand Down
52 changes: 39 additions & 13 deletions Ondoset/Ondoset/Presentation/Initial/SplashView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
import SwiftUI

struct SplashView: View {

@State var isActive: Bool = false // 다음 화면 활성화
@State var isActive: Bool = false
@AppStorage("isLogin") var isLogin: Bool = false
@AppStorage("isFirst") var isFirst: Bool = true
@StateObject var locationManager: LocationManager = LocationManager()
@AppStorage("locationAuthorization") var locationAuth: LocationAuthorizationType = .notDetermined

init() {
UserDefaults.standard.register(defaults: ["locationAuthorization": LocationAuthorizationType.notDetermined.rawValue])
}

var body: some View {
ZStack {

if !isLogin && isActive {
SignInView()
} else if isLogin && isActive {
OndosetHome()
// } else if isLogin && isActive && !isFirst {
// OndosetHome()
// } else if isLogin && isActive && isFirst {
// OnboardingView()
} else {

ZStack {
Color(.main)
VStack {
Expand All @@ -38,15 +37,42 @@ struct SplashView: View {
}
}
.onAppear {

DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
if locationAuth == .notDetermined {
locationManager.locationManager.requestWhenInUseAuthorization()
} else if locationAuth == .authorizedWhenInUse {
isActive = true
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
let status = locationManager.locationManager.authorizationStatus
if status == .authorizedWhenInUse {
locationAuth = .authorizedWhenInUse
isActive = true
} else if status == .denied {
locationAuth = .denied
locationManager.showSettingAlert = true
}
}
.alert(isPresented: $locationManager.showSettingAlert) {
Alert(
title: Text("위치 권한 필요"),
message: Text("위치 권한이 거부되었습니다. 위치 정보를 사용하려면 설정에서 권한을 허용해주세요."),
primaryButton: .default(Text("설정으로 이동"), action: {
if let appSetting = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(appSetting)
}
}),
secondaryButton: .cancel(Text("취소"), action: {
if locationAuth == .denied {
locationManager.locationManager.requestWhenInUseAuthorization()
}
})
)
}
.ignoresSafeArea()
}
}

#Preview {
SplashView()
}
//#Preview {
// SplashView(locationManager: LocationManager())
//}