Book Image

Instant Passbook App Development for iOS How-to

By : Keith D. Moon
Book Image

Instant Passbook App Development for iOS How-to

By: Keith D. Moon

Overview of this book

With iOS 6, Apple introduced the Passbook app as a central digital wallet for all the store cards, coupons, boarding passes, and event tickets that have become a popular feature of apps. The passes in Passbook can be tied to relevant locations and times, providing additional visibility for your brand or service. Instant Passbook App Development for iOS How-to is a step-by-step walkthrough of how to create, customize, deliver, and update a pass for Passbook, the newest and most exciting iOS 6 feature. With sample code and clear instructions you will be guided through the process and helped to avoid the pitfalls. Instant Passbook App Development for iOS How-to helps you understand Apple's Passbook feature, leading you through creating and distributing your first pass. Clear step-by-step instructions, along with sample code and resources, will get you up and running so you can integrate Passbook into your app or service. With this book you will learn how to create, customize, sign, deliver, and update your Passbook pass, with the help of sample code and clear instructions.
Table of Contents (7 chapters)

Delivering your Pass via e-mail (Medium)


Passes can be delivered as an e-mail attachment, allowing the recipient to view the Pass and add it to their Passbook app.

Getting ready

The Pass e-mail creation script, used below, can be downloaded from the following location:

http://passkit.pro/ruby-email-script

How to do it…

  1. Save the following code into a file named send_pass_by_email.rb.

    require 'net/smtp'
    
    # This script accepts the following arguments: recipients name, recipients email address, path to Pass.
    # Example usage: 
    # ruby send_pass_by_email.rb "Peter Brooke" [email protected] ../Pass-Example-Generic/Pass-Example-Generic.pkpass
    
    # Retrieve command line arguments
    recipientName = ARGV[0]
    recipientEmail = ARGV[1]
    passFilePath = ARGV[2]
    
    # Setup template email values
    senderName = "Passbook Example Company"
    senderEmail = "[email protected]" 
    emailSubjectText = "New Employee Pass"
    emailBodyText = "Please find attached your new employee Pass"
    
    # Setup SMTP settings
    smtpDomain = "TO DEFINE. Eg. gmail.com"
    smtpLogin = "TO DEFINE. Eg. [email protected]"
    smtpPassword = "TO DEFINE" 
    
    # Read file and base64 encode
    fileContent = File.read(passFilePath)
    encodedContent = [fileContent].pack("m")
    
    # The is used to separate the MIME parts, it can be anything
    # as long as it does not appear elsewhere in the email text
    boundaryMarker = "SEPARATINGSTRINGNOTFOUNDELSEWHERE"
    
    # Setup the email headers.
    headers =<<EOF
    From: #{senderName} <#{senderEmail}>
    To: #{recipientName} <#{recipientEmail}>
    Subject: #{emailSubjectText}
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary=#{boundaryMarker}
    --#{boundaryMarker}
    EOF
    
    # Setup the email body
    body =<<EOF
    Content-Type: text/plain
    Content-Transfer-Encoding:8bit
    
    #{emailBodyText}
    --#{boundaryMarker}
    EOF
    
    # Setup the Pass attachment with the correct MIME Encoding
    attachment =<<EOF
    Content-Type: application/vnd.apple.pkpass; name=\"#{passFilePath}\"
    Content-Transfer-Encoding:base64
    Content-Disposition: attachment; filename="#{passFilePath}"
    
    #{encodedContent}
    --#{boundaryMarker}--
    EOF
    
    completeEmail = headers + body + attachment
    
    # Send email using your SMTP settings
    smtp = Net::SMTP.new 'smtp.gmail.com', 587
    smtp.enable_starttls
    smtp.start(smtpDomain, smtpLogin, smtpPassword, :login) do
      smtp.send_message(completeEmail, senderEmail, recipientEmail)
    end
  2. Under the section headed # Setup template email values, enter relevant values for the sender name, sender e-mail address, subject, and e-mail body.

  3. Under the section header # Setup SMTP settings, enter the details of the SMTP e-mail server and account details that will be used to send the e-mail. These can be found from the setting of your e-mail client, or you can use a free e-mail service like Gmail.

  4. This Ruby script accepts three arguments, the recipient's name, the recipient's e-mail address and the path to the Pass to be attached. Open the Terminal and send a Pass-enabled e-mail by calling the script with appropriate arguments, as shown in the following example:

    ruby <Path to send_pass_by_email.rb> "Peter Brooke" [email protected] <Path to Pass to attach .pkpass>

    The following screenshot shows what the resulting e-mail will look like on iOS:

    ,

  5. If you sent this e-mail to an e-mail account you have access to, open this e-mail in the Mail app on an iPhone running iOS 6 or Mail on OSX 10.8.2, and you will be given the option to open and view the Pass and add it to Passbook.

How it works…

The script used above is written in Ruby, as the Ruby interpreter is installed by default on OSX.

Sending a Pass as an attachment that will be understood by iOS and OSX, and presented to the user, requires it to have a specific MIME Type specified in the attachment. This MIME type is the following:

application/vnd.apple.pkpass

This script, or something similar could be used to automate the delivery of Passes to a large number of users, through email.