iOS 10에서 카메라 및 라이브러리에 대한 권한 요청-Info.plist
앱에서 WKWebView를 구현했습니다. 표시된 웹 페이지에 사진에서 이미지를 가져와야하는 파일 입력이 있습니다. 해당 입력을 누르고 "사진 찍기"또는 "사진 보관함"을 선택할 때마다 앱이 갑자기 충돌합니다. 앱이 사진을 찍거나 라이브러리에서 가져올 수있는 권한이 없기 때문이라고 생각합니다.
사용자가 언급 된 방법 (사진 찍기 또는 사진 라이브러리) 중 하나를 선택할 때 권한 요청을 어떻게 푸시합니까?
WKWebView와 함께 Swift 3.0을 사용합니다.
Info.plist에 아래 권한을 추가해야합니다. 더 많은 추천
카메라 :
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
사진 :
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use
프로그래밍 방식으로 액세스를 요청할 수도 있습니다. 대부분의 경우 액세스 권한을 얻었는지 여부를 알아야하기 때문에 선호합니다.
Swift 4 업데이트 :
//Camera
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
//access granted
} else {
}
}
//Photos
let photos = PHPhotoLibrary.authorizationStatus()
if photos == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
if status == .authorized{
...
} else {}
})
}
당신은 코드를 공유하지 않기 때문에 이것이 당신에게 도움이 될지 확신 할 수 없지만 일반적으로 그것을 모범 사례로 사용합니다.
스위프트 4
파일 : Info.plist
카메라
<key>NSCameraUsageDescription</key>
<string>camera description.</string>
사진
<key>NSPhotoLibraryUsageDescription</key>
<string> photos description.</string>
사진 저장
<key>NSPhotoLibraryAddUsageDescription</key>
<string> photos add description.</string>
위치
<key> NSLocationWhenInUseUsageDescription</key>
<string> location description.</string>
Apple 음악 :
<key>NSAppleMusicUsageDescription</key>
<string>My description about why I need this capability</string>
달력
<key>NSCalendarsUsageDescription</key>
<string>My description about why I need this capability</string>
시리
<key>NSSiriUsageDescription</key>
<string>My description about why I need this capability</string>
위에서 언급 한 plist 설정과 적절한 접근 자 (AVCaptureDevice 또는 PHPhotoLibrary)를 사용하지만, 실제로 필요한 경우 다음과 같이 경고하고 설정으로 보냅니다.
Swift 4.0 및 4.1
func proceedWithCameraAccess(identifier: String){
// handler in .requestAccess is needed to process user's answer to our request
AVCaptureDevice.requestAccess(for: .video) { success in
if success { // if request is granted (success is true)
DispatchQueue.main.async {
self.performSegue(withIdentifier: identifier, sender: nil)
}
} else { // if request is denied (success is false)
// Create Alert
let alert = UIAlertController(title: "Camera", message: "Camera access is absolutely necessary to use this app", preferredStyle: .alert)
// Add "OK" Button to alert, pressing it will bring you to the settings app
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
}))
// Show the alert with animation
self.present(alert, animated: true)
}
}
}
파일 : Info.plist
의 경우 카메라 :
<key>NSCameraUsageDescription</key>
<string>You can take photos to document your job.</string>
들어 사진 라이브러리 ,이 하나의 앱 사용자가 사진 라이브러리를 검색 할 수 있도록 할 것입니다.
<key>NSPhotoLibraryUsageDescription</key>
<string>You can select photos to attach to reports.</string>
Swift 4 The easiest way to add permissions without having to do it programatically, is to open your info.plist file and select the + next to Information Property list and scroll through the drop down list to the Privacy options and select Privacy Camera Usage Description for accessing camera or Privacy Photo Library Usage Description for accessing the Photo Library. Fill in the String value on the right after you've made your selection to include the text you would like displayed to your user when the alert pop up asks for permissions.
To ask permission for the photo app you need to add this code (Swift 3):
PHPhotoLibrary.requestAuthorization({
(newStatus) in
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
}
})
'Development Tip' 카테고리의 다른 글
karma가 이미 설치된 경우 karma 명령을 찾을 수 없습니다. (0) | 2020.11.02 |
---|---|
클라이언트 측에서 암호 암호화 (0) | 2020.11.02 |
제로 패딩이없는 Python 날짜 시간 형식 (0) | 2020.11.02 |
OSX에 MongoDB 설치 및 실행 (0) | 2020.11.02 |
클래스 경로에서 파일 인스턴스로 파일을로드 / 참조하는 방법 (0) | 2020.11.02 |