File transfer in Objective C using XMPP Framework
I would like to transfer (send and receive) the file using XMPP Framework in Objective C (from iPhone to i开发者_开发技巧Phone).Is there any tutorial which explains the detail steps to do this.
File Transfer using XMPPFramework
This application is merely a brief demo of how to use the file transfer extension of the XMPPFramework.
A detailed blog post can be found here.
My changes have been merged into the master branch of the XMPPFramework, so you'll want to include the latest version in your project.
Both incoming file transfers and outgoing file transfers are functional within this demo, but I've left a significant amount of error-handling out, so you'll want to include that in your app.
Server Settings
In order for SOCKS5 to work properly, your server must be configured to handle proxy connections. I've only tested this using ejabberd, but these are the mod_proxy65 settings I used:
{mod_proxy65, [
{ip, {0,0,0,0}},
{hostname, "myhostnamehere"},
{port, 7777},
{access, all},
{shaper, c2s_shaper}
]},
If you're unable to get the proxy functioning, you always have the option to set disableSOCKS5 = YES, which will force an IBB transfer instead. This is slower, but it's very widely supported.
Usage
Incoming File Transfers
Instantiate a new XMPPIncomingFileTransfer, activate it, add a delegate, and wait for a file transfer request.
_xmppIncomingFileTransfer = [XMPPIncomingFileTransfer new];
[_xmppIncomingFileTransfer activate:_xmppStream];
[_xmppIncomingFileTransfer addDelegate:self delegateQueue:dispatch_get_main_queue()];
Responding to disco#info queries and the like are handled for you. You'll get a delegate call when an SI offer is received, at which point you can decide whether or not you wish to accept. You can also set autoAcceptFileTransfers = YES and you won't need to call acceptSIOffer: yourself.
- (void)xmppIncomingFileTransfer:(XMPPIncomingFileTransfer *)sender
didFailWithError:(NSError *)error
{
DDLogVerbose(@"%@: Incoming file transfer failed with error: %@", THIS_FILE, error);
}
- (void)xmppIncomingFileTransfer:(XMPPIncomingFileTransfer *)sender
didReceiveSIOffer:(XMPPIQ *)offer
{
DDLogVerbose(@"%@: Incoming file transfer did receive SI offer. Accepting...", THIS_FILE);
[sender acceptSIOffer:offer];
}
- (void)xmppIncomingFileTransfer:(XMPPIncomingFileTransfer *)sender
didSucceedWithData:(NSData *)data
named:(NSString *)name
{
DDLogVerbose(@"%@: Incoming file transfer did succeed.", THIS_FILE);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:name];
[data writeToFile:fullPath options:0 error:nil];
DDLogVerbose(@"%@: Data was written to the path: %@", THIS_FILE, fullPath);
}
Outgoing File Transfers
To start a new outgoing file transfer, simply create an instance of XMPPOutgoingFileTransfer, activate it, add a delegate, and send your data:
_fileTransfer = [[XMPPOutgoingFileTransfer alloc] initWithDispatchQueue:dispatch_get_main_queue()];
[_fileTransfer activate:[self appDelegate].xmppStream];
[_fileTransfer addDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err;
if (![_fileTransfer sendData:data
named:filename
toRecipient:[XMPPJID jidWithString:recipient]
description:@"Baal's Soulstone, obviously."
error:&err]) {
DDLogInfo(@"You messed something up: %@", err);
}
The following delegate calls when get invoked when appropriate:
- (void)xmppOutgoingFileTransfer:(XMPPOutgoingFileTransfer *)sender
didFailWithError:(NSError *)error
{
DDLogInfo(@"Outgoing file transfer failed with error: %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"There was an error sending your file. See the logs."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
- (void)xmppOutgoingFileTransferDidSucceed:(XMPPOutgoingFileTransfer *)sender
{
DDLogVerbose(@"File transfer successful.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success!"
message:@"Your file was sent successfully."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
精彩评论