Before 2016, iMessage was simply the default messaging application for iOS devices including iPhone, iPad, and iPod. iMessage extensions meant not only sending and receiving iMessages, but also includes sharing media files and stickers. Now it is even possible to make payments!

In 2016, iOS 10 was presented, and one of the biggest pieces of news was connected to this iconic chat, which was well-known by that time even among non-Apple users. In iOS 10, applications gained the ability to send and receive messages as Apple released a Messages framework for developers.

As Apple stated, this framework could be used to build an app or to integrate this messaging app into an existing project. This would let users interact with third-party apps without leaving iMessage.

Basically, Apple gave developers two options: either run their apps in iMessage entirely or integrate its functionality into their existing iOS applications.

Sending payments that iMessage provides became one of the most interesting uses of iMessage-based apps.

In this article, however, we’ll talk about setting up FaceTime and iMessage in an existing mobile application. This will allow your users to use FaceTime and iMessage on devices without having to leave your application.

Advantages of Using iMessage in Your App

What is iMessage and what can you gain by adding it to your mobile solution?

The service has become quite advanced since iOS 10. For example, link previews now allow users to watch YouTube videos directly from iMessage, for instance, without needing to open a browser. Stickers and a drawing keyboard are two other new functions that have appeared in this messaging service.

This service also works with Siri so users can text someone or check for any incoming messages without even touching their iPhone or iPad.

In 2017, iOS 11 brought new payment capabilities; it is now possible to send direct payments via Apple Pay.

The last cool thing about the latest version of iMessages apart from its renewed design and new effects, is synchronization with iCloud so that messages are always up to date across devices.

Moreover, iMessage has always been much faster and more convenient than SMS or MMS.

As you can see, it offers many features, so letting your users access them without leaving your app can be a great idea.

iMessage implementation

Now we’ll explain in detail how to integrate iMessage into your existing app.

iMessage is a separate target, so the first step is to add it to your project. Going to File -> New -> Target will open the required window.

 

 

After selecting iMessage Extension and clicking Next, you’ll see a new folder for this target, containing a default view controller and storyboard. We’ll come back to these later.

As iMessage is a separate target, it requires a separate application identifier and provisioning profiles. Log in to your developer account and generate a new App ID for the bundle identifier of a new target.

 

 

When your App ID is ready, go to the Provisioning Profiles section and generate, download, and install Development and Production provisioning profiles for your new App ID.

 

 

Once your provisioning profiles are set up, you can start coding. Your new target will have its own .xccassets file with an app icon placeholder that allows you to set the iMessage app icon the same way you would for any other application. You can set the app icon so you can locate your app in a target’s list more easily. Select the messages target from the targets list and run it.

 

 

The application will now open on device\simulator and you’ll see your application in the list. If your app isn’t there, click the more button to open the application list screen, then click Edit and enable your application.

 

 

Since you haven’t changed anything yet, your app will display the default Hello World screen.

 

 

Our target folder contains MessagesViewController.swift and MainInterface.storyboard. This is actually the screen that will be displayed in the iMessage app. Let’s add Table View and set it up to display some information from our application – for example, a recipes list. But how do we read this list from our main application? App Groups will help. Let’s go back to the Developer Portal once more and select Identifiers -> App Groups and create a new group.

 

 

Double check that App Groups are enabled for both your Application and iMessage extension application identifiers.

 

 

Now inside your project, go to settings. For both targets, select Capabilities, enable App Groups and check the box for the app group you created:

 

 

Now that you’ve added your app group, you can transfer data between the main application and the iMessage extension using NSUserDefaults.

In the main application, save your recipes list:

 


 let recipes = ["Recipe 1", "Recipe 2", "Recipe 3", "Recipe 4", "Recipe 5", "Recipe 6", "Recipe 7"];
    let defaults = UserDefaults.init(suiteName: "group.mydomain.messages")
    defaults?.set(recipes, forKey: "myRecipes")
    defaults?.synchronize()

 

In the iMessage extension, read the saved data:

 

    let defaults = UserDefaults.init(suiteName: "group.mydomain.messages")
    if let savedRecipes = defaults?.array(forKey: "myRecipes") as? [String] {
        self.recipes = savedRecipes
    }

 

Using the above code and a couple UITableViewDataSource callbacks, we’ll get the list of recipes from our main application to display in the iMessage extension:

 

 

Now you can finally start sending some messages from the iMessage extension. Below is some sample code and the resulting UI:

 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        _ = [tableView.deselectRow(at: indexPath, animated: true)]
        
        //Collapse recipes view if it's expanded
        if (self.presentationStyle == .expanded) {
            self.requestPresentationStyle(.compact)
        }
        
        //Layout for message
        let layout = MSMessageTemplateLayout()
        layout.image = UIImage.init(named: "salad.jpeg")
        layout.caption = "\(self.recipes[indexPath.row]) - What a wonderful salad!"
        

        //The message itself
        let message = MSMessage()
        message.layout = layout
        
        //Send message to active conversation
        self.activeConversation?.insert(message, completionHandler: { (error) in
            if (error == nil) {
                print("Message sent")
            } else {
                print("Error!")
            }
        })
    }

 

 

That’s it! Now you know how to implement iMessage in your application. Finally, let’s move to another thing that can make communication within your app more interesting: FaceTime integration.

Advantages of FaceTime

The idea of FaceTime is actually very simple – it’s just a native iOS video chat app that allows video conversations between Apple product owners.

FaceTime has many alternatives that work not only for iOS – Skype, for example – but if you’re building a native iOS app, integrating FaceTime is a good idea.

Different social media platforms and messengers can take advantage of FaceTime integration. Pretty much any application that involves video communication can use FaceTime.

One interesting idea is using FaceTime in a medical app. Nowadays, many people don’t have enough time to go see a doctor, and video consultations with family doctors are becoming a real trend. With FaceTime integrated into your app, you can allow your users to communicate with medical professionals within your application.

Now let’s see how we can make it work.

Advantages of FaceTime

FaceTime calls from your application are pretty simple to implement using FaceTime URL schemes.

There are two types of schemes – one for audio calls and one for video calls. Both schemes can use either a phone number or an email address.

Here’s the FaceTime audio scheme:

 

 
facetime-audio://14085551234
facetime-audio://user@example.com

 

And here’s sample code for an audio call:

 

@IBAction func callAction(_ sender: Any) {
        
        let callUrl = URL.init(string: "facetime-audio://user@example.com")!
        
        if (UIApplication.shared.canOpenURL(callUrl)) {
            UIApplication.shared.open(callUrl, options: [:], completionHandler: nil)
        } else {
            print("Can't make FaceTime Audio call")
        }
    }

 

 

Here’s the FaceTime video scheme:

 

facetime://14085551234
facetime://user@example.com

 

And here’s sample code for a video call:

 

@IBAction func callAction(_ sender: Any) {
        
        let callUrl = URL.init(string: "facetime://14085551234")!
        
        if (UIApplication.shared.canOpenURL(callUrl)) {
            UIApplication.shared.open(callUrl, options: [:], completionHandler: nil)
        } else {
            print("Can't make FaceTime Video call")
        }
    }

 

 

That’s it! Now you know how to integrate and use FaceTime as part of your mobile solution.

If you have any questions on how to make the most out of iMessage in your mobile app or if you want to create an app that works with FaceTime, don’t hesitate to contact
Mobindustry for a free consultation.

 
Article written by:
Pavel Peday

 

Request Callback

Request Callback

+