Search code examples
iosswiftxcodeswift3alamofire

Not getting response after using Swift 3 and Alamofire 4


I having problems after changing my code to swift 3 and using Alamofire 4. before using Alamofire 4 and swift 3 my code was working perfect. and when i changed to swift 3 and using Alamofire 4 i had a lot of errors and after fixing them all. I still have one problem which is i am not getting any response !!

My current code using Swift 3 and Alamofire 4

import UIKit
import Alamofire

protocol VideoModelDelegate{
    func dataReady()
}


class VideoModel: NSObject {

    let API_KEY = "xxxxxxxxxxxxxxxxx"
    let UPLOADS_PLAYLIST_ID = "PLc4t1-K0nthvSUhYtc43BiG-Z9LK2mw5D"


    var videoArray = [Video]()

    var delegate: VideoModelDelegate?

    let urladdress = "https://www.googleapis.com/youtube/v3/playlistItems"


    func getFeedVideos() {

        Alamofire.request("https://www.googleapis.com/youtube/v3/playlistItems", method: .get, parameters: ["part":"snippet", "playlistId": UPLOADS_PLAYLIST_ID,"key": API_KEY, "maxResults": "50"], encoding: JSONEncoding.default).responseJSON(completionHandler: { (response) -> Void in

            print("##############################")
            print(response)
            print("##############################")



            if let JSON = response.result.value as? [String: Any] {

                print(JSON)

                var arrayOfVideos = [Video]()

                if let items = JSON["items"] as? [[String : Any]] {

                    for video in items {
                        print(JSON)

                        let videoObj = Video()
                        videoObj.videoId = (video as NSDictionary).value(forKeyPath:"snippet.resourceId.videoId") as! String

                        videoObj.videoTitle = (video as NSDictionary).value(forKeyPath:"snippet.title") as! String

                        videoObj.videoDescription = video["snippet.description"] as! String

                        if video[ "snippet.thumbnails.maxres.url"] != nil {
                            videoObj.videoThumbnailUrl = video["snippet.thumbnails.maxres.url"] as! String
                        }
                        else if video["snippet.thumbnails.hqdefault.url"] != nil{
                            videoObj.videoThumbnailUrl = video["snippet.thumbnails.hqdefault.url"] as! String

                        }
                        else if video["snippet.thumbnails.sddefault.url"] != nil{
                            videoObj.videoThumbnailUrl = video["snippet.thumbnails.sddefault.url"] as! String

                        }
                        else if video["snippet.thumbnails.mqdefault.url"] != nil{
                            videoObj.videoThumbnailUrl = video["snippet.thumbnails.mqdefault.url"] as! String

                        }

                        else if video["snippet.thumbnails.default.url"] != nil{
                            videoObj.videoThumbnailUrl = video["snippet.thumbnails.default.url"] as! String

                        }
                        else{

                        }



                        arrayOfVideos.append(videoObj)


                        if self.delegate != nil {
                            self.delegate!.dataReady()
                        }
                    }
                }
                else{
                    print("NOO")
                }
            }

        })
    }
}

My previous code which was working fine:

import UIKit
import Alamofire

protocol VideoModelDelegate{
    func dataReady()
}


class VideoModel: NSObject {

    let API_KEY = "xxxx"
    let UPLOADS_PLAYLIST_ID = Video().playlistID

    var videoArray = [Video]()

    var delegate:VideoModelDelegate?


    func getFeedVideos(){

        // Fetch the videos dynamically using YouTube Data API
        Alamofire.request(.GET, "https://www.googleapis.com/youtube/v3/playlistItems", parameters: ["part":"snippet", "playlistId":UPLOADS_PLAYLIST_ID, "key":API_KEY, "maxResults":50], encoding: ParameterEncoding.URL, headers: nil).responseJSON { (response) in

            if let JSON = response.result.value {

                var arrayOfVideos = [Video]()
                for video in JSON["items"] as! NSArray{
                    //print(video)

                    // Create video objects off of the JSON response
                    let videoObj = Video()
                    videoObj.videoId = video.valueForKeyPath("snippet.resourceId.videoId") as! String
                    videoObj.videoTitle = video.valueForKeyPath("snippet.title") as! String
                    videoObj.videoDescription = video.valueForKeyPath("snippet.description") as! String


                    if video.valueForKeyPath("snippet.thumbnails.maxres.url") != nil {
                        videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.maxres.url") as! String
                    }
                    else if video.valueForKeyPath("snippet.thumbnails.hqdefault.url") != nil{
                        videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.hqdefault.url") as! String

                    }
                    else if video.valueForKeyPath("snippet.thumbnails.sddefault.url") != nil{
                        videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.sddefault.url") as! String

                    }
                    else if video.valueForKeyPath("snippet.thumbnails.mqdefault.url") != nil{
                        videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.mqdefault.url") as! String

                    }
                    else{
                        videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.default.url") as! String
                    }

                    arrayOfVideos.append(videoObj)

                }

                // when all the video objects have been constructed, assign the array to the VideoModel property
                self.videoArray = arrayOfVideos.reverse()

                // Notify the delegate the data is ready
                if self.delegate != nil{
                    self.delegate!.dataReady()
                }
            }
        }

    }

}

When i try to print the response this what i get in the log:

FAILURE: Error Domain=kCFErrorDomainCFNetwork Code=303 "(null)" UserInfo={NSErrorPeerAddressKey={length = 28, capacity = 28, bytes = 0x1c1e01bb000000002607f8b040090809 ... 0000200a00000000}, _kCFStreamErrorCodeKey=-2201, _kCFStreamErrorDomainKey=4}


Solution

  • you have tried with encoding: JSONEncoding.default.

    encoding method should be URLEncoding.default