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

依然范特西╮ 提交于 2019-11-26 20:26:32
jregnauld

Take a look at these links, it can help you:

https://instagram.com/developer/mobile-sharing/iphone-hooks/

http://wiki.akosma.com/IPhone_URL_Schemes

Open a facebook link by native Facebook app on iOS

Otherwise, there is a quick example with Instagram for opening a specific profile (nickname: johndoe) here:

var instagramHooks = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!) {  
  UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
  //redirect to safari because the user doesn't have Instagram
  UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}
Ibrahim

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:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>instagram</string>
    <string>fb</string>
</array>

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)")!

In swift 3;

First you should add this on your Info.plist

Than you can use this code;

    let instagramUrl = URL(string: "instagram://app")
    UIApplication.shared.canOpenURL(instagramUrl!)
    UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil)

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)
        }
    }
}

In swift 4:

Just change appURL and webURL :

twitter://user?screen_name=\(screenName)

instagram://user?screen_name=\(screenName)

facebook://user?screen_name=\(screenName)
  • 'openURL' was deprecated in iOS 10.0:
let screenName =  "imrankst1221"
    let appURL = NSURL(string: "instagram://user?screen_name=\(screenName)")!
    let webURL = NSURL(string: "https://twitter.com/\(screenName)")!

    if UIApplication.shared.canOpenURL(appURL as URL) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(appURL as URL)
        }
    } else {
        //redirect to safari because the user doesn't have Instagram
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(webURL as URL)
        }
    }

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!