Asynchronous Multiple URL Pullers in iOS - ios

I was trying to solve this challenge for fun, http://tleyden.github.io/blog/2014/01/13/objective-c-coding-interview-challenge-urlpuller/, where it asks to asynchronously pull content for an array of urls. Now, I know how to pull content for one url using asynchronous methods of NSURLConnection using initWithRequest, Does this mean I have to create an array of NSURLConnections and requests to pull this off?

yes. you have to create an array of NSURLConnection. But how about already-built network library like:
https://github.com/AFNetworking/AFNetworking
http://allseeing-i.com/asihttprequest/ (Discontinued, but very powerful)
you can also use Cocoapods to import it. that's my favourite way

Related

URLSession: Any way to read a file as it is being downloaded?

The use-case is that I want the user to be able to preview a song hosted at a remote URL. Fine, you say, just use AVPlayer. Yes, but I want to be able to cache the file locally if it is completely downloaded in the course of being previewed. As far as I can tell, there's no way to access the local data that AVPlayer downloads for its streaming playback. Likewise, there's no way to get access to the data being downloaded by URLSession until it is completely downloaded. So, I'd like to find a result using one of these approaches:
Access the data in AVPlayer once it has completed downloading it and save to a file.
Download the data progressively to the cached URL and have AVPlayer play it as enough data becomes available.
Are either of these scenarios possible? Is there some other scenario which will achieve what I am looking to do?
So the solution to this came from the AlamoFire source code. If you use a URLSessionDataTask in the traditional, non-combine way and have a controller conforming to URLSessionDataDelegate, then you can implement the urlSession(_:dataTask:didReceive:) protocol method to receive the data as it arrives, rather than waiting to receive it at completion. This allows you to directly write the data to a file of your choosing that is completely under your app's control.

how to use MTKTextureLoader newTextureWithContentsOfURL to retrieve an image from the web?

Is it possible to use MTKTextureLoader.newTextureWithContentsOfURL to retrieve an image from the web (ex: https://i.stack.imgur.com/EwPfY.jpg?s=48&g=1)? I try but I didn't succeed, so maybe I did something wrong
The -newTextureWithContentsOfURL:... methods are intended for use with file URLs, not Web URLS.
If you want to load an image from the Internet, you should use NSURLSession and NSURLDataTask to download the image contents first (asynchronously), and then use MTKTextureLoader's -newTextureWithData:options:error: method or its asynchronous counterpart to create a MTLTexture.

Get data from server in iOS

I have a script that takes hours and outputs if it is open or closed and I want to run this on my server, and then from my iOS app get if a certain place is open or closed. And I was wondering how I could do this without generating an JSON file and then parsing that as I would to frequently re-generate the file as the things change from open to closed and such. So is there a way I could make a call to the server and just get the data that I want?
Im not entirely sure whether you are asking specifically for an iOS framework for requests and responses or server side. So I figure I will try to answer both. Here are some swift projects you might be interested in. I'm not incredibly familiar with objective c yet but these resources might prove useful.
Alamofire
This will help you send and receive http requests.
Swifty JSON
This will help with JSON parsing
However if your question is more regarding server side issues. You could look into a REST api and that way you could only request certain entities that you are interested in instead of just sending back the entire batch of information with all of your data, and searching through it on the iOS device.
Hopefully this is somewhat helpful.
If you are only planning on GET-ing some content from the web you can use the NSURL class. NSURL *lru = [NSURL URLWithString:#"http:mywebserver.com"] and then create a new string object from the url with [NSString stringWithContentsOfURL:lru], or any data you would like. If you are trying to hold a session or having more control over the request, I would suggest looking into the NSURLSession class.

How to send request to external server in Swift for stats purposes

I am building my first iOS game. Now I would like to know how many people have played the game, and how many games in total have been played. As far as I know Apple does not show these kind of stats, so I figured the best way to do this is to ping a hidden URL to my webserver every time a users plays a round of the game.
The game I am making is written in Swift. I have tried to find some sample code to ping external URL's, but so far I have not found anything.
Could anyone point me in the right direction?
In my opinion the best thing to do is to use google analytics.
It will give you a lot of statistics (like how many times each screen has been seen etc)
https://developers.google.com/analytics/solutions/mobile?hl=fr
But if you want to do it on your own you can easily call an URL on your web service. Personally I love AlamoFire (https://github.com/Alamofire/Alamofire)
there is a lot of sample on their github page.
You need to configure an external API to connect your swift app with a db running on a server.
In swift, to make pretty request to external services, you can use alamofire. It's a pretty library of the same creator of afnetworking. In a simple way, you can make http networking in Swift. Alamofire only runs for IOS 8 or later.
You need to install alamofire, call the library in your viewcontroler (import Alamofire) and make your request. Read the library documentation, it's clear.
If you are simply trying to hit a url on your website, without passing any data, you can do it very simply by.
let url = NSURL(string: "http://mywebserver.com/secret_end_point")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!)
task.resume()

Uploading to Amazon-S3 via AFNetworking

I'm having difficulty uploading files to S3 via AFNetworking. When I attempt to upload, I get callbacks indicating a small percentage of the file is uploaded, then the callbacks cease, and one or two minutes later I get a timeout message.
I'm using AFAmazonS3Client but it seems like a pretty straight forward subclass of AFHTTPClient so it seems unlikely the problem lies there. Since I'm new to both AFNetworking and S3, I'm pretty sure the problem is typing this message :)
I've downloaded the AWS-IOS SDK and was able to post a picture from their sample code without a problem. So I don't think its a misconfiguration of my S3 account.
After struggling a bit with the same problem and a bit of research, I found a solution that seems to work well.
Download the Amazon AWS SDK for iOS and add it to your project:
http://aws.amazon.com/sdkforios/
Then, implement the upload test with a custom NSOperation class as explained here:
Using delegates, operations, and queues
The AmazonServiceRequestDelegate protocol is the key to get progress updates, in a similar fashion to what AFNetworking does.
In particular, this method is what I needed:
-(void)request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;

Resources