kevin 1 year ago
parent
commit
0cff124c90
1 changed files with 17 additions and 6 deletions
  1. 17 6
      appbuilder-ios/NexilisLite/NexilisLite/Source/APIS.swift

+ 17 - 6
appbuilder-ios/NexilisLite/NexilisLite/Source/APIS.swift

@@ -701,12 +701,23 @@ public class APIS: NSObject {
         }
     }
     
-    public static func sendWhatsapp(message: String) {
-        var urlString = message
-        var urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
-        var url  = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")
-        if UIApplication.shared.canOpenURL(url! as URL) {
-            UIApplication.shared.open(url! as URL, options: [:]) { (success) in
+    public static func sendSMS(phoneNumber: String, message: String = ""){
+        let formattedNumber = phoneNumber.replacingOccurrences(of: "-", with: "")
+        let urlStringEncoded = message.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
+        let paramMessage = message.isEmpty ? "" : "&body=\(urlStringEncoded!)"
+        let url = URL(string: "sms:\(formattedNumber)\(paramMessage)")
+        if UIApplication.shared.canOpenURL(url!) {
+            UIApplication.shared.open(url!)
+        }
+    }
+    
+    public static func sendWhatsapp(phoneNumber: String, message: String = "") {
+        let formattedNumber = phoneNumber.replacingOccurrences(of: "+", with: "").replacingOccurrences(of: "-", with: "")
+        let urlStringEncoded = message.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
+        let paramMessage = message.isEmpty ? "" : "?text=\(urlStringEncoded!)"
+        let url  = URL(string: "https://wa.me/\(formattedNumber)\(paramMessage)")
+        if UIApplication.shared.canOpenURL(url!) {
+            UIApplication.shared.open(url!, options: [:]) { (success) in
                 if success {
                     //print("WhatsApp accessed successfully")
                 } else {