Search code examples
iosswiftinfobipinfobip-api

How to integrate Infobip sms gateway into my project and how to implement two factor authentication in iOS Swift


Here I have the doubt about how to integrate the Infobip SMS gateway into iOS Swift project. I am new in iOS Swift, so I have no clarity in this concept.

How to check the condition to receive the SMS for the registered mobile number in app like Whatsapp. Here I give what I am tried in my project.

var post:NSString = "username=\(self.appDelegate.username)&password=\(self.appDelegate.password)&gsm=\(self.appDelegate.gsm)&text=\(self.appDelegate.text)&recipients=\(self.appDelegate.recipients)&sender=\(self.appDelegate.sender)"

    NSLog("PostData: %@",post);

    var url1:NSURL = NSURL(string: "http://api.infobip.com/api/v3/sendsms/json")!

    var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

    var postLength:NSString = String( postData.length )

    var request:NSMutableURLRequest = NSMutableURLRequest(URL: url1)
    request.HTTPMethod = "POST"
    request.HTTPBody = postData
    request.setValue(postLength, forHTTPHeaderField: "Content-Length")
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Accept")


    var reponseError: NSError?
    var response: NSURLResponse?

    var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)

    if ( urlData != nil ) {
        let res = response as NSHTTPURLResponse!;

        NSLog("Response code: %ld", res.statusCode);

        if (res.statusCode >= 200 && res.statusCode < 300)
        {
            var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

            NSLog("Response ==> %@", responseData);

            var error: NSError?

            let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary


            let status: NSString? = jsonData.objectForKey("status") as? NSString
            let tmp: NSString? = "success"
            let tmm: NSString? = "already register"

            NSLog("Success: %ld", status!);

            if(status == "success")
            {
                NSLog("Login SUCCESS");

                let VerificationcodeViewController = self.storyboard?.instantiateViewControllerWithIdentifier("verificationcodeViewController") as UIViewController

                self.navigationController?.pushViewController(VerificationcodeViewController, animated: true)
            }else if(status == "already register")
            {
                NSLog("Login failed");
                var alertView:UIAlertView = UIAlertView()
                alertView.title = " Already Exists"
                alertView.message = "Enter a new mail id"
                alertView.delegate = self
                alertView.addButtonWithTitle("OK")
                alertView.show()
            }

            else {

                NSLog("Login failed1");
                var alertView:UIAlertView = UIAlertView()
                alertView.title = " Wrong Email"
                alertView.message = "Invalid Email"
                alertView.delegate = self
                alertView.addButtonWithTitle("OK")
                alertView.show()
            }

        } else {

            NSLog("Login failed2");

        }
    } else {

        NSLog("Login failed3");
    }

Solution

  • I believe that you need 2FA library, instead of pure SMS send!

    Here is details what is 2FA and how it works.