File Sharing API for PubNub Cocoa Objective-C SDK

Allows users to upload and share files. You can upload any file of up to 5 MB in size. This feature is commonly used in social apps to share images, or in medical apps to share medical records for patients.

When a file is uploaded on a channel, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel receive a file event which contains a file ID, filename, and optional description.

Send file

Upload file / data to specified channel.

Method(s)

- (void)sendFileWithRequest:(PNSendFileRequest *)request
completion:(nullable PNSendFileCompletionBlock)block;
ParameterTypeRequiredDescription
requestPNSendFileRequestYesSend file request with all information about file and where it should be uploaded.
blockPNSendFileCompletionBlockNoSend file file request completion block.

PNSendFileRequest

ParameterTypeRequiredDefaultDescription
fileMessageMetadataNSDictionaryNoNSDictionary with values to be used by PubNub service to filter file messages
cipherKeyNSStringNoFrom client configurationKey to be used to encrypt uploaded data
fileMessageTTLNSUIntegerNo0How long message should be stored in channel's storage. Set to 0 (zero) to store message according to the retention policy you have set on your key.
fileMessageStoreBOOLNoYESWhether published file messages should be stored in the channel's history
messageidNoMessage to be sent along with file to specified channel
filenameNSStringYesName to be used to store uploaded data. This is set by default only if the request is created with requestWithChannel:fileURL:.

Basic Usage

NSURL *localFileURL = ...;
PNSendFileRequest *request = [PNSendFileRequest requestWithChannel:@"channel"
fileURL:localFileURL];

