简体   繁体   中英

Send mail with attachments from an app built with Corona on iOS 10.2.1

I need to send an email with attachments from my app built with Corona SDK on an iOS device with 10.2.1. I had at one time built a Corona app that had the ability to send an email with attachments and it worked just fine. Now, as of iOS 10.2.1, it doesn't seem to work and I can't tell if it's because I'm writing the code wrong or if the native.showPopup is not compatible with iOS 10.2.1.

I am using Corona build 2017.3059.

Also, I have checked my device (an iPhone 6) with native.canShowPopup( "mail" ) and it indicated that the mail pop up feature is available on the device.

Below is the code I'm using.

local widget = require( "widget" )

-- Function to handle button event
local function sendMail( event )
   if ( "ended" == event.phase ) then
      if ( native.canShowPopup( "mail" ) ) then
         native.showAlert( "Alert!", "Mail IS available on this device", { "OK" } )
         local options =
         {
            to = { "john.doe@somewhere.com", "jane.doe@somewhere.com" },
            cc = { "john.smith@somewhere.com", "jane.smith@somewhere.com" },
            subject = "My High Score",
            isBodyHtml = true,
            body = "<html><body>I scored over <b>9000</b>!!! Can you do better?</body></html>"
         }
         native.showPopup( "mail", options )
      else
         native.showAlert( "Alert!", "Mail NOT available on this device", { "OK" } )
      end
   end
end

-- Create the widget
local mailButton = widget.newButton(
{
   label = "Mail",
   fontSize = 64,
   onRelease = sendMail, -- this is my function to send mail
   labelColor = { default={ 1, 1, 1 } },
   labelAlign = "center",
   emboss = false,
   shape = "roundedRect",
   width = 200,
   height = 100,
   cornerRadius = 13,
   fillColor = { default={1,0,0}, over={0,1,1} },
   strokeColor = { default={1,1,1}, over={1,1,1} },
   strokeWidth = 4
}
)

mailButton.x = display.contentCenterX
mailButton.y = display.contentCenterY

I was able to get this working. There were actually two issues:

1) The email accounts on my device were problematic for some reason.

For some reason, I'm not sure why, I had to delete my email accounts and reinstate them and that seemed to solve the problem. It was a pretty common configuration I feel like, I had two Gmail accounts installed on the Apple iOS Mail app. But deleting them and reinstating them did the trick

2) You can't trigger alert boxes before triggering an email

Before my code would work properly, I had to get rid of the:

native.showAlert( "Alert!", "Mail IS available on this device", { "OK" } )

So the working code is:

local widget = require( "widget" )

-- Function to handle button event
local function sendMail( event )
   if ( "ended" == event.phase ) then
      if ( native.canShowPopup( "mail" ) ) then
         local options =
         {
            to = { "john.doe@somewhere.com", "jane.doe@somewhere.com" },
            cc = { "john.smith@somewhere.com", "jane.smith@somewhere.com" },
            subject = "My High Score",
            isBodyHtml = true,
            body = "<html><body>I scored over <b>9000</b>!!! Can you do better?</body></html>"
         }
         native.showPopup( "mail", options )
      else
         native.showAlert( "Alert!", "Mail NOT available on this device", { "OK" } )
      end
   end
end

-- Create the widget
local mailButton = widget.newButton(
{
   label = "Mail",
   fontSize = 64,
   onRelease = sendMail, -- this is my function to send mail
   labelColor = { default={ 1, 1, 1 } },
   labelAlign = "center",
   emboss = false,
   shape = "roundedRect",
   width = 200,
   height = 100,
   cornerRadius = 13,
   fillColor = { default={1,0,0}, over={0,1,1} },
   strokeColor = { default={1,1,1}, over={1,1,1} },
   strokeWidth = 4
}
)

mailButton.x = display.contentCenterX
mailButton.y = display.contentCenterY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM