在可可应用程序中使用 xmpp 发送任何文件。是不是可以?

Posted

技术标签:

【中文标题】在可可应用程序中使用 xmpp 发送任何文件。是不是可以?【英文标题】:Send any file using xmpp in cocoa application. Is it possible?在可可应用程序中使用 xmpp 发送任何文件。是否可以? 【发布时间】:2014-10-17 08:04:34 【问题描述】:

在我的聊天应用程序中,我无法在聊天时发送任何图像或文件。我尝试的是---

方法一……

NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:@"Send Image Testing"];

NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:[jid full]];
[message addAttributeWithName:@"from" stringValue:[[xmppStream myJID] full]];
[message addChild:body];

NSImage *img = [NSImage imageNamed:@"loginLogo.png"];
NSData *imageData = [img TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];

NSData *data = [imageRep representationUsingType:NSJPEGFileType properties:nil];
NSString *imgStr = [NSString encodeBase64WithData:data];

NSXMLElement *ImgAttachement = [NSXMLElement elementWithName:@"attachment"];
[ImgAttachement setStringValue:imgStr];
[message addChild:ImgAttachement];

[xmppStream sendElement:message];

我在“消息”xmlElement 中添加了一个名为“附件”的“xmlElement”。 “附件”的字符串值是以“Base64”格式编码的 ImageDataString。但是此代码仅将文本发送到另一端(而不是图像)。

不知道失败的原因,可能是我应该发送图像的 NSImage 或服务器链接来代替图像数据。

方法2...

我还尝试了“XMPPOutgoingFileTransfer”类,代码如下。

[_fileTransfer sendData:decodedData 
                       named:@"hello"
                 toRecipient:[XMPPJID jidWithString:@"MYUSERNAME@chat.facebook.com/RESOURCENAME"]
                 description:@"Baal's Soulstone, obviously."
                       error:&err])

但每次都会出现同样的错误 - Error Domain=XMPPOutgoingFileTransferErrorDomain Code=-1 "Unable to send SI offer; the recipient doesn't have the required features."

如果有任何想法,请帮忙 提前致谢

【问题讨论】:

你的问题我们还不清楚,这段代码到底发生了什么错误,是的,是的,是的,是的,是别的什么??? @Tirth 当我编辑我的问题时,我可以使用上述方法发送文本,但问题是 ----- 没有发送图像。如果您有任何想法,请提供帮助 【参考方案1】:

我是这样工作的——

    在setupStrem方法里面,这样设置传入端-

    xmppIncomingFileTransfer = [[XMPPIncomingFileTransfer alloc] init];
    xmppIncomingFileTransfer.disableIBB = NO;
    xmppIncomingFileTransfer.disableSOCKS5 = NO;
    [xmppIncomingFileTransfer   activate:xmppStream];
    
    [xmppIncomingFileTransfer addDelegate:self delegateQueue:dispatch_get_main_queue()];
    

    实现传入的结束委托方法-

    - (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);
    
    

传入的文件将被写入文档目录,完成后您可以更新 UI。

    在发送方-

    if (!_fileTransfer) 
            _fileTransfer = [[XMPPOutgoingFileTransfer alloc] initWithDispatchQueue:dispatch_get_main_queue()];
    
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
            [_fileTransfer activate:appDelegate.xmppStream];
            _fileTransfer.disableIBB = NO;
            _fileTransfer.disableSOCKS5 = NO;
    
            [_fileTransfer addDelegate:self delegateQueue:dispatch_get_main_queue()];
        
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,  NSUserDomainMask, YES);
        NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:filename];
        NSData *data = [NSData dataWithContentsOfFile:fullPath];
    
        NSError *err;
        if (![_fileTransfer sendData:data  named:filename  toRecipient:[XMPPJID jidWithString:self.contact.primaryResource.jidStr] description:@"Baal's Soulstone, obviously."  error:&err]) 
            DDLogInfo(@"You messed something up: %@", err);
        
    

    实现传出委托方法 -

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

注意disableIBBdisableSOCKS5 两端应该匹配。 如果问题依然存在,去XMPPOutgoingFileTransfer.m再去方法-

 - (void)handleRecipientDiscoInfoQueryIQ:(XMPPIQ *)iq withInfo:(XMPPBasicTrackingInfo *)info

然后在这一行放置一个 NSLOG/Breakpoint - hasSOCKS5 = hasSI && hasFT && hasSOCKS5; hasIBB = hasSI && hasFT && hasIBB;

发送文件时,这两个值都应为 TRUE。检查哪个是 FALSE(导致错误),您将了解为什么传入端立即发送 FALSE。尝试解决这个问题。

【讨论】:

我阅读了您的答案,但在 xmpp 演示中找不到 setupStrem 方法

以上是关于在可可应用程序中使用 xmpp 发送任何文件。是不是可以?的主要内容,如果未能解决你的问题,请参考以下文章

使用 XMPP 框架在 Objective C 中传输文件

我应该为任何聊天应用考虑 XMPP

如何在 somessaging xmpp 框架中发送图像

消息未通过 XMPP 发送

在 Python 中使用访问令牌通过 XMPP 发送 Facebook 消息

为啥我的 XMPP 客户端重新发送消息?