简体   繁体   English

从iOS应用程序打开Facebook Messenger

[英]Open Facebook Messenger from iOS app

Does anyone know how can I open the Facebook messenger application with a pre-filled text? 有谁知道我该如何打开带有预填充文本的Facebook Messenger应用程序?

For example, to open the messenger app at a specified user, you can write the following: 例如,要以指定用户打开Messenger应用,您可以编写以下内容:

 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: 对于Whats应用程序来说非常简单:

NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:@"whatsapp://send?text=%@", @"String to post here"]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

There is such a function but it exists only in Facebook's SDK. 有这样的功能,但仅存在于Facebook的SDK中。

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

Swift 4 & 5 斯威夫特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!)
                }
            }
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM