Open iMessage from my application
Asked Answered
C

4

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

Cupreous answered 16/11, 2016 at 8:59 Comment(3)
"I don't need any composers", why not? I assume that this answer should help youSpringwood
because I need exactly iMessage to access my iMessage extensionCupreous
Did you try MFMessageComposeViewController to open iMessage?Ultra
L
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

Lubra answered 16/11, 2016 at 9:8 Comment(3)
how can we send group message to an array of recepients using the above idea ?Acetylcholine
For Swift 3, use the answer from @RajatMoya
@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}Leveille
B
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)
}
Bowing answered 16/11, 2016 at 9:10 Comment(1)
This works well! I made an edit to only construct URL once.Moya
B
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.

Bronchopneumonia answered 6/7, 2021 at 21:12 Comment(0)
D
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];
}
Dolf answered 4/12, 2018 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.