Directly Link To Social Media User Profile In Swift Application

Here’s how I use UIButtons on EchoTools to link to my social media platforms. If a user has the specific app already installed (ie, Facebook or Instagram), my profile will launch within that app. Otherwise, they will be routed to the web version of my profile.

There are plenty of resources showing how to use XCode to add a UIButton in your project. You’ll then need to add an IBAction associated with that UIButton in a Swift file. Replace “yourUSERNAME” with your relevant username for each social media scheme below.


INSTAGRAM

    @IBAction func igButtonClick(_ sender: Any)
    {
            let appURL = URL(string: "instagram://user?username=yourUSERNAME")!
            let application = UIApplication.shared
            
            if application.canOpenURL(appURL)
            {
                application.open(appURL)
            }
            else
            {
                let webURL = URL(string: "https://instagram.com/yourUSERNAME")!
                application.open(webURL)
            }
    }

FACEBOOK

    @IBAction func fbButtonClick(_ sender: Any)
    {
        let appURL = URL(string: "fb://profile/yourUSERNAME")!
        let application = UIApplication.shared
        
        if application.canOpenURL(appURL)
        {
            application.open(appURL)
        }
        else
        {
            let webURL = URL(string: "https://www.facebook.com/yourUSERNAME")!
            application.open(webURL)
        }
    }

LINKEDIN

    @IBAction func linkedinButtonClick(_ sender: Any)
    {
        let appURL = URL(string: "linkedin://profile/yourUSERNAME/")!
        let application = UIApplication.shared
        
        if application.canOpenURL(appURL)
        {
            application.open(appURL)
        }
        else
        {
            let webURL = URL(string: "https://www.linkedin.com/in/yourUSERNAME/")!
            application.open(webURL)
        }
    }

TWITTER

    @IBAction func twitterButtonClicked(_ sender: Any)
    {
        let appURL = URL(string: "twitter://user?screen_name=yourUSERNAME")!
        let application = UIApplication.shared
        
        if application.canOpenURL(appURL)
        {
            application.open(appURL)
        }
        else
        {
            let webURL = URL(string: "https://twitter.com/yourUSERNAME")!
            application.open(webURL)
        }
    }

Let me know if this helps! 🙂

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles