Understanding iOS Messaging in-app Not Working Properly
=====================================================
When developing an app that requires sending messages to users, it’s not uncommon for developers to encounter issues with messaging in-app. In this post, we’ll delve into the specifics of an iPhone app’s messaging behavior and explore how to troubleshoot common problems.
Introduction to iOS Messaging
iOS provides a built-in API called MFMessageComposeViewController that allows developers to compose and send messages using the native messaging app on the device. This API is part of the UIKit framework, which is included in all Xcode projects for iOS development.
To use this API, you’ll need to import the Contacts/Contacts.h header file, which includes the necessary protocols and functions for working with messages. Once imported, you can create an instance of MFMessageComposeViewController and present it modally to the user.
Creating a MFMessageComposeViewController Instance
Here’s an example of how to create an instance of MFMessageComposeViewController:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Create a new instance of MFMessageComposeViewController
let controller = MFMessageComposeViewController()
// Set the recipients for the message (in this case, an empty array)
controller.recipients = [NSArray()]
// Set the body of the message
var body = "Check out this information: %@"
body = String(format: body, URL(string: self.feedItem["url"] as! String)!)
// Configure the message composition delegate (in this case, the view controller itself)
controller.messageComposeDelegate = self
// Present the modal view controller
self.present(controller, animated: true, completion: nil)
}
}
In this example, we’re creating a new instance of MFMessageComposeViewController and setting its properties as needed. We then configure the message composition delegate to be the view controller itself.
Understanding Message Formatting
When sending messages using MFMessageComposeViewController, there are some key things to keep in mind when formatting your messages:
- The message body should be a string that will be displayed to the user.
- When including links or other URLs, you’ll need to use an
NSURLinstance and format it as a string using the%@placeholder. - If you’re including multiple values (like in our example), you’ll need to use the
NSString -stringWithFormat:method to combine them.
In the provided code snippet, we’re using the %@ placeholder to include the URL of the article being shared. This works because of how the NSString -stringWithFormat: method is used, which allows us to insert the values at runtime into a formatted string.
The Problem with Using %@
Here’s where things get interesting:
let controller = MFMessageComposeViewController()
controller.recipients = [NSArray()]
controller.body = @"Check Out This Informtaion, %@"
[NSURL URLWithString:self.feedItem["url"]]
The problem here is that MFMessageComposeViewController expects its body property to be a string. However, when we do this:
let body = "Check Out This Informtaion, %@"
body = String(format: body, URL(string: self.feedItem["url"] as! String)!)
we’re creating a new instance of NSString by formatting the %@ placeholder with the URL. The issue is that this line will be executed independently of the previous line.
So if we do:
controller.body = @"Check Out This Informtaion, %@"
[NSURL URLWithString:self.feedItem["url"]]
it creates two separate strings: @"Check Out This Informtaion, %" and [NSURL URLWithString:self.feedItem["url"]]. The latter is a valid URL instance but it’s not being used to format the string.
Fixing the Problem
As user2056143 pointed out, you’re missing an NSString -stringWithFormat: around your values. Here’s how we can fix our code snippet:
controller.body = [NSString stringWithFormat:@"Check Out This Informtaion, %@", [NSURL URLWithString:self.feedItem["url"]]]
By using the NSString -stringWithFormat: method, we ensure that the URL is formatted into the string correctly.
Last modified on 2024-03-07