-
Notifications
You must be signed in to change notification settings - Fork 30
ACFileTransfer
#Transfer Files Over Bluetooth Today, many apps are Internet-enabled. However, there are particular cases where you may want to transfer information from one device to another without requiring a full Internet implementation. You may want information to be securely sent from one peer to another without involving an entire API.
##How It Works ACFileTransfer accomplishes this by leveraging the GameKit framework and sending data over Bluetooth. The issue with the GameKit framework is that only certain amounts of information can be sent at once. The ACFileTransfer component allows large files to be transferred from one device to another by splitting the information into packets. These packets are sent and can arrive in any order. The ACFileTransfer component reassembles them on the other side.
##Data Types ACFileTransfer can transfer both files, or regular data. It does passes a "filename" which you can use to determine what is done with the data upon receipt. For instance, you may send data with a string as the file name that can be parsed in order to determine the action to be takes with the data. To better understand this, let's look at a demo.
##Sample Code Here is a code block to demonstrate how to set up a file transfer instance and send a file.
ACFileTransfer* fileTransfer = [[ACFileTransfer alloc] initWithDelegate:self];
[fileTransfer sendFile:@"File Path To.data" toPeers:fileTransfer.peers];
This will send the contents of a file to all connected peers with the associated file's filename. You can use the GameKit to prompt the users to choose a particular device or devices to send data to as well. This is beyond the scope of this document.
You can also send just raw data to all peers with this method:
[fileTransfer sendData:[NSKeyedArchiver archivedDataWithRootObject:myObject] toPeers:fileTransfer.peers];
In this example, we have archived an object using NSKeyedArchiver which is a common way to encode an object as NSData for storing as files for persistence.
It's also possible to set a filename to send with the data if desired. This requires a few more lines and looks like this:
fileTransfer.contents = data;
fileTransfer.filename = filename;
[fileTransfer sendToPeers:fileTransfer.peers];
##Receiving Data ACFileTransfer automatically sets up a GKSession that listens for file transfers in your app. These are handled by implementing methods in your delegate object. For instance, if you want to do something with the file once it's received, you just write this method into your delegate:
-(void)fileTransfer:(ACFileTransfer *)fileTransfer receivedFile:(ACFileTransferDetails*)file {
[file.data writeToFile:[path stringByAppendingPathComponent:file.filename] atomically:YES];
}
That's really all there is to it. Now you may think that's pretty weak. The good news is that there are more delegate methods that you can implement to do other things. For instance, you can know once the file has been sent to peers and play a sound:
-(void)fileTransfer:(ACFileTransfer*)fileTransfer sentFile:(NSString *)file {
[self playSound:@"Sharing Done.aiff" atVolume:0.2];
}
You may also want to monitor the progress of a file transfer. You can do this by listening for when the transfer begins, is updated and when it finishes like this:
-(void)fileTransfer:(ACFileTransfer *)fileTransfer beganFile:(ACFileTransferDetails *)file {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
progressBar.value = 0.0f;
}
-(void)fileTransfer:(ACFileTransfer*)fileTransfer receivedPacket:(ACFileTransferDetails*)packet {
progressBar.value = (packet.bytes / packet.total);
}
-(void)fileTransfer:(ACFileTransfer *)fileTransfer receivedFile:(ACFileTransferDetails*)file {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
progressBar.value = 1.0f
// Do something with the received data
}
##Conclusion I hope that has given you some help in implementing a wireless file transfer protocol for your app!