如何安全地停止使用Multipeer Connectivity发送图像

时间:2014-05-13 06:05:51

标签: ios ios7 multipeer-connectivity

我使用多对等连接框架 -

sendResourceAtURL:withName:toPeer:withCompletionHandler:

发送图像的方法。但是我发现了一个问题:如果图像发送没有完成并且我调用MCSession的断开连接方法,应用程序崩溃。所以我想知道如何安全地停止发送图像具有多对等连接。

    - (Transcript *)sendImage:(NSURL *)imageUrl
{
    NSProgress *progress;
    // Loop on connected peers and send the image to each
    for (MCPeerID *peerID in self.MySession.connectedPeers) {
        // Send the resource to the remote peer.  The completion handler block will be called at the end of sending or if any errors occur
        progress = [self.MySession sendResourceAtURL:imageUrl withName:[NSString stringWithFormat:@"%@!@#%@",[imageUrl lastPathComponent],self.MyUserName] toPeer:peerID withCompletionHandler:^(NSError *error) {
            // Implement this block to know when the sending resource transfer completes and if there is an error.
            if (error) {
                NSLog(@"Send resource to peer [%@] completed with Error [%@]", peerID.displayName, error);
            }
            else {
                // Create an image transcript for this received image resource
                Transcript *transcript = [[Transcript alloc] initWithUserName:self.MyUserName imageUrl:imageUrl direction:TRANSCRIPT_DIRECTION_SEND];
                [self.delegate updateTranscript:transcript];
            }
        }];
    }
    // Create an outgoing progress transcript.  For simplicity we will monitor a single NSProgress.  However users can measure each NSProgress returned individually as needed
    Transcript *transcript = [[Transcript alloc] initWithUserName:self.MyUserName imageName:[imageUrl lastPathComponent] progress:progress direction:TRANSCRIPT_DIRECTION_SEND];

    return transcript;
}

1 个答案:

答案 0 :(得分:1)

您必须在图像发送completionHandler上设置一个BOOL变量或通知,而不是每当您获得BOOL值正确或通知时检查,如果它是正确的,则可以调用MCSession的断开连接方法。