11

Is there any way to open iMessage apple app from my app? I don't need any composers, the main idea is to open actual iMessage with my extension.

EDITED

I don't want to send any messages; I need only iMessage App to be opened

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100

4 Answers4

17

this can be done as follows (in Swift 2):

let phoneNumber = "958585858"
let text = "Some message"

guard let messageURL = NSURL(string: "sms:\(phoneNumber)&body=\(text)") 
else { return }
if UIApplication.sharedApplication().canOpenURL(messageURL) {
    UIApplication.sharedApplication().openURL(messageURL)
}

and if you only need to open messages app :

guard let messageAppURL = NSURL(string: "sms:")
else { return }
if UIApplication.sharedApplication().canOpenURL(messageAppURL) {
    UIApplication.sharedApplication().openURL(messageAppURL)
}

make sure to add sms: to LSApplicationQueriesSchemes in your Info.plist

Gandalf458
  • 2,139
  • 1
  • 21
  • 36
Hammadzafar
  • 480
  • 1
  • 7
  • 21
  • how can we send group message to an array of recepients using the above idea ? – Sushree Swagatika Jul 26 '17 at 08:29
  • For Swift 3, use the answer from @Rajat – Gandalf458 Apr 23 '18 at 19:08
  • @Hammad I want to open Messages application with a predefined message, but with no phone number provided . Is this how should I format my code? `let message = "some invitation message" guard let messageAppURL = URL(string: "sms: &body=\(message)") else {return}` – bibscy Jul 29 '18 at 14:22
6

With swift3 you can do it like this

if UIApplication.shared.canOpenURL(URL(string:"sms:")!) {
     UIApplication.shared.open(URL(string:"sms:")!, options: [:], completionHandler: nil)
}
Rajat
  • 10,977
  • 3
  • 38
  • 55
1

Here's what will open the Messages app without showing also showing the "send message" composer. I couldn't get the other solutions here to work without showing such a composer, so I figured I'd try to hack it another way:

if let url = URL(string: "messages://"), UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url)
}

Full disclosure: I have only tested this on iOS 14, and I have not tried submitting it to the App Store to see if Apple approves it or not.

Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
0

ViewController.h

NSString *app = @"Messages.app";

ViewController.m

// https://developer.apple.com/documentation/appkit/nsworkspace?language=objc
- (IBAction)openclick:(id)sender {
    NSLog(@"Opening iMessage");
    [[NSWorkspace sharedWorkspace] launchApplication:(NSString *)app];
}
Ray
  • 238
  • 5
  • 8