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

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];
}

Related

How to set file name to NSData before uploading to server

I am currently making an app which converts an NSDictionary into a JSON file, in the intention of uploading it to a server. My issue is, is that I have no idea (let alone control) over what the name of the uploaded file is, how do I change/modify it?
Cheers,
Seb OH
This is something that should be handled by whatever FTP client library you are using.
For instance, FTPManager on GitHub (https://github.com/nkreipke/FTPManager) has the following method:
- (BOOL) uploadData:(NSData*)data withFileName:(NSString *)fileName toServer:(FMServer*)server;
That looks like it will fit your need just fine.

Finding video type from NSData

I am loading an video from a URL provided by a third-party. There is no file extension (or filename for that matter) on the URL (as it is an obscured URL). I can take the data from this (in the form of NSData) and load it into a video player and display it fine.
I want to persist this data to a file. However, I don't know what format the data is in (mp4, wav)? I assume it is mp4 (since it's an video from the web) but is there a programmatic way of finding out for sure? I've looked around StackOverflow and at the documentation and haven't been able to find anything. I just wanted to know the file extension whether it is an image or video.
You should check the content type returned by the server:
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)theResponse
{
NSString* content_type = [[(NSHTTPURLResponse*)theResponse allHeaderFields] valueForKey:#"Content-Type"];
//content_type might be image/jpeg, video/mp4 etc.
}

ios share extension - files from dropbox not loading correctly

When I try to load a shared item, the data that comes back is dropbox's login page - as if I weren't authenticated.
Here is the current method I am using to get the file data:
[itemProvider loadItemForTypeIdentifier:docType options:nil completionHandler:^(NSURL *url, NSError *error) {
//my code
}];
doctype is an appropriate kUTType like kUTTypeImage or kUTTypeText, for example. The mimeType that we write the file with is correct to, per other files. It's the actual content loaded from dropbox (just a login page every time).
I have used other variations of the method (UIImage *, and NSData *) but get the same result for dropbox files.
Our shared extension works fine with files that are downloaded in apps like goodreader or Files. The problem arises when I try to share a file from the dropbox app. It gives me a url that I can put into any browser and it will take me to the file, so the url is not the problem.
Has anyone else faced this?
Here is an example link to a document that does this:
https://www.dropbox.com/s/qxkd1957qf7iq9x/04%20-%20Test%20Document.doc?dl=0
Thank you for your help on this Greg. I found that this worked instead by just changing the url and setting dl=1 like so
From:
https://www.dropbox.com/s/qxkd1957qf7iq9x/04%20-%20Test%20Document.doc?dl=0
To https://www.dropbox.com/s/qxkd1957qf7iq9x/04%20-%20Test%20Document.doc?dl=1

NSData initWithContentsOfURL reading not all data, but only on device

I am banging my head about an issue I have on iOS7 development. I use the following piece of code to load an image from a webserver:
NSData* data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://someServer/someImage.jpg"]];
This works like a charme in simulator, reading exactly the 134185 bytes that the image has. Creating an UIImage from that data works as intended.
Once I test the exact same code on a device (iPad Mini, iOS 7.03), though, it just reads 14920 byte from the same URL. Needless to say that I can't create an UIImage from that data then, creation fails and returns a nil.
The read does not produce any errors (no console output, and also using the signature with the error output param returns nil here). Is there anything I missed around this rather straightforward task? Haven't found anything on the web on this…
Thanks, habitoti
So you don't have any error, and something is downloading. Maybe try to read this response and post here (I guess it is html/text body)?
You can use NSString method:
+ (instancetype)stringWithContentsOfURL:(NSURL )url encoding:(NSStringEncoding)enc error:(NSError *)error;
Can I suggest you use a library like SDWebImage to retrieve your image, it caches it and downloads the images asynchronously.
It also has a category for UIImageView so you can just call [imageView setImageWithURL:]; and it will load the image in when its ready.

ASIHTTPRequest didReceiveData - how to use?

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

Resources