[self.client sendFileWithRequest:request completion:^(PNSendFileStatus *status) {
if (!status.isError) {
/**
* File upload successfully completed.
* Uploaded file information is available here:
* status.data.fileIdentifier is the unique file identifier
* status.data.fileName is the name used to store the file
*/
} else {
/**
* Handle send file error. Check the 'category' property for reasons
show all 22 lines

Response

@interface PNSendFileData : PNServiceData

// Unique identifier assigned to the file during upload
@property (nonatomic, nullable, readonly, strong) NSString *fileIdentifier;

// Time at which the file information message was published.
@property (nonatomic, nullable, readonly, strong) NSNumber *timetoken;

// Name under which the uploaded file is stored
@property (nonatomic, nullable, readonly, strong) NSString *fileName;

/**
* Whether the file uploaded or not.
*
* This property should be used during error handling to identify whether the
show all 28 lines

Other Examples

Upload binary data

NSData *data = [[NSUUID UUID].UUIDString dataUsingEncoding:NSUTF8StringEncoding];
PNSendFileRequest *request = [PNSendFileRequest requestWithChannel:@"channel"
fileName:@"cat_picture.jpg"
data:data];

[self.client sendFileWithRequest:request completion:^(PNSendFileStatus *status) {
if (!status.isError) {
/**
* File upload successfully completed.
* Uploaded file information available here:
* status.data.fileIdentifier - unique file identifier
* status.data.fileName - name which has been used to store file
*/
} else {
/**
show all 23 lines

Upload using stream

NSInputStream *stream = ...;
NSUInteger streamSize = /* size of data in stream */;
PNSendFileRequest *request = [PNSendFileRequest requestWithChannel:@"channel"
fileName:@"image.png"
stream:stream
size:streamSize];

[self.client sendFileWithRequest:request completion:^(PNSendFileStatus *status) {
if (!status.isError) {
/**
* File upload successfully completed.
* Uploaded file information available here:
* status.data.fileIdentifier - unique file identifier
* status.data.fileName - name which has been used to store file
*/
show all 25 lines

Returns

Same as from basic usage.

List channel files

Retrieve list of files uploaded to Channel.

Method(s)

- (void)listFilesWithRequest:(PNListFilesRequest *)request
completion:(PNListFilesCompletionBlock)block;
ParameterTypeRequiredDescription
requestPNListFilesRequestYesList files request with all information which should be used to fetch channel's files list.
blockPNListFilesCompletionBlockYesList files request completion block.

PNListFilesRequest

ParameterTypeRequiredDefaultDescription
nextNSStringNoRandom string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
limitNSUIntegerNo100Number of files to return in response.

Basic Usage

PNListFilesRequest *request = [PNListFilesRequest requestWithChannel:@"channel"];
request.limit = 20;
request.next = ...;

[self.client listFilesWithRequest:request
completion:^(PNListFilesResult *result, PNErrorStatus *status) {
if (!status.isError) {
/**
* Uploaded files list successfully fetched.
* result.data.files - List of uploaded files (information).
* result.data.next - Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
* result.data.count - Total number of files uploaded to channel.
*/
} else {
/**
show all 22 lines

Response

@interface PNListFilesData : PNServiceData

// List of channel files.
@property (nonatomic, nullable, readonly, strong) NSArray<PNFile *> *files;

// Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
@property (nonatomic, nullable, readonly, strong) NSString *next;

// How many files has been returned.
@property (nonatomic, readonly, assign) NSUInteger count;

@end

@interface PNListFilesResult : PNResult

show all 19 lines

Get File Url

Generate URL which can be used to download file from target Channel.

Method(s)

- (nullable NSURL *)downloadURLForFileWithName:(NSString *)name
identifier:(NSString *)identifier
inChannel:(NSString *)channel;
ParameterTypeRequiredDescription
nameNSStringYesName under which uploaded file is stored for channel.
identifierNSStringYesUnique file identifier which has been assigned during file upload.
channelNSStringYesName of channel within which file with name has been uploaded.

Basic Usage

NSURL *url = [self.client downloadURLForFileWithName:@"user_profile.png"
identifier:@"<file-identifier>"
inChannel:@"lobby"];

Returns

URL which can be used to download remote file with specified name and identifier.

Download file

Download file from specified Channel.

Method(s)

- (void)downloadFileWithRequest:(PNDownloadFileRequest *)request
completion:(PNDownloadFileCompletionBlock)block;
ParameterTypeRequiredDescription
requestPNDownloadFileRequestYesDownload file request with information about file which should be downloaded.
blockPNDownloadFileCompletionBlockYesDownload file request completion block.

PNDownloadFileRequest

ParameterTypeRequiredDefaultDescription
cipherKeyNSStringNoFrom client configurationKey which should be used to decrypt downloaded data.
targetURLNSURLNoTemporary locationURL where downloaded file should be stored locally. File downloaded to temporary location will be removed after completion block return.

Basic Usage

PNDownloadFileRequest *request = [PNDownloadFileRequest requestWithChannel:@"lobby"
identifier:@"<file-identifier>"
name:@"user_profile.png"];
request.targetURL = ...;

[self.client downloadFileWithRequest:request
completion:^(PNDownloadFileResult *result, PNErrorStatus *status) {

if (!status.isError) {
/**
* File successfully has been downloaded.
* status.data.location - location where downloaded file can be found
* status.data.temporary - whether file has been downloaded to temporary storage and
* will be removed on completion block return.
*/
show all 24 lines

Response

@interface PNDownloadFileData : PNServiceData

/**
* Whether file is temporary or not.
*
* Temporary file will be removed as soon as completion block will exit. Make sure
* to move temporary files (w/o scheduling task on secondary thread) to persistent
* location.
*/
@property (nonatomic, readonly, assign, getter = isTemporary) BOOL temporary;

// Location where downloaded file can be found.
@property (nonatomic, readonly, nullable, readonly, strong) NSURL *location;

@end
show all 22 lines

Delete file

Delete file from specified Channel.

Method(s)

- (void)deleteFileWithRequest:(PNDeleteFileRequest *)request
completion:(nullable PNDeleteFileCompletionBlock)block;
ParameterTypeRequiredDescription
requestPNDeleteFileRequestYesDelete file request with all information about file for removal.
blockPNDeleteFileCompletionBlockNoDelete file request completion block.

Basic Usage

PNDeleteFileRequest *request = [PNDeleteFileRequest requestWithChannel:@"channel"
identifier:@"<file-identifier>"
name:@"greetings.txt"];

[self.client deleteFileWithRequest:request completion:^(PNAcknowledgmentStatus *status) {
if (!status.isError) {
// File successfully has been deleted.
} else {
/**
* Handle file delete error. Check 'category' property to find out possible issue
* because of which request did fail.
*
* Request can be resent using: [status retry]
*/
}
show all 16 lines

Response

@interface PNErrorData : PNServiceData

// Stringified error information.
@property (nonatomic, readonly, strong) NSString *information;

@end

@interface PNAcknowledgmentStatus : PNErrorStatus

// Whether status object represent error or not.
@property (nonatomic, readonly, assign, getter = isError) BOOL error;

// Additional information related to error status object.
@property (nonatomic, readonly, strong) PNErrorData *errorData;

show all 16 lines

Publish file message

Publish file message from specified Channel.

Method(s)

- (void)publishFileMessageWithRequest:(PNPublishFileMessageRequest *)request
completion:(nullable PNPublishCompletionBlock)block;
ParameterTypeRequiredDescription
requestPNPublishFileMessageRequestYesFile message publish request with all information about uploaded file.
blockPNPublishCompletionBlockNoFile message publish request completion block.

Basic Usage

PNPublishFileMessageRequest *request = [PNPublishFileMessageRequest requestWithChannel:@"channel"
fileIdentifier:@"fileIdentifier"
name:@"fileName"];

[self.client publishFileMessageWithRequest:request completion:^(PNPublishStatus *status) {
if (!status.isError) {
// File message successfully published.
} else {
// Handle file message publish error. Check 'category' property to find out possible
// issue because of which request did fail.
//
// Request can be resent using: [status retry];
}
}];

Response

@interface PNPublishData : PNServiceData

/**
* Service-provided time stamp at which message has been pushed to remote
* data object live feed.
*/
@property (nonatomic, readonly, strong) NSNumber *timetoken;

// Service-provide information about service response message.
@property (nonatomic, readonly, strong) NSString *information;

@end

@interface PNPublishStatus : PNAcknowledgmentStatus

show all 19 lines
Last updated on