I use Multi-Peer Connectivity framework's –
sendResourceAtURL:withName:toPeer:withCompletionHandler:
method to send image.But I find a problem:if the image sending isn't finished and I call MCSession's disconnect method,the app crashes.So I want to know how to safely stop sending image with Multi-Peer Connectivity.
- (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;
}