Tag Archives: UIApplication

How to open the Pinterest App from your own iOS App

Pinterest_Badge_RedYou can open Pinterest links from your iOS app directly in the Pinterest app if it’s installed. This includes direct links to a user, a user’s board, or a specific pin.

In the following code snippets we’ll do just that, and if the Pinterest App is not installed on the device we’ll open the link in Safari instead. These methods can be hooked up to a UIButton.

Note that for this to work, users need to be logged in to the Pinterest App – otherwise the app opens with a login prompt and not the link we want.

Opening a Pinterest User Profile

Much like on Twitter, users on Pinterest have handles. Mine is versluis2000, and my Pinterest URL is http://www.pinterest.com/versluis2000. Here’s how you open this user via the Pinterest app:

Trying to display your own Pinterest User Profile may crash the Pinterest App – at least that’s what happened in my tests.

Opening a Pinterest Board

Users on Pinterest can pin links to their boards which work like collections or categories. Those boards have slugs that appear after the user handle. I have a board called iOS Projects which has the following URL: http://www.pinterest.com/versluis2000/ios-projects/

Here’s how we can open one directly in the Pinterest App:

Opening a Pinterest Pin

When a user finds a link on the web and pins it, the link will receive a unique numeric ID. Such an ID is independent from the user who pins it. Here’s a pin to a sketch of a Polaroid camera I drew: http://www.pinterest.com/pin/76279787413881109/

Let’s open this pin in the Pinterest App:

How about pinning something directly from my own app?

If you’d like users to pin something directly from your app, take a look at the iOS Pin It SDK – brought to you by the friendly folks at Pinterest. The article also explains the deep linking structure we’re using in the above examples:

Care for a Demo Project? https://github.com/versluis/Pinterest

How to disable touch events in iOS

There comes the time when your app needs to do something, and while it’s doing that you don’t want the user messing with your interface: no switching to another tab, scrolling around, flicking switches or pressing buttons – because perhaps any of those actions would crash your app.

But how do you do that?

It’s actually super easy – if you know how. UIApplication to the rescue:

This will disable all user input for any view that’s currently displayed, application wide. Animating menu items (such as UIActivityIndicators) will remain animating and visible.

When you’re done, simply call the opposite of the above and you’re back in the touch business:

How to open the Google Plus App from your own iOS App

Google+To open a URL inside the Google+ App we need to know the Google+ URI scheme.

Instead of http:// it’s gplus:// – so if your Google+ URL in a web browser is http://plus.google.com/+WpguruTv, then you can open the same URL from your iOS App with gplus://plus.google.com/+WpguruTv

Let’s have a look at how to do this in code:

First we try to open the Google+ App using the gplus:// URI. If that’s unsuccessful we’ll open the standard http:// URI which will automatically bring up Safari.

It’s a good idea to test for network connectivity before attempting to open any URL (for example with the excellent Reachability by Tony Million).

How to react to Local Notifications in your iOS App

Now that we know how to create Local Notifications, let’s see how we can react to them. Say your app wasn’t running, and you wanted to know if it was launched as a result of a notification.

Photo 06-01-2014 13 36 27

In our AppDelegate we are passed a launchOptions dictionary, which will have a UIApplicationLaunchOptionsLocalNotificationKey option that will contain another dictionary we can give our notification. This probably sounds more complicated than it is… basically if this key is nil, then we were not launched as a result of a notification. If it’s not nil, it will contain options from the notification that launched it.

When we create our notification we can pass in a userInfo dictionary like so (see previous step for details):

Note that when the app is already running, the didFinishLaunching method is not called. So if the app was open in the background this approach won’t work.

To find out if your already running app has been brought into the foreground, you can implement the following delegate method in your AppDelegate. This will be called if a notification was received, and will include the same userInfo dictionary as above:

Note that this method will be called every time your app receives a notification, not just when it’s woken from the background.

How to create Local Notifications in your iOS App

Local and Push Notifications are very similar, the only difference is their origin. While Push Notifications are sent to Apple who in turn send them to your device, a Local Notification is created like a mix between an alert view and a timer. To the end user they look the same.

Photo 06-01-2014 13 32 27

To create the we can use an instance of the UILocalNotification class, followed by scheduling the notification:

When we run our app it’s a good ice ago cancel all existing notifications that may be hanging around. Otherwise, you’d have 4x the amount of notifications when you run the app 4 times in a row. Next we create our notification and customise it. You can also set a badge number and define your own sound files.

The alertAction is the button or slider text when shown on the lock screen, and the alertBody the actual message. You can even specify an alertLaunchImage that will be shown when the user hits the action button (like a launch image).

All that remains is to schedule the notification. In our example it will come up once every minute, as specified in the repeatInterval. Choose any NSCalendar unit you like (daily, weekly, hourly, etc).

To bring up a notification right away you can do it like this:

Local notifications are fired even if the app is not running, or if it’s in the background. Users can choose to opt-out of receiving those, and they can choose how notifications from your app will appear (under Settings – Notifications – Your App).

Note that if your app is in the foreground notifications are not displayed (because that would be really annoying).

How to open the Twitter App from your own iOS App

Opening the Twitter App is pretty much the same as opening the Facebook App discussed earlier, just the URL scheme is different.

This is how we open Twitter so people can follow the user (me in this case, versluis). If the call is unsuccessful, we’ll try to open the Twitter page in Safari. And if that doesn’t work, we’ll display a log message:

To find out which other apps you can opened, and to figure out what scheme they follow:

To register a Custom URL Scheme in your own app: