【问题标题】:How to open an URL with parameter in Swift3如何在 Swift3 中打开带参数的 URL
【发布时间】:2017-06-17 15:22:14
【问题描述】:

例如,如果您从我的应用程序中单击某人的 Instagram 按钮 你会在个人资料中打开链接instagram://app/用户名

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

我想打开显示 'mark20' 的 Instagram。目前它是开放的 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 ")
        }
    }

【问题讨论】:

  • 什么不起作用?
  • 我想打开显示 'mark20' 的 Instagram。目前它是开放的 Instagram 新闻提要,但未显示“mark20”个人资料

标签: ios swift instagram openurl


【解决方案1】:

斯威夫特

 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>

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      相关资源
      最近更新 更多