ASIHTTPRequest didReceiveData - how to use? - ios

I want to download a file using ASIHTTPRequest, and I want it to behave like a regular direct-to-file download, except I want to encrypt the data as it comes in.
Because I need custom data handling, I need to have my delegate implement request:didReceiveData, and I found out that: "ASIHTTPRequest will not populate responseData or write the response to downloadDestinationPath - you must store the response yourself if you need to."
I can't find any examples of code that implements a custom didReceiveData, I'm not sure how to handle data as it comes in or how to set the download destination path. Is there example I can look at online?
To write data to a destination file I tried to define the function as simply:
-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data {
[data writeToFile:request.downloadDestinationPath atomically:YES];
}
But when the request is complete, the file doesn't exist, verified by:
for (ASIHTTPRequest* req in queue.operations) {
NSLog(#"file at %#", req.downloadDestinationPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:req.downloadDestinationPath]) {
NSLog(#"file exists!");
}
}
If anyone has experience with this library and could point me to a resource, example project, tutorial, or just has a simple answer, I would much appreciate it :)
EDIT: would it be better to use NSURLConnection?

The library comes with a sample project

Related

iOS large file download ~1GB files

For my bachelor thesis I have to implement an eLearning app for iOS. As you can see in my title, I have to download a file in the documents directory that has a size of nearly 1 or 2 GB. I'm very new at iOS-Development, so I would appreciate every tip how i can handle that.
Many thanks in advance!
Its good that you have read about NSURLConnection and ASIHTTP. But as you must be aware ASIHTTP is no longer actively developed/maintained. NSURLConnection is good to understand and learn the basics.
For day to day use, I'd suggest you use AFNetworking. It is simple to use and contains example for understanding how to use it.
For downloading large files it is recommended to write the downloaded data directly to a file rather than storing it in the memory. Using AFNetorking you can do this by,
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:#"download.zip" append:NO];
I have not tried downloading that large data myself, but I'm sure this will be a good start point for you.
AFNetworking: https://github.com/AFNetworking/AFNetworking
API documentation: http://cocoadocs.org/docsets/AFNetworking/2.0.0-RC2/
Happy coding!
Well, since this will very likely take a while to download I would definitely recommend doing this asynchronously using NSURLRequests and NSURLConnections.
My biggest concern though, is the pure size of this download and how this will likely not fit into RAM so this download may not actually be as easy as it seems. You can give it a shot, but what I would recommend is breaking this into multiple files/downloads if possible.
I will post code in a minute when I find code I have previously written.
Added Code
First off, make your class that needs to do the downloading to be an NSURLConnectionDelegate and `NSURLConnectionDataDelegate.
Then you can implement the download request as follows.
NSMutableData *linkData = [[NSMutableData alloc] init];
NSURLRequest *linkRequest = [[NSURLRequest alloc] initWithURL:youURL cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:10.0];
NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:linkRequest delegate:self];
The variable linkData should really be declared in the interface file and then allocated in the implementation, but for brevity I just created it as above.
Then you need the following methods which will be called
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[linkData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
// handle a fail
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
// do what you need when download finishes
}

NSData dataWithContentsOfURL: not returning data for URL that shows in browser

I am making an iOS client for the Stack Exchange API. After a long, drawn out fight I finally managed to implement authentication - which gives me a token I stick into a URL. When the token is valid, the URL looks like this:
https://api.stackexchange.com/2.1/me/associated?key=_____MY_SECRET_KEY______&access_token=_____ACCESS_TOKEN_:)_____
which, when valid, brings me to this JSON in a webpage:
{"items":[{"site_name":"Stack Overflow","site_url":"http://stackoverflow.com","user_id":1849664,"reputation":4220,"account_id":1703573,"creation_date":1353769269,"badge_counts":{"gold":8,"silver":12,"bronze":36},"last_access_date":1375455434,"answer_count":242,"question_count":26},{"site_name":"Server Fault","site_url":"http://serverfault.com","user_id":162327,"reputation":117,"account_id":1703573,"creation_date":1362072291,"badge_counts":{"gold":0,"silver":0,"bronze":9},"last_access_date":1374722580,"answer_count":0,"question_count":4},...
And I get the correct JSON with this code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.stackexchange.com/2.1/me/associated?key=__SECRET_KEY_:)__&access_token=%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"token"]]];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if (jsonData)
{
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
}
When I manually invalidate the token, however, the URL still looks the same, and the page in a browser displays this:
{"error_id":403,"error_name":"access_denied","error_message":"`key` is not valid for passed `access_token`, token not found."}
However, dataWithContentsOfURL: is always nil. Why? What am I doing wrong?
I do get an NSError returned:
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x1dd1e9f0 {NSURL=https://api.stackexchange.com/2.1/me/associated?key=key((&access_token=to‌​ken))}
NSCocoaErrorDomain Code=256 actually means a "file system or file I/O related error whose reason is unknown".
Why you get this error is likely because using dataWithContentsOfURL: will not work with that remote URL - or maybe because of the query params which contain the authentication and the token. Thus, you get the "weird" error.
In general, NSData's dataWithContentsOfURL: should only be use to access local file resources.
In order to solve your problem, you should improve your code in two steps:
1) Use NSURLConnection's convenient class method
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
The block defines what to do with the response data when the request finished. Generally, first check the error parameter, then status code of the response and Content-type - in this order.
2) Replace the former with your own instance method (or one from a third party) with a similar signature but which is much more sophisticated.
Approach #2 enables you to implement and use the following important features
Cancellation
customize authentication in every aspects
load body data to files
process received chunks simultaneous
perform multiple requests in a queue which controls the number of simultaneous connections
and a couple more.
Approach #2 is oftentimes implemented as a subclass of NSOperation and encapsulates a NSURLConnection object (which you need to cancel the connection).
You'll find answers of how to use NSURLConnection in asynchronous mode implementing the delegates. Also, there are third party solutions.
You might find the official documentation invaluable, too:
Using NSURLConnection
For a quick start, you may take a look at my "Simple GET request" class on Gist:
SimpleGetHTTPRequest
This class is NOT based on NSOperation, but it can be modified easily. Consult the official documentation of NSOperation how to make a subclass. This is basically easy, but has a few important things (KVO) which you should get correct.
In my case, adding AppTransportSecuritySettings dictionary into info.plist and setting key AllowArbitraryLoads to true.
Fixed my problem...
Hope it helps new developers.
Your URL might be having a space that's why it returns nil.Just replace the space in URL with a '+' :
stringByReplacingOccurrencesOfString:#" " withString:#"+"
I had this problem when I changed my folder structure. It gave me other NSError codes such as 512 and 4 for any file operations (local and web). The solution was to delete my IOS Simulator folders (Library\Developer\CoreSimulator).
If you are trying to access a remote url via HTTP and using XCode 7 or later you may get a NSCocoaErrorDomain returned from [NSData dataWithContentsOfURL:].
The root cause of this may actually be your "App Transport Security Settings". By default iOS doesn't allow arbitrary loading of URLS.

Xcode - iOS - Simply upload a file to FTP Server

I'm trying to work with FTP Servers.
I have googled around for everything and everything is hard to understand for beginners like me. SimpleFTPSample is hard to understand because it is so much at a time. views, buttons, labels, textflelds, upload, download, request, list, get. Same with BlackRaccoon and everything else.
How to Simply and programily upload "test.txt" to FTP Server: "192.168.1.111" in Xcode (iPhone app) without views or button. Just code that can be in the ViewDidLoad for example.
Maybe something like this?:
NSURL* url = [NSURL URLWithString:#"ftp://username:pw#189.92.32.34"];
CFReadStreamRef stream = CFReadStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url);
stream.delegate= self;
[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[stream open];
but which file?
expand this, or write a new code. i don't know, this is new for me.
Thanks Jonathan
As the writer of Black Raccoon perhaps I'm biased (well, I KNOW I'm biased), but I've attempted to make it as simple and powerful as possible. Let's look at what you want to do, upload a file:
There are four things we need to upload a file - start up code, then four delegate methods: overwrite check, data, success and the fail. Let's assume that you read the entire file into memory (okay for small files less than 2 megs).
First, you need this in your header:
BRRequestUpload *uploadData; // Black Raccoon's upload object
NSData *uploadData; // data we plan to upload
Now for the code part:
- (IBAction) uploadFile :(id)sender
{
//----- get the file path for the item we want to upload
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filepath = [NSString stringWithFormat: #"%#/%#", applicationDocumentsDir, #"file.text"];
//----- read the entire file into memory (small files only)
uploadData = [NSData dataWithContentsOfFile: filepath];
//----- create our upload object
uploadFile = [[BRRequestUpload alloc] initWithDelegate: self];
//----- for anonymous login just leave the username and password nil
uploadFile.path = #"/home/user/myfile.txt";
uploadFile.hostname = #"192.168.1.100";
uploadFile.username = #"yourusername";
uploadFile.password = #"yourpassword";
//----- we start the request
[uploadFile start];
}
The first will be asking your code if you want to overwrite an existing file.
-(BOOL) shouldOverwriteFileWithRequest: (BRRequest *) request
{
//----- set this as appropriate if you want the file to be overwritten
if (request == uploadFile)
{
//----- if uploading a file, we set it to YES (if set to NO, nothing happens)
return YES;
}
}
Next, Black Raccoon will ask you for chunks of data to send. If you have a very large file you NEVER want to try to send it all in one shot - Apple's API will choke and drop data. However, we only have one small chunk so we do this:
- (NSData *) requestDataToSend: (BRRequestUpload *) request
{
//----- returns data object or nil when complete
//----- basically, first time we return the pointer to the NSData.
//----- and BR will upload the data.
//----- Second time we return nil which means no more data to send
NSData *temp = uploadData; // this is a shallow copy of the pointer
uploadData = nil; // next time around, return nil...
return temp;
}
Remember we can ONLY do this for a small file.
Next we have our completion handler (if things worked according to plan):
-(void) requestCompleted: (BRRequest *) request
{
if (request == uploadFile)
{
NSLog(#"%# completed!", request);
uploadFile = nil;
}
}
Lastly we have our failure handler:
-(void) requestFailed:(BRRequest *) request
{
if (request == uploadFile)
{
NSLog(#"%#", request.error.message);
uploadFile = nil;
}
}
It would be WONDERFUL if it was as simple as saying [BRFtpUploadTo: dest srcfile: srcFile destfile: dstFile] but there are many reasons why you SHOULDN'T. Part of it has to do with how Apple has implemented their internal FTP. There are also the issues of blocking, errors, etc. In the end, FTP sounds like it should be trivial but ends up being a bit of a nightmare.
FTP is non-trivial which is why there are so many implementations. I'm not arguing that Black Raccoon is the best, but it is maintained with response to issues being between minutes to a couple of days.
It may look daunting at first, but Black Raccoon is, in my opinion, one of the better FTP libraries. I've spent a lot of time and effort to make it a quality product with excellent response to issues. How do I do this for free? Volume. ;)
Good luck with whatever FTP software you end up with!
Upload path is required when uploading. That is the way FTP works.
The port is the standard FTP port. I know of no way to change this without violating the API. If you figure it out, you stand a good chance of not passing Apple's check.
This code will upload/download any file.
I do not know how to make this work under secure conditions. This uses Apple's FTP protocol. There are other FTP packages that have built this from scratch and are far more intelligent. I would look into them.
BR was designed because I needed simple FTP communication. White Raccoon didn't do this for me because at the time (it has since been modernized).

CocoaHTTPServer on iOS: set up server so user can download NSData as file

I want to make the following webpage using CocoaHTTPServer: there should be a link to download a file, but the source file must be NSData object in memory.
As far as I see in samples, there is an easy way to link some file on iPhone to the hyperlink. Is it possible to "link" NSData?
Would be very thankful for examples.
All you need to do is to return HTTPDataResponse in your HTTPConnection subclass.
If you want an example have a look at the CocoaHTTPServer sample called DynamicServer and replace - httpResponseForMethod: URI: in MyHTTPConnection with something similar to the following:
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
// Before returning you can analyze the passed path argument and select the correct data object to return...
return [[HTTPDataResponse alloc] initWithData:placeYourDataInstanceHere];
}

How to test if a file at an external URL exists without downloading it?

I would like to test if a URL exists without loading any actual data.
I was thinking of initiating a NSURLConnection, and then using these two delegate methods to check the status:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Connection failed ... presumably this is a server issue, and I don't know if the URL exists or not.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
int status = [((NSHTTPURLResponse *)response) statusCode];
[connection cancel];
// Do something with the status
}
Would this be a sensible way? The files I'm testing could potentially be very large, so I want to make sure that the actual file is not downloaded.
Thanks,
Ron
p.s. I'm looking at adding this because Apple forced my App to stop backing up files downloaded from the internet. Instead, I store the files in non-backed up space, and in my backed up database I keep track of the original location of the file. Since the file is intended to be a permanent part of the user's library, I would like to periodically test if a file is not accessible and then move it into backed up space (so it will survive a restore to a new device, for example). I'm very annoyed at Apple for forcing me to make this change as I can imagine customers losing important data.
p.p.s. For some strange reason, these delegate methods no longer appear in the documentation for NSURLConnectionDelegate Protocol. I'm assuming the documentation is just messed up.
Personally, I would send a raw request or modify the head to use HEAD which is a standard HTTP header.
It has the same result type as GET, so if you can check the result headers, see if its 403,404 or 201 then you're good.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

Resources