How to open settings programmatically in Swift


import SwiftUI



struct ButtonUI: View {

    var body: some View {

        

            ZStack {

                

                Color.white

            RoundedRectangle(cornerRadius: 50)

                    .fill(.blue)

                    .frame(width: 250, height: 75, alignment: .center)

                

                Text("Enable Location")

                    .font(.title3)

                    .fontWeight(.bold)

                    .foregroundColor(Color.white)

                

            }

            .offset(x: 0, y: 300)



    }

}



struct ButtonUI_Previews: PreviewProvider {

    static var previews: some View {

        ButtonUI()

    }

}

// I have used this code to create a button like shape in Xcode how to do i make it work like open the location settings in iPhone

// it should work like a button to open the location settings in iPhone


  • What do you mean precisely by location settings

  • I need a button in my app , when i click that button it should open the iPhone settings app and automatically navigate through the location settings

Add a Comment

Replies

You can access your app's settings page URL from the UIApplication.openSettingsURLString property.

Use it like this:

Button("Open Settings") {
    // Get the settings URL and open it
    if let url = URL(string: UIApplication.openSettingsURLString) {
        UIApplication.shared.open(url)
    }
}


There is currently no way to deep link directly to your app's location settings.

iOS 16 did, however, introduce a UIApplication.openNotificationSettingsURLString property for the notifications settings section, so maybe the location settings URL is possible in the future, or through this.

  • I will try it and update it to you thank you

  • sorry to bother as I have just started to learn Swift I don't know how to take the url can you give me the full code containing the settings url

  • That is all the code you need. UIApplication.openSettingsURLString is a predefined string essentially provided to you by your app – you don't need to type it out yourself.