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

前端 未结 8 2425
佛祖请我去吃肉
佛祖请我去吃肉 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 03:59

    Update for Swift 4 and iOS 10+

    OK, there are two easy steps to achieve this in Swift 3:

    First, you have to modify Info.plist to list instagram and facebook with LSApplicationQueriesSchemes. Simply open Info.plist as a Source Code, and paste this:

    LSApplicationQueriesSchemes
    
        instagram
        fb
    
    

    After that, you can open instagram and facebook apps by using instagram:// and fb://. Here is a complete code for instagram and you can do the same for facebook, you can link this code to any button you have as an Action:

    @IBAction func InstagramAction() {
    
        let Username =  "instagram" // Your Instagram Username here
        let appURL = URL(string: "instagram://user?username=\(Username)")!
        let application = UIApplication.shared
    
        if application.canOpenURL(appURL) {
            application.open(appURL)
        } else {
            // if Instagram app is not installed, open URL inside Safari
            let webURL = URL(string: "https://instagram.com/\(Username)")!
            application.open(webURL)
        }
    
    }
    

    For facebook, you can use this code:

    let appURL = URL(string: "fb://profile/\(Username)")!
    

提交回复
热议问题