Call Customer Care Button in SwiftUI

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

In today’s fast-paced world, customer service is more important than ever. As a business, it’s essential to provide your customers with a seamless and efficient experience when they need help. One way to do this is by implementing a “Call to Customer Care” feature in your iOS app.

SwiftUI is a powerful tool for building user interfaces on iOS, and it’s perfect for creating a button that allows users to call customer support with just a tap. In this blog post, we’ll show you how to create a call to customer care button in SwiftUI.

First, you’ll need to create a new SwiftUI View that will contain the button. Inside the view, you’ll add a Button element and give it a title, such as “Call Customer Care.” Next, you’ll need to add an action to the button that makes the phone call. You can do this using the UIApplication.shared.open() method, which opens a URL with the phone number of your customer support.

Here’s an example of what the code for the view might look like:

struct CallToCustomerCareView: View {
    var body: some View {
        Button(action: {
            UIApplication.shared.open(URL(string: "tel://1234567890")!)
        }) {
            Text("Call Customer Care")
        }
    }
}

In this example, the button will open the phone app and dial the number “1234567890” when tapped. You can replace this number with the actual phone number for your customer support.

You can also customize the button’s appearance using SwiftUI’s built-in styling options. For example, you can change the button’s color, font, and padding to match the design of your app.

Activate/Deactivate based on time

Here’s an example of how you can implement a call to customer care button in SwiftUI that is only active during specific hours of the day and deactivated on weekends:

struct CallToCustomerCareView: View {
    @State private var isButtonEnabled = true
    
    var body: some View {
        Button(action: {
            UIApplication.shared.open(URL(string: "tel://1234567890")!)
        }) {
            Text("Call Customer Care")
        }
        .disabled(!isButtonEnabled)
        .onAppear {
            checkButtonAvailability()
        }
    }
    
    func checkButtonAvailability() {
        let currentDate = Date()
        let calendar = Calendar.current
        let hour = calendar.component(.hour, from: currentDate)
        let weekday = calendar.component(.weekday, from: currentDate)
        
        if weekday == 1 || weekday == 7 {
            // deactivate button on weekends
            isButtonEnabled = false
        } else if hour < 9 || hour >= 17 {
            // deactivate button outside of business hours
            isButtonEnabled = false
        } else {
            isButtonEnabled = true
        }
    }
}

In this example, the button is initially created with an action that opens the phone app and dials the customer support number when tapped. The button’s isEnabled property is controlled by the isButtonEnabled state variable. The .disabled() modifier is used to deactivate the button when the isButtonEnabled variable is set to false.

The checkButtonAvailability() function is called on the view’s onAppear() event, which means it will be called every time the view appears on the screen. Inside the function, the current date and time are retrieved using the Date() and Calendar.current classes. The hour and weekday are extracted from the date using the calendar.component() method.

If the current day is a weekend, the button is deactivated. If it’s a weekday, the button is deactivated if the current hour is before 9AM or after 5PM. If the current time is within business hours, the button is activated.

This way, the button will only be active during specific hours of the day and deactivated on weekends, providing a convenient way for customers to reach out to customer support during the time they are open.



✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



Get Ready to Shine: Mastering the iOS Interview




Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.