如何在 Swift3 中打开带参数的 URL

How to open an URL with parameter in Swift3

例如,如果您从我的应用中点击某人的 Instagram 按钮 你要在个人资料

中打开linkinstagram://app/用户名

假设用户将“mark20”作为 Instagram 用户名 你打开linkinstagram://app/mark20/

我想打开 Instagram 并显示 'mark20'。目前它是开放的 Instagram 新闻提要但不开放 'mark20' 个人资料。我想点击按钮打开 Instagram 用户个人资料页面

例如

我该怎么做

func shareToInstagram() {

        //let instagramURL = URL(string: "instagram://app/")
        //not working anymore 
        let instagramURL = URL(string: "instagram://app/mark20/")
        if (UIApplication.shared.canOpenURL(instagramURL! as URL)) {

            UIApplication.shared.open(instagramURL!, options: ["":""], completionHandler: nil)

        } else {
            print(" Instagram isn't installed ")
        }
    }

是的,我解决了问题 path parameter 不再工作,但 query parameter 工作

func shareToInstagram() {

        let instagramURL = URL(string: "instagram://user?username=mark20")

        if (UIApplication.shared.canOpenURL(instagramURL! as URL)) {


            UIApplication.shared.open(instagramURL!, options: ["":""], completionHandler: nil)

           print("miss you so much ")


        } else {
            print(" Instagram isn't installed ")
        }
    }

更多检查这个linkinstagram iPhone Hooks

Swift

 var instagramAppURL = URL(string: "instagram://user?username=USERNAME")
        if UIApplication.shared.canOpenURL(instagramAppURL!) {
            UIApplication.shared.openURL(instagramAppURL!)
        }

对象 C

NSURL *instagramAppURL = [NSURL URLWithString:@"instagram://user?username=USERNAME"];
if ([[UIApplication sharedApplication] canOpenURL:instagramAppURL]) {
    [[UIApplication sharedApplication] openURL:instagramAppURL];
}

首先,您必须修改 Info.plist 以使用 LSApplicationQueriesSchemes 列出 instagram 和 facebook。只需打开 Info.plist 作为源代码,然后粘贴:

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

更多详情和更多方式请参考下方link https://www.instagram.com/developer/mobile-sharing/iphone-hooks/