繁体   English   中英

从iOS应用程序打开用户twitter个人资料页面

[英]Open a users twitter profile page from iOS app

我正试图从我的应用程序深入链接到本机Twitter应用程序上的用户的Twitter个人资料。 我已经为twitter添加了架构规则和以下代码:

    application.open(  URL(string:"twitter://user?screen_name=BarackObama", options[:],  completionHandler:{(success) in 
        print("Success")
    })

我可以成功打开Twitter应用程序并看到控制台打印“成功”,但我自己的推文是我看到的,而不是用户的推特页面。 此网址架构是否仍然有效?

谢谢

好的,在Swift 4中有两个简单的步骤来实现这一点:

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

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>twitter</string>
</array>

之后,您可以打开Twitter应用程序。 这是一个完整的twitter代码,您可以将此代码链接到您作为操作的任何按钮:

@IBAction func followOnTwitter(sender: AnyObject) {
   let screenName =  "AffordIt_App"
   let appURL = NSURL(string: "twitter://user?screen_name=\(screenName)")!
   let webURL = NSURL(string: "https://twitter.com/\(screenName)")!

   let application = UIApplication.shared

   if application.canOpenURL(appURL as URL) {
        application.open(appURL as URL)
   } else {
        application.open(webURL as URL)
   }
}

将此用于Twitter配置文件共享,Swift 4:

    let screenName =  "NJMINISTRIESINC"
    let appURL = URL(string: "twitter://user?screen_name=\(screenName)")!
    let webURL = URL(string: "https://twitter.com/\(screenName)")!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM