I am using parse for my chatting application. When I upload files, I keep the url, and send the url to other users, who can then download files via this URL.
Here is my code for uploading files:
+ (void)uploadBlob:(NSData *)blob fileName:(NSString *)fileName type:(NSString *)type {
if ([self private_checkCloudForbidden]) {
return;
}
if (CHOSEN_SERVER_DATABASE == ServersQuickBlox) {
if ([Format isThumbnailWithBlobFileName:fileName]) {
type = @"image";
}
NSString *qbContentType = @"";
if ([type isEqualToString:@"image"]) {
qbContentType = @"image/jpg";
}
[QBContent TUploadFile:blob fileName:fileName contentType:qbContentType isPublic:YES delegate:[CloudDelegate sharedInstance]];
}
else if (CHOSEN_SERVER_DATABASE == ServersParse) {
PFFile *file = [PFFile fileWithName:fileName data:blob];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error){
NSLog(@"Error: %@", [[error userInfo] objectForKey:@"error"]);
} else {
[CloudHandler didUploadBlobWithFileName:file.name blobID:file.url];
}
}];
}
}
and in CloudHandler didUploadBlobWithFileName method, I will save the file's url as the blobID. And here is how I download files via the blobID/url when using QuickBlox:
+ (void)downloadWithBlobId: (NSString *)blobID {
if ([self private_checkCloudForbidden]) {
return;
}
if (CHOSEN_SERVER_DATABASE == ServersQuickBlox) {
[QBContent TDownloadFileWithBlobID:blobID.integerValue delegate:[CloudDelegate sharedInstance]];
}
else if (CHOSEN_SERVER_DATABASE == ServersParse) {
//TODO: HOW TO DOWNLOAD FILES?
}
}
I didn't find the API to download file via URL. (it's a bit weird if parse does provide url or blobID that is useless
EDIT:
The reason I don't use attributes of type 'file':
1. I can't store 'file' on local database. It has to be the blobID or URL.
2. when I send a rich message, I can send along the blobID, so that the receiver does not have to fetch the object first before downloading the binary.
The API supplied with UIKit for retrieving data from a URL is asynchronous, so as to leave the UI responsive. It uses the interface NSURLConnectionDelegate. You should implement that interface in order to receive the data asynchronously. First you initialize the retrieval from the URL like this:
Here the class containing this code is set as the delegate, so this class should be declared as implementing the named interface:
A current implementation of mine looks like this:
This should get you going.
PFFile object does not contain method for downloading because it is built in functionality of iOS SDK. Or you can use AFNetworking as alternative. I think that the simplest way just to download the file is using synchronous constructor of NSData in conjunction with GCD:
You should transfer the PFObject instead of the url. Like this:
This way you can download the file and have the file name as well.