Skip to content

Instantly share code, notes, and snippets.

@surfjedi
Forked from rboyd/ios_detect.coffee
Created September 19, 2012 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surfjedi/3747236 to your computer and use it in GitHub Desktop.
Save surfjedi/3747236 to your computer and use it in GitHub Desktop.
Detect iPhone user agent, offer app download, and javascript redirect to iTunes App Store
OFFER = 'We have an app available in the App Store! Download now?'
ITUNES_URL = '<Your iTunes URL Here>'
createCookie = (name,value,days) ->
if days
date = new Date()
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 365))
expires = '; expires=' + date.toGMTString()
else
expires = ''
document.cookie = name + '=' + value + expires + '; path=/';
readCookie = (name) ->
nameEQ = name + '='
cookie_array = document.cookie.split(';')
for value in cookie_array
value = value.substring(1, value.length) while (value.charAt(0) == ' ')
return value.substring(nameEQ.length, value.length) if value.indexOf(nameEQ) == 0
$ ->
if navigator.userAgent.match(/iPhone/i) or navigator.userAgent.match(/iPod/i) or navigator.userAgent.match(/iPad/i)
if !readCookie('iphoneAdvertised')
createCookie('iphoneAdvertised', true, 7)
res = confirm OFFER
if res
window.location.replace(ITUNES_URL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment