media.hasSource()

Type Function
Library media.*
Return value Boolean
Revision 2017.3060
Keywords media, camera, photo library
See also media.selectPhoto()
media.capturePhoto()

Overview

Determines if the given media source, such as a camera or photo library, is available on the platform. This function should be called before calling media.selectPhoto() or media.capturePhoto() to determine if that media service is available. Returns true if the media source is available on the platform/device. Returns false if not.

Gotchas

media.hasSource( media.Camera ) returns two booleans. If the first boolean is true, this means that the device has a camera and the app has permission to access it. If the second boolean is true, this means that the device has a camera. With these two booleans, the following logic applies:

iOS

  • For media.hasSource( media.Camera ), the first boolean will only be false if the device does not have a camera, or the user has explicitly denied camera access to the app.

  • iOS will handle requesting permission to access the Camera or Photos the first time the user attempts this through the app.

Syntax

media.hasSource( mediaSource )
mediaSource (required)

Constant. Can be one of the following:

  • media.PhotoLibrary
  • media.Camera
  • media.SavedPhotosAlbum

Examples

Checking for Photo Library
local onComplete = function( event )
    print( "Returned from camera view." )
end

if ( media.hasSource( media.PhotoLibrary ) ) then
    media.selectPhoto( { mediaSource=media.PhotoLibrary, listener=onComplete } )
else
    native.showAlert( "Corona", "This device does not have a camera.", { "OK" } )
end
Checking/Requesting Camera Access
local onComplete = function( event )
    print( "Returned from camera view." )
end

local hasAccessToCamera, hasCamera = media.hasSource( media.Camera )

if ( hasAccessToCamera == true ) then
    -- There is access to the camera
    media.capturePhoto( { listener = onComplete } )
elseif ( hasCamera == true and native.canShowPopup( "requestAppPermission" ) ) then
    -- A camera exists, but there is no access to it!
    native.showPopup( "requestAppPermission", { appPermission="Camera" } )
else
    native.showAlert( "Corona", "This device does not have a camera.", { "OK" } )
end