簡體   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