iOS 5 Twitter Framework: Tweeting without user input and confirmation (modal view controller)

前端 未结 4 1141
鱼传尺愫
鱼传尺愫 2020-11-27 17:18

Essentially what I want is for the app, once the user has allowed access to their Twitter account, to be able to tweet whatever the user has selected in a UITableView<

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 17:55

    The accepted answer is no longer valid due to several changes. This one works with iOS 10, Swift 3, and version 1.1 of Twitter's API.

    ** UPDATE **

    This answer has been updated as the previous one relied upon a deprecated Twitter endpoint.

    import Social
    import Accounts
    
    func postToTwitter() {
        let accountStore = ACAccountStore()
        let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
    
        accountStore.requestAccessToAccounts(with: accountType, options: nil) { (granted, error) in
            if granted, let accounts = accountStore.accounts(with: accountType) {
                // This will default to the first account if they have more than one
    
                if let account = accounts.first as? ACAccount {
                    let requestURL = URL(string: "https://api.twitter.com/1.1/statuses/update.json")
                    let parameters = ["status" : "Tweet tweet"]
                    guard let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, url: requestURL, parameters: parameters) else { return }
                    request.account = account
                    request.perform(handler: { (data, response, error) in
                        // Check to see if tweet was successful
                    })
                } else {
                    // User does not have an available Twitter account
                }
            }
        }
    }
    

    This is the API that is being used.

提交回复
热议问题