We present two transits of the hot-Jupiter exoplanet XO-2b using the Gran Telescopio Canarias (GTC). The time series observations were performed using long-slit spectroscopy of XO-2 and a nearby reference star with the Optical System for Imaging and low Resolution Integrated Spectroscopy (OSIRIS) instrument, enabling differential spectrophotometric transit light curves capable of measuring the exoplanet's transmission spectrum. Two optical low-resolution grisms were used to cover the optical wavelength range from 3800 to 9300 Å. We find that sub-mmag-level slit losses between the target and reference star prevent full optical transmission spectra from being constructed, limiting our analysis to differential absorption depths over ∼1000 Å regions. Wider long slits or multi-object grism spectroscopy with wide masks will likely prove effective in minimizing the observed slit-loss trends. During both transits, we detect significant absorption in the planetary atmosphere of XO-2b using a 50-Å bandpass centred on the Na i doublet, with absorption depths of Δ(Rpl/R★)2 = 0.049 ± 0.017 per cent using the R500R grism and 0.047 ± 0.011 per cent using the R500B grism (combined 5.2σ significance from both transits). The sodium feature is unresolved in our low-resolution spectra, with detailed modelling also likely ruling out significant line-wing absorption over an ∼800 Å region surrounding the doublet. Combined with narrow-band photometric measurements, XO-2b is the first hot Jupiter with evidence for both sodium and potassium present in the planet's atmosphere.