从IOS App IOS 11及更高版本中发送SendTweet

时间:2019-02-10 22:40:36

标签: ios swift twitter

在IOS 11之前,您可以使用AccountStore来访问Twitter凭据,以使用类似以下代码的内容从应用程序进行鸣叫,但是Apple已弃用此功能。 Twitter也有sunsetted TwitterKit提供了一种解决方法。在2019年,是否有一种方法可以将应用程序中的消息共享给Twitter(当用户在手机上安装了Twitter应用程序时)?例如,Facebook仍然提供了将状态更新发布到Twitter的选项。

func sendTweet(){
    let account = ACAccountStore()
    let accountType = account.accountTypeWithAccountTypeIdentifier(
        ACAccountTypeIdentifierTwitter)
    account.requestAccessToAccountsWithType(accountType, options: nil,completion: {(success: Bool, error: NSError!) -> Void in

        if success {
        let arrayOfAccounts = account.accountsWithAccountType(accountType)

        if arrayOfAccounts.count > 0 {
        let twitterAccount = arrayOfAccounts.last as! ACAccount
        var message = Dictionary<String, AnyObject>()
        message["status"] = self.txtPostDesc.text! //textbox
        let imageData = UIImagePNGRepresentation(self.imagePost)//pickerview add
        let imageString = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
        message["media_ids"] = imageString
        let requestURL = NSURL(string: "https://upload.twitter.com/1/statuses/update_with_media.json")
        let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: SLRequestMethod.POST, URL: requestURL, parameters: message)

        postRequest.addMultipartData(imageData, withName: "media", type: nil, filename: nil)
            postRequest.account = twitterAccount

            postRequest.performRequestWithHandler({(responseData: NSData!,urlResponse: NSHTTPURLResponse!,error: NSError!) -> Void in

            if let err = error {
                print("Error : \(err.localizedDescription)")
            }
                print("Twitter HTTP response \(urlResponse.statusCode)")
                self.alertShow("successful")
                })

            }
        }
    })

}


  [1]: https://github.com/twitter/twitter-kit-ios/wiki/Migrating-from-iOS-Social-Framework

0 个答案:

没有答案