我試圖在社交應用程序中分享主題和消息,如whatsapp,facebook,twitter等,但下面的代碼無法正常工作。如果社交應用程序未安裝到用戶應用程序,則應將其重定向到AppStore。請檢查我的代碼。如何在社交媒體應用程序中分享數據,如使用swift的whatsapp,facebook
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")
}
}
}
}
請確定「不工作」 –
我強烈認爲Twitter和Facebook網址不以「whatsapp://」 –
@MartinR開頭,當我調用該函數「sendWhatspp」時。它對我說「WhatsApp沒有安裝」事件它安裝在我的iphone 6中。 –