Try using a brand new genuine Apple Lightning cable. Some 3rd party cables will charge an iPhone fine but run into trouble transferring data (in my experience).
Also never move the iPhone when it's connected to the Mac and make sure the Lightning port is clean. Wiggling the iPhone can cause a connection break if the connection is compromised