How to open fb and instagram app by tapping on button in Swift

前端 未结 8 2424
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 03:17

How can I open Facebook and Instagram app by tapping on a button in swift? Some apps redirect to the Facebook app and open a specific page. How can I do the sam

相关标签:
8条回答
  • 2020-11-30 04:01

    You actually don't need to use a web and app URL anymore. The web URL will automatically open in the app if the user has it. Instagram or other apps implement this on their end as a Universal Link

    Swift 4

    func openInstagram(instagramHandle: String) {
        guard let url = URL(string: "https://instagram.com/\(instagramHandle)")  else { return }
        if UIApplication.shared.canOpenURL(url) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 04:02

    Based on accepted answer here is the way to do this more elegantly with Swift 4

    UIApplication.tryURL([
            "instagram://user?username=johndoe", // App
            "https://www.instagram.com/johndoe/" // Website if app fails
            ])
    

    And truly remember to add the scheme to allow the app to open. However even if you forget that instagram will open in Safari.

    The tryUrl is an extension similar to presented here: https://stackoverflow.com/a/29376811/704803

    0 讨论(0)
提交回复
热议问题