버튼을 클릭했을 때 전화 설정을 열려면 어떻게 해야 합니까?
인터넷에 접속할 수 없을 때 알림을 표시하는 기능을 앱에 구현하려고 합니다.경보에는 두 가지 액션(OK 및 Settings)이 있습니다.사용자가 설정을 클릭할 때마다 전화기의 설정으로 프로그램합니다.
저는 Swift와 Xcode를 사용하고 있습니다.
「」를 사용합니다.UIApplication.openSettingsURLString
Swift 5.1 업데이트
override func viewDidAppear(_ animated: Bool) {
let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
스위프트 4.2
override func viewDidAppear(_ animated: Bool) {
let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
⚠(조심해!)
이 답변은 문서화되어 있지 않은 API에 근거하고 있으며, 최근 (iOS12 이후) Apple은 이 접근법으로 앱을 거부하고 있습니다.
원래의 답변은 다음과 같습니다.
스위프트 5
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
스위프트 4
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
메모: 다음 방법은 iOS 11 이하의 모든 버전에서 사용할 수 있습니다. 상위 버전에서는 개인 API이기 때문에 앱이 거부될 수 있습니다.
앱 설정 이외의 설정으로 유저를 이동시키고 싶은 경우가 있습니다.다음 방법을 사용하면 이를 실현할 수 있습니다.
먼저 프로젝트에서 URL 체계를 구성합니다.Target -> Info -> URL Scheme에 있습니다.+ 버튼을 클릭하고 URL Schemes에 prefs를 입력합니다.
스위프트 5
UIApplication.shared.open(URL(string: "App-prefs:Bluetooth")!)
스위프트 3
UIApplication.shared.open(URL(string:"App-Prefs:root=General")!, options: [:], completionHandler: nil)
재빠르다
UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General")!)
목표-C
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];
사용 가능한 모든 URL은 다음과 같습니다.
**IOS에서는 < 12 **
- prefs:root=General&path=개요
- prefs:root=General&path=ACCESSBILITY
- prefs: root=LEMA_MODE
- prefs:root=General&path=AUTOLOK
- prefs : root = General & path = CELLUAR_USAGE
- 프리프: root=밝기
- prefs: root=Prefs
- prefs:root=General&path=DATE_AND_TIME
- prefs:root=FACTIME
- prefs:root=General
- prefs : root = General & path = pre pre
- 프리프: 루트=캐슬
- prefs:root=CALL&path=스토리지_AND_백업
- prefs:root=General&path=INTERNATIONAL
- prefs:root=LOCATION_SERVICES
- prefs:root=ACCOUNT_SETTINGS
- 프리프: root=MUSIC
- prefs:root=MUSIC&path=EQ
- prefs:root=MUSIC&path=VolumeLimit
- prefs:root=General&path=네트워크
- prefs: root=SMB_PLUS_IPOD
- 프리프: root=NOTES
- prefs:root=NOTIMATIONS_ID
- prefs: root=Phone
- prefs: root=Prefs
- prefs:root=General&path=Managed Configuration List
- prefs : root = General & path = pre pre
- prefs:root=Reling&path=호출음
- prefs: root=Prefs
- prefs : root = General & path = pre pre
- prefs: root=Prefs
- prefs:root=General&path=SOFTWARE_UPDATE_LINK
- prefs: root=STORE
- prefs: root=REMP
- prefs: root=REMP
- prefs:root=General&path=Prefs:root=VIDEO
- prefs:root=General&path=네트워크/VPN
- prefs: root=Prefs
- prefs: root=REMP
- prefs:root=INTERNET_THERING
- prefs: root=Phone&path=Prefs
- prefs:root=DO_NOT_DISTURB
IOS 13의 경우
- 앱 준비:General & path=개요
- 앱 준비:비행기 모드
- 앱 준비:일반 경로=자동 잠금
- 앱 준비:블루투스
- 앱 준비:General & path = DATE_AND_TIME
- 앱 준비:동작 시간
- 앱 준비:일반
- 앱 준비:General & path = 표준
- 앱 준비:캐슬
- 앱 준비:캐슬&패스=스토리지_AND_백업
- 앱 준비:일반 경로=국제
- 앱 준비:음악
- 앱 준비:메모들
- 앱 준비:알림_아이디
- 앱 준비:전화
- 앱 준비:사진들
- 앱 준비:General & path=Managed Configuration List(일반 & 경로=관리 구성 목록)
- 앱 준비:General & path = 표준
- 앱 준비:사운드 & 패스 = 호출음
- 앱 준비:소리.
- 앱 준비:일반&path=SOFTWARE_UPDATE_링크
- 앱 준비:가게
- 앱 준비:배경화면
- 앱 준비:와이파이
- 앱 준비:인터넷 테더링
앱 준비:하지 않다
테스트되지 않음
앱 준비:트위터(--)
- 앱 준비:FACEBOOK (--)
- 앱 준비:NIKE_PLUS_IPOD(--)
주의: 네트워크 설정은 시뮬레이터에서 열리지 않지만 실제 디바이스에서는 링크가 작동합니다.
SWIFT 5
if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(settingsUrl)
}
iOS 8+에서는 다음을 수행할 수 있습니다.
func buttonClicked(sender:UIButton)
{
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
}
스위프트 4
let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsUrl)
@vivek의 힌트를 이용하여 Swift 3을 기반으로 한 utils 클래스를 개발했습니다만, 잘 부탁드립니다!
import Foundation
import UIKit
public enum PreferenceType: String {
case about = "General&path=About"
case accessibility = "General&path=ACCESSIBILITY"
case airplaneMode = "AIRPLANE_MODE"
case autolock = "General&path=AUTOLOCK"
case cellularUsage = "General&path=USAGE/CELLULAR_USAGE"
case brightness = "Brightness"
case bluetooth = "Bluetooth"
case dateAndTime = "General&path=DATE_AND_TIME"
case facetime = "FACETIME"
case general = "General"
case keyboard = "General&path=Keyboard"
case castle = "CASTLE"
case storageAndBackup = "CASTLE&path=STORAGE_AND_BACKUP"
case international = "General&path=INTERNATIONAL"
case locationServices = "LOCATION_SERVICES"
case accountSettings = "ACCOUNT_SETTINGS"
case music = "MUSIC"
case equalizer = "MUSIC&path=EQ"
case volumeLimit = "MUSIC&path=VolumeLimit"
case network = "General&path=Network"
case nikePlusIPod = "NIKE_PLUS_IPOD"
case notes = "NOTES"
case notificationsId = "NOTIFICATIONS_ID"
case phone = "Phone"
case photos = "Photos"
case managedConfigurationList = "General&path=ManagedConfigurationList"
case reset = "General&path=Reset"
case ringtone = "Sounds&path=Ringtone"
case safari = "Safari"
case assistant = "General&path=Assistant"
case sounds = "Sounds"
case softwareUpdateLink = "General&path=SOFTWARE_UPDATE_LINK"
case store = "STORE"
case twitter = "TWITTER"
case facebook = "FACEBOOK"
case usage = "General&path=USAGE"
case video = "VIDEO"
case vpn = "General&path=Network/VPN"
case wallpaper = "Wallpaper"
case wifi = "WIFI"
case tethering = "INTERNET_TETHERING"
case blocked = "Phone&path=Blocked"
case doNotDisturb = "DO_NOT_DISTURB"
}
enum PreferenceExplorerError: Error {
case notFound(String)
}
open class PreferencesExplorer {
// MARK: - Class properties -
static private let preferencePath = "App-Prefs:root"
// MARK: - Class methods -
static func open(_ preferenceType: PreferenceType) throws {
let appPath = "\(PreferencesExplorer.preferencePath)=\(preferenceType.rawValue)"
if let url = URL(string: appPath) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
throw PreferenceExplorerError.notFound(appPath)
}
}
}
API는 확실히 바뀌기 때문에 매우 편리합니다.또한 리팩터링도 한 번 빠르게 할 수 있습니다.
iOS 10.3에서는 App-Specific URL Schemes의 첫 번째 응답이 효과가 있었습니다.
if let appSettings = URL(string: UIApplicationOpenSettingsURLString + Bundle.main.bundleIdentifier!) {
if UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
}
App-Prefs:root=Privacy&path=LOCATION일반적인 위치 설정에 액세스 할 수 있었습니다.의:: 디디이이디에에에에다다다다.
iOS 12 이상
open(url:options:completionHandler:)되어 있습니다.이입니다.UIApplication.OpenExternalURLOptionsKey(일부러)
@objc func openAppSpecificSettings() {
guard let url = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(url) else {
return
}
let optionsKeyDictionary = [UIApplication.OpenExternalURLOptionsKey(rawValue: "universalLinksOnly"): NSNumber(value: true)]
UIApplication.shared.open(url, options: optionsKeyDictionary, completionHandler: nil)
}
"App-Prefs"와 같이 URL을 명시적으로 구성하면 일부 앱이 스토어에서 거부되었습니다.
: " " " " :prefs:root ★★★★★★★★★★★★★★★★★」App-Prefs:rootURL "API" "API" "API"이러한 앱을 사용하면 애플이 앱을 거부할 수 있습니다. 앱 제출 시 얻을 수 있는 내용은 다음과 같습니다.
앱에서 비공개 엔티티인 "prefs:root=" 비공개 URL 체계를 사용합니다.공개되지 않은 API는 변경될 경우 사용자 환경이 나빠질 수 있으므로 App Store에서는 사용이 허용되지 않습니다.이 앱의 향후 제출 시 공개되지 않은 API를 계속 사용하거나 숨기면 Apple Developer 계정이 종료되고 App Store에서 관련된 모든 앱이 삭제될 수 있습니다.
다음 단계
이 문제를 해결하려면 공용 API를 사용하여 관련 기능을 제공하도록 앱을 수정하거나 "prefs:root" 또는 "App-Prefs:root" URL 스키마를 사용하여 기능을 제거하십시오.
앱에 필요한 기능을 제공할 수 있는 대안이 없는 경우 개선 요청을 제출할 수 있습니다.
ios10/Xcode 8의 시뮬레이터:
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
작동하다
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
하지 않다.
나는 이 코드 줄을 본 적이 있다.
UIApplication.sharedApplication() .openURL(NSURL(string:"prefs:root=General")!)
동작하지 않습니다.ios10/Xcode8에서는 동작하지 않습니다.조금씩 코드 차이가 있을 뿐입니다.이것으로 교환해 주세요.
UIApplication.sharedApplication().openURL(NSURL(string:"App-Prefs:root=General")!)
스위프트3
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
다음으로 대체
UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)
도움이 됐으면 좋겠다.건배.
@Luca Davanzo에 추가
iOS 11, 일부 권한 설정이 앱 경로로 이동되었습니다.
iOS 11 지원
static func open(_ preferenceType: PreferenceType) throws {
var preferencePath: String
if #available(iOS 11.0, *), preferenceType == .video || preferenceType == .locationServices || preferenceType == .photos {
preferencePath = UIApplicationOpenSettingsURLString
} else {
preferencePath = "\(PreferencesExplorer.preferencePath)=\(preferenceType.rawValue)"
}
if let url = URL(string: preferencePath) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
throw PreferenceExplorerError.notFound(preferencePath)
}
}
SWIFT 4
원하는 경우 앱의 특정 설정을 사용할 수 있습니다.
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
UIApplication.open(_:options:completionHandler:)주 스레드에서만 사용해야 합니다.
솔루션:
if let appSettings = URL(string: UIApplication.openSettingsURLString + Bundle.main.bundleIdentifier!) {
if UIApplication.shared.canOpenURL(appSettings) {
DispatchQueue.main.async {
UIApplication.shared.open(appSettings)
}
}
}
위와 같이 @niravdesai는 App-prefs라고 말했습니다.나는 그것을 발견했다.App-Prefs:는 iOS 9, 10, 11 모두에서 동작합니다.테스트 대상 디바이스입니다.반면에.prefs:iOS 9에서만 동작합니다.
언급URL : https://stackoverflow.com/questions/28152526/how-do-i-open-phone-settings-when-a-button-is-clicked
'source' 카테고리의 다른 글
| 텍스트를 숫자로 변환하는 방법 (0) | 2023.04.19 |
|---|---|
| 키보드를 사용하여 Excel 셀에서 메뉴를 드롭다운하는 방법 (0) | 2023.04.19 |
| Python List는 얼마나 클 수 있습니까? (0) | 2023.04.19 |
| 프로그래밍 방식으로 WPF 데이터 그리드에 열 및 행 추가 (0) | 2023.04.19 |
| grep를 사용하여 한 줄에 두 문자열을 일치시킵니다. (0) | 2023.04.19 |
