Testing launch experience for a Mac app

In iOS it’s quite easy, you just delete the app and all of it’s data is removed as well. But how do you do it in OSX? Well, you should remove:

~/Library/Application Support/<YOUR OSX APP BUNDLE NAME>/

for example, by default should be:

~/Library/Application Support/com.yourcompany.yourapp/

And this is it! I suggest you do a Project -> Clean Build folder (to see this option you will have to press Alt while in the Porject menu) afterwards, just to be sure. 

Questions / comments? I’m at @MarcMasVi 

Marc

 

Scheduled notifications in macOS

Notifications are a very useful addition to macOS applications, when done right can help inmensly. 

Here’s how you create scheduled and immediate notifications. Let’s start with the immediate ones:

func triggerNotification() -> Void {

    let notification = NSUserNotification()

    notification.title = “titleOfNotification”

    notification.informativeText = “whateverTextYou WantToAdd”

    notification.soundName = NSUserNotificationDefaultSoundName

    NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)

}

This will trigger a notification the moment you execute it, if however you would like to schedule a notification for a later date you can do:

func showNotification(atDate: NSDate) -> Void {

    let notification = NSUserNotification()

    notification.title = “It’s MONTH”

    notification.informativeText = “Time to check your finances!”

    notification.soundName = NSUserNotificationDefaultSoundName

    notification.deliveryDate = NSDate().dateByAddingTimeInterval(5.0)

    //notification.deliveryDate = getDateOfNextNotification()

    NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(notification)

}

In this case you would be showing the notification after 5 seconds, more useful though is to plan it for a later date, like you can see in my commented code. The getDateOfNextNotification function simply returns the NSDate where I would like the notification to trigger. 

If you plan to use this, for instance in App Delegate, you may want to check if you have already scheduled notification to avoid duplicates. You can do the following:

func scheduleUpcomingAlertNotification() -> Void {

    let pipeOfNotifications = NSUserNotificationCenter.defaultUserNotificationCenter().scheduledNotifications.count

    if( pipeOfNotifications > 0){

        return

    }else trigger the function

Questions / comments? I’m at @MarcMasVi 

Marc

Copy one or multiple NSTableView rows, Swift

So here’s a simple yet tricky one: you’ve created your NSTableView but now you would like to allow a user to copy to the clipboard one, or a couple of rows. How do you do it?

1. Implement the function copy (func copy(sender: AnyObject?){}), do not confuse with the method for duplicating an object

 2. Get the index set from the tableView selection and retrieve the relevant rows from your dataSource. From there is just a matter of composing the text to copy into the pasteboard. 

Here’s my code structure, implemented in my NSTableViewController:

func copy(sender: AnyObject?){

        

        var textToDisplayInPasteboard = “”

        let indexSet = tableView.selectedRowIndexes

        for (_, rowIndex) in indexSet.enumerate() {

            var iterator: CoreDataOjectType

            iterator=tableDataSource.objectAtIndex(rowIndex)

            textToDisplayInPasteboard = (iterator.name)! 

        }

        let pasteBoard = NSPasteboard.generalPasteboard()

        pasteBoard.clearContents()

        pasteBoard.setString(textToDisplayInPasteboard, forType:NSPasteboardTypeString)

        

    }

This will also automatically enable the Edit -> Copy (Command + C) menu, that would be disabled -grayed out- without this code. 

Questions / comments? I’m at @MarcMasVi 

Marc