Giter VIP home page Giter VIP logo

xmpp-file-transfer-demo's Introduction

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

Developed By

Jonathon Staff

License

Copyright 2014 Jonathon Staff

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

xmpp-file-transfer-demo's People

Contributors

jonstaff avatar

Stargazers

MohsinAli avatar

Watchers

MohsinAli avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.