Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Facebook Messenger from iOS app

Does anyone know how can I open the Facebook messenger application with a pre-filled text?

For example, to open the messenger app at a specified user, you can write the following:

 NSURL *fbURL = [NSURL URLWithString:@"fb-messenger://user-thread/USER_ID"]; 
if ([[UIApplication sharedApplication] canOpenURL: fbURL]) {
    [[UIApplication sharedApplication] openURL: fbURL];
}

For Whats app is very easy:

NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:@"whatsapp://send?text=%@", @"String to post here"]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}
like image 288
Denisa Toderean Avatar asked Jul 30 '14 14:07

Denisa Toderean


2 Answers

There is such a function but it exists only in Facebook's SDK.

Look at this - https://developers.facebook.com/docs/ios/share#message-dialog

like image 142
Sergey Grischyov Avatar answered Nov 04 '22 06:11

Sergey Grischyov


Swift 4 & 5

private func inviteViaFacebook() {
    let id = "akbarkhanshinwar"
    // If you have User id the use this URL
    let urlWithId = "fb-messenger://user-thread/\(id)"
    // If you don't have User id then use this URL
    let urlWithoutId = "fb-messenger://user-thread"
    if let url = URL(string: urlWithId) {

        // this will open your Messenger App
        UIApplication.shared.open(url, options: [:], completionHandler: {
            (success) in

            if success == false {
                // If Messenger App is not installed then it will open browser.
                // If you have User id the use this URL
                let urlWithIdForBrowser = "https://m.me/\(id)"
                // If you don't have User id then use this URL
                let urlWithoutIdForBrowser = "https://m.me"
                let url = URL(string: urlWithIdForBrowser)
                if UIApplication.shared.canOpenURL(url!) {
                    UIApplication.shared.open(url!)
                }
            }
        })
    }
}
like image 43
Akbar Khan Avatar answered Nov 04 '22 07:11

Akbar Khan