如何使用swift在whatsapp,facebook等社交媒体应用中分享数据

时间:2017-08-10 07:14:09

标签: ios swift

我正在尝试在社交应用中分享主题和消息,如whatsapp,facebook,twitter等,但代码下方不起作用。如果社交应用未安装到用户应用,则应将其重定向到AppStore。请检查我的代码。

static func sendWhatspp(msg:String)
{
    let urlWhats = "whatsapp://send?text=\(msg)"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                UIApplication.shared.openURL(whatsappURL as URL)
            } else {
                AFWrapper.showError("Error!", msg: "WhatsApp is not Installed")
            }
        }
    }
}


static func sendTwitter(msg:String)
{
    let urlWhats = "twitter://send?text=\(msg)"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                UIApplication.shared.openURL(whatsappURL as URL)
            } else {
                AFWrapper.showError("Error!", msg: "Twitter is not Installed")
            }
        }
    }
}

 static func sendFB(msg:String)
{
    let urlWhats = "facebook://send?text=\(msg)"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                UIApplication.shared.openURL(whatsappURL as URL)
            } else {
                AFWrapper.showError("Error!", msg: "Facebook is not Installed")
            }
        }
    }
}

static func sendAll(msg:String)
{
    let urlWhats = "whatsapp://send?text=\(msg)"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                UIApplication.shared.openURL(whatsappURL as URL)
            } else {
                AFWrapper.showError("Error!", msg: "No Share app Installed")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您的sendWhatsapp代码没有问题。您是否在您的Info.plist LSApplicationQueriesSchemes中添加了whatsapp?你需要添加它才能工作。

在Facebook和Twitter上分享。他们是不同的

import Social 

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
    var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)   
    self.presentViewController(fbShare, animated: true, completion: nil)
} else {
    var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)  
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

Swift 4.0:

if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
        var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        self.present(fbShare, animated: true, completion: nil)
    } else {
        let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

对于twitter:只需将SLServiceTypeFacebook替换为SLServiceTypeTwitter。