I'm fairly new to Swift
programming and developing ios apps. I'm building something that's going to retrieve posts and other information from a Wordpress website using Wordpress API
. I'm using https://mywebsite.com/wp-json/wp/v2/posts
as an endpoint to get the data I need. I'm working with Alamofire
and SwiftyJSON
to pull the data and parse before I display the information in a UICollectionView
.
I'm using parameters to query the API to get me the required data. Essentially filtering the posts at this time. The problem is that I'm always getting the default number (first ten posts) in the response. The query seems to work fine because the website was able to receive the exact query request and is responding as expected. But for whatever reason, I'm not able to see that data in the response that's received inside Xcode. It's defaulting to the first 10 posts as if there were no filters or quires applied. I have no clue what's going on. I've been trying to debug it for a couple of days without any luck. So, I'm posting here to see if anyone has any thoughts for resolving this mystery.
Following is the function I wrote for the same.
func getPosts() {
Alamofire.request(baseURL, parameters: parameters).responseJSON { (response) -> Void in
if((response.result.value) != nil) {
let postsJson = JSON(response.result.value!)
self.posts = [SitePosts]()
//Testing
print(response.request as Any)
print(response.response?.allHeaderFields)
print("Total posts: \(postsJson.arrayValue.count)")
do {
for posts in postsJson.arrayValue {
let post = SitePosts()
let title = posts["title"]["rendered"].stringValue
let content = posts["content"]["rendered"].stringValue
let url = posts["link"].stringValue
let urlToImage = posts["featured_image_link"].string
post.content = content
post.title = title
post.url = url
post.imageUrl = urlToImage
self.posts?.append(post)
}
DispatchQueue.main.async {
self.postsCollectionView.reloadData()
}
}
}
}
}
Following is the response request that was sent to the API
[AnyHashable("wpe-backend"): apache, AnyHashable("Server"): nginx, AnyHashable("x-type"): default, AnyHashable("x-cacheable"): bot, AnyHashable("allow"): GET, AnyHashable("x-cache"): HIT: 2, AnyHashable("Cache-Control"): max-age=10800, must-revalidate, AnyHashable("Content-Encoding"): gzip, AnyHashable("x-cache-group"): bot, AnyHashable("x-pass-why"): , AnyHashable("access-control-expose-headers"): X-WP-Total, X-WP-TotalPages, AnyHashable("x-wp-totalpages"): 8, AnyHashable("Content-Type"): application/json; charset=UTF-8, AnyHashable("x-content-type-options"): nosniff, AnyHashable("Expires"): Thu, 19 Nov 1981 08:52:00 GMT, AnyHashable("access-control-allow-headers"): Authorization, Content-Type, AnyHashable("x-wp-total"): 77, AnyHashable("Date"): Fri, 22 Feb 2019 22:46:42 GMT, AnyHashable("Link"): https://mywebsite.com/wp-json/wp/v2/posts?page=2; rel="next", AnyHashable("x-robots-tag"): noindex]
I'm using Xcode 10.1
, Swift 4.2
, Alamofire (4.8.1)
, SwiftyJSON (4.2.0)
and Wordpress V2 API
.
I've added a breakpoint right after the Alamofire request. Attaching the screenshot of what I see.Screenshot of the breakpoint
I think something is going on with Alamofire package after archiving the project. I tried recreating the project and it worked fine until I archived the project to test on another device. So, I've switched over to urlRequest instead of using Alamofire request. Replacing Alamofire request with the below worked fine for me.
var urlRequest = URLRequest(url: URL(string: "https://mywesite.com/wp-json/wp/v2/posts?per_page=100")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in