I am transferring photo peer to peer. All things works fine but I am not able to get the photo(file) transfer speed i.g internet speed. Like MB the file is transferred. Second I want to fetch the size of that file.
We are passing photo in data format using MCSession
Due to privacy I cannot add the project code here but I will share the refrence github project that I followed. In project I am passing string and In my case its Photo. All things are same.
I checked in Stackoverflow but not found any accurate answer!
Reference Project Link: https://github.com/YogeshPateliOS/MultipeerConnectivity-.git
Thank You!
TLDR: If you do not want to read the long explanation and get straight to the code, all the ideas below are brought together and can be tested by downloading my public repository which has comments to explain all of this.
So here are my suggestions on how you can achieve this
After reviewing your code, I see that you are using the following function to send data
There is nothing wrong with this and you can indeed convert your UIImage to a Data object and send it this way, it will work.
However I don't think you can track progress and MultiPeer does not give you any delegates to track progress using this method.
Instead you are left with two other options. You could use
Or you could use
I am going to use the first option as that is more straightforward however I think the stream option will give you better results. You can read up on both the options here:
Send Resource (we will implement this one)
Streaming
Step 1
I made some UI updates to your original code by adding a UIImageView to show the transferred image to the advertiser(guest) and a UIButton to start the file transfer from the browser(host)
The UIImageView has an outlet named
@IBOutlet weak var imageView: UIImageView!and an action for the UIButton@IBAction func sendImageAsResource(_ sender: Any)I have also added an image called image2.jpg to the project which we will send from the host to the guest.
Step 2
I also declared few additional variables
Step 3
Set up the host and guest as normal by tapping Guest and Host buttons respectively. After that, tap the
Send image as resourcebutton on the host and the action is implemented as follows for the host:Step 4
This next function is used by the host and the guest. The guest will make side of things will make sense later on, however for the host, in step 3, you have stored a progress object after initiating the file transfer and you have launched a timer to fire every 0.1 seconds, so now implement the timer to query this progress object to display the progress and data transfer status on the host side in the UILabel
Step 5
Handle the receiving of the file on the receiver (guest) side by implementing the following delegate methods
Step 6
Run the code and this is the end result (uploaded to youtube) on the guest side which shows the progress and the file once the transfer is complete and the same progress is shown on the host side as well.
Step 7
I did not implement this but I believe this bit is straight forward:
The file size can be calculated as from the host and it can be sent as a message to the guest on the size to expect
You can can compute the approximate % of a file that has been downloaded by multiplying the progress % by the file size
The speed can be calculated based on the amount of data downloaded / time elapsed so far since the transfer started
I can try to add this code if you feel these calculations are not straightforward.
Update
I have updated the above code samples, github repo and the video to include the final 3 steps as well which gives the final result as follows: