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

时间:2017-06-17 04:45:49

标签: ios swift instagram openurl

如果你从我的应用程序点击某人的Instagram按钮上的例子  您将在个人资料

中打开链接instagram://app/用户名

假设用户将“mark20”作为Instagram用户名  您打开链接instagram://app/mark20/

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

例如

enter image description here

我该怎么做

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

2 个答案:

答案 0 :(得分:2)

斯威夫特

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

Obj 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>

有关详细信息和更多方式,请参阅以下链接 https://www.instagram.com/developer/mobile-sharing/iphone-hooks/

答案 1 :(得分:1)

是的,我发现问题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 ")
        }
    }

更多查看此链接instagram iPhone Hooks