Twitter share not working on device

338 views Asked by At

My twitter share is working on the simulator, but not on the device(iPhone 6).

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
    let url = self.articles?[indexPath.row]["article_url"].string!
    let targetURL = NSURL(string: url!)
    let articleTitle = self.articles?[indexPath.row]["title"].string!
    let authorName = self.articles?[indexPath.row]["author_name"].string!
    let articleID = self.articles?[indexPath.row]["article_id"].int!

    var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
    var twitterMessage = String(authorName!) + " " + String(articleTitle!) + " @yazarappio"

    twitterSheet.setInitialText(twitterMessage)
    twitterSheet.addURL(targetURL)
    self.presentViewController(twitterSheet, animated: true, completion: nil)
    UIPasteboard.generalPasteboard().string = url
}

On the Xcode I don't have a problem with this code, but for running on the device I added this code part:

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
            let message = ["status" : "My first post from iOS 8"]
            let requestURL = NSURL(string: 
        "https://api.twitter.com/1.1/statuses/update.json")
            let postRequest = SLRequest(forServiceType: 
            SLServiceTypeTwitter, 
            requestMethod: SLRequestMethod.POST, 
            URL: requestURL, 
            parameters: message)

            postRequest.account = twitterAccount

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

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

And it worked, I can post from the iOS device. But I couldn't find a way to add targetURL and presentViewController

1

There are 1 answers

1
Sandeep Singh On

Check your device if you added an account for twitter in iPhone settings