TwitterKit iOS stream problem


#1

Hi Guys,

So I need to integrate streaming for my tweet but i have problem when using the twitterkit ios sdk.
I have used SLRequest for twitter in iOS and it works when preparing the signed URLRequest. But it doesnt work with twitterkit.

Here is the code

import UIKit
import TwitterKit

class ViewController: UIViewController , URLSessionDelegate, URLSessionDataDelegate , NSURLConnectionDelegate, NSURLConnectionDataDelegate {
    
    var dataTask: URLSessionDataTask? = nil
    let mappingQueue = DispatchQueue(label: "com.nakiostudio.background", attributes: [])
    var urlConnection:NSURLConnection? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        
        Twitter.sharedInstance().logIn(completion: { (session, error) in
            if let sess = session {
                print("signed in as \(sess.userName)");
                
                var clientError : NSError?
                
                
                if let userID = Twitter.sharedInstance().sessionStore.session()?.userID {
 
                    let statusesShowEndpoint = "https://stream.twitter.com/1.1/statuses/filter.json?track=Random"
                    let client = TWTRAPIClient(userID: userID)
                    let request = client.urlRequest(withMethod: "POST", url: statusesShowEndpoint, parameters: nil, error: &clientError)
                    self.urlConnection = NSURLConnection.init(request: request, delegate: self)
                    self.urlConnection!.start()
                    
                }
                
            } else {
                print("error: \(error?.localizedDescription)");
            }
        })
        

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        completionHandler(.allow)
    }
    
    func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
        print("did become invalid")
    }
    
    func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        let host = task.originalRequest?.url?.host
        if let serverTrust = challenge.protectionSpace.serverTrust, challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == host {
            completionHandler(.useCredential, URLCredential(trust: serverTrust))
        }
    }
    
    func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
        print("did finish events")
    }
    
    func urlSession(_ session: URLSession,
                    dataTask: URLSessionDataTask,
                    didReceive data: Data){
        print("did receive data")
        let string = String(data:data, encoding: String.Encoding.utf8)
        print(string)
    }
    
    func connection(_ connection: NSURLConnection, didFailWithError error: Error) {
        print("DID FAIL WITH ERROR \(error)")
    }
    
    func connection(_ connection: NSURLConnection, didReceive data: Data) {
        print("DID RECEIVE DATA")
        let string = String(data:data, encoding: String.Encoding.utf8)
        print(string)
    }
}

I will usually get this error

Error Domain=NSURLErrorDomain Code=-1001 “The request timed out.” UserInfo={NSUnderlyingError=0x60000045a5b0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 “The request timed out.” UserInfo={NSErrorFailingURLStringKey=https://stream.twitter.com/1.1/statuses/filter.json, NSErrorFailingURLKey=https://stream.twitter.com/1.1/statuses/filter.json, _kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4, NSLocalizedDescription=The request timed out.}}, NSErrorFailingURLStringKey=https://stream.twitter.com/1.1/statuses/filter.json, NSErrorFailingURLKey=https://stream.twitter.com/1.1/statuses/filter.json, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}