if MFMailComposeViewController.canSendMail() { let data = prepareCSVData() let mailController = MFMailComposeViewController() mailController.mailComposeDelegate = self mailController.setSubject(“Data Export”) mailController.setMessageBody(“Attached is a CSV file containing your data.”, isHTML: false) // Add attachment if let data = data { mailController.addAttachmentData(data, mimeType: “text/comma-separated-values”, fileName: “headaches.csv”) presentViewController(mailController, animated: true, completion: nil) } else { NSLog(“No data”) } }
Category: snippets
Prevent “Array index out of range” Error in Swift
If you don’t know if the array index you are calling exists, here is a nice way to just return nil instead of an error. I grabbed this from StackOverflow but unfortunately lost the link. I am putting it here because I use it once in a while and get tired of searching for it.… Continue reading Prevent “Array index out of range” Error in Swift
Scroll to the Bottom of a Collection View
private func scrollToBottom() { let lastSectionIndex = (collectionView?.numberOfSections())! – 1 let lastItemIndex = (collectionView?.numberOfItemsInSection(lastSectionIndex))! – 1 let indexPath = NSIndexPath(forItem: lastItemIndex, inSection: lastSectionIndex) collectionView!.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: false) }
Create a Confirmation Alert in iOS with Swift
If you want to present an alert with an option to either take an action or cancel: @IBAction func clearImage(sender: UIBarButtonItem) { let alert = UIAlertController(title: “Clear Canvas”, message: “Are you sure you want to clear the canvas?”, preferredStyle: .Alert) let clearAction = UIAlertAction(title: “Clear”, style: .Destructive) { (alert: UIAlertAction!) -> Void in self.canvas.image =… Continue reading Create a Confirmation Alert in iOS with Swift
Open File with TextEdit from Command Line on OSX
open -a TextEdit filename Then go download Notepad from the Mac App Store because TextEdit kind of sucks.
Create Navigation Bar Programmatically in Swift
In your UIPopoverPresentationControllerDelegate: func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? { let presentedViewController = controller.presentedViewController let navigationController = UINavigationController(rootViewController: presentedViewController) let dismissButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: “dismissPopover:”) presentedViewController.navigationItem.rightBarButtonItem = dismissButton return navigationController } func dismissPopover(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: nil) }
Override Application Transport Security in xCode
When you get the error App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file., you can override the security and allow all http urls through by adding this to your info.plist file: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key><true> </true></dict> However, before doing this,… Continue reading Override Application Transport Security in xCode
Get user input in Swift
Here is a helper function that handles user input. You can just plop all this in a file called HelperFunctions.swift. /* This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, by Yong Bakos. */ import Foundation // Wait for the user to type something in the console, and return what // they type as… Continue reading Get user input in Swift
Display formatted date from DatePicker in iOS
If you have an NSDate object from a UIDatePicker and you want to display it formatted: let dateFormatter = NSDateFormatter() dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle myDateLabel.text = dateFormatter.stringFromDate(myDatePicker.date)
Seeder Class Not Found in Laravel 5
Remember whenever you create a new Seeder class, to run composer dump-autoload so the framework can find it!