使用操作按钮快速打开twitter配置文件

时间:2018-02-07 23:30:14

标签: swift twitter

我试图通过点击按钮打开我的Twitter个人资料,当我点击按钮打开Safari时,我想用twitter应用程序打开我怎么能这样做以及我在这里做错了什么?

let twUrl = URL(string: "twitter://user?screen_name=wixvii")!
let twUrlWeb = URL(string: "https://www.twitter.com/wixvii")!
if UIApplication.shared.canOpenURL(twUrl){
   UIApplication.shared.open(twUrl, options: [:],completionHandler: nil) 
}else{
   UIApplication.shared.open(twUrlWeb, options: [:], completionHandler: nil)
}

2 个答案:

答案 0 :(得分:1)

将此命令添加到info.plist中可以解决此问题。

  

LSApplicationQueriesSchemes

     

微博

答案 1 :(得分:1)

1.在 Xcode 或 TextEdit 中添加到 Info.plist 源代码下一个字符串:

<块引用>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
</array>

enter image description here

2.使用此功能在您的 iOS 应用中打开 Twitter:

func twitter() {
 
   let lowerCaseSocialNetworkName = "twitter"
   let socialNetworkDomain = "com"
   let userName = "bandyliuk"
   let appURL = URL(string: "\(lowerCaseSocialNetworkName)://user? screen_name=\(userName)")!
   let application = UIApplication.shared
 
   if application.canOpenURL(appURL) {
      application.open(appURL)
   } else {
      let webURL = URL(string: "https://\(lowerCaseSocialNetworkName).\ (socialNetworkDomain)/\(userName)")!
   application.open(webURL)
   }
 
}