FileTransfer using XmppFrameWork in ios

纵然是瞬间 提交于 2019-11-27 13:45:11

Note: My XEP-0096 implementation is different from your's. Any way you created session so just get that ID It may help you:

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{

    //**************** Handling the file transfer task *****************//
    NSLog(@"---------- Check if this IQ is for a File Transfer ----------");
   NSString *myFileTransferID = YOUR_SESSION_ID_GET_FROM_XEP--0096//[xmppFileTransfer xmppFileTransferResponse:(XMPPIQ *)iq];

    if ([myFileTransferID length]>0) {
        // Get the file size

        NSXMLElement *si = [iq elementForName:@"si"];
        NSXMLElement *file = [si elementForName:@"file"];
        if (file) {

//All are the attribute of file while creating section Id you need to take car for that
            filesize = [file attributeUInt32ValueForName:@"size"];
            received_FileOwner = [iq fromStr];
            received_FileType = [si attributeStringValueForName:@"mime-type"];
            received_FileName = [file attributeStringValueForName:@"name"];
        }
    }
    if([myFileTransferID length] > 0 && [[iq type] isEqualToString:@"result"]) {
        //  Create bytestream socket for File Transfer via XEP-0065
        TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:xmppStream toJID:[iq from] fileTransferSID:myFileTransferID];
        [turnSockets addObject:turnSocket];
        [turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    }
    else if([myFileTransferID length] > 0 && [[iq type] isEqualToString:@"set"]) {

        if ([TURNSocket isNewStartTURNRequest:iq]) {
            NSLog(@"TURN Connectio started:: to establish:: incoming file transfer request..");
            TURNSocket *turnSocket = [[TURNSocket alloc]initWithStream:sender incomingTURNRequest:iq withSessionID:myFileTransferID];
            [turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
            [turnSockets addObject:turnSocket];
        }
    }
}

TurnSocket delegates

- (void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket
{
    NSLog(@"Socket Suceeed Port For File Transfer: %d",socket.localPort);
    DDLogInfo(@"TURN Connection succeeded!");
    DDLogInfo(@"You now have a socket that you can use to send/receive data to/from the other person.");

    if ([imageDataFile length]>0) {  
        // fileTransferData data to write
        [socket fileTransferData withTimeout:200.0f tag:TAG_FILETRANSFER_STREAM];
        //        [turnSockets removeObject:sender];

    }
    else {
        fileTransferData = [[NSMutableData alloc]init];
        [socket readDataToLength:filesize withTimeout:290.0f tag:0];
    }
}

- (void)turnSocketDidFail:(TURNSocket *)sender
{
    DDLogInfo(@"TURN Connection failed!");

    [turnSockets removeObject:sender];
}


- (void)readRecievedData:(NSData*)data withTurnSocket:(TURNSocket *)receiver {

     [fileTransferData appendData:data];
     float progress = (float)[fileTransferData length] / (float)[data length];

     NSLog(@"Progresaa value is: %f",progress);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!