Home > Net >  Swiftui Verify with Twilio
Swiftui Verify with Twilio

Time:10-31

i want to phone number verify my app project.Twilio documantation include UIKit but not include swiftui example,And i m try Swiftui framework but not couldn't create button action.

import UIKit

class StartVerificationViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBOutlet weak var countryCodeField: UITextField!
@IBOutlet weak var phoneNumberField: UITextField!
@IBAction func sendVerification(_ sender: Any) {
    
    // send verification SMS
    if let phoneNumber = phoneNumberField.text, let countryCode = countryCodeField.text {
        VerifyAPI.sendVerificationCode(countryCode, phoneNumber)
    }
  }
}

*** VerifyApi is here

import Foundation
 import SwiftUI

struct VerifyAPI {

static func sendVerificationCode(_ countryCode: String,
                                 _ phoneNumber: String) {
    let parameters = [
        "via": "sms",
        "country_code": countryCode,
        "phone_number": phoneNumber
    ]
    
    RequestHelper.createRequest("start", parameters) {
        json in
        return .success(DataResult(data: json))
       }
     }
   }

CodePudding user response:

This is something that I followed.

 import SwiftUI
 import Alamofire

 struct MobileNumberView: View {
  @State var text = ""
  @State var CountryCode = " 44"
  @State var isEditing: Bool = false
  @State var pushActive = false
  @State private var showsAlert = false
  @State var isSearching = false
  @State var Changedtext = ""
  @StateObject var profileViewGlobals : ProfileViewGlobals
  @State var comingPath:String

var body: some View {
    
    NavigationView{
        
        GeometryReader { geometry in
            
            VStack(spacing: 0){
                Color("Green")
                    .edgesIgnoringSafeArea(.top)
                
                ZStack {
                    Color("Green")
                    
                    VStack {
                        
                        Text("Enter your phone number\nto continue")
                            .font(.title)
                            .fontWeight(.bold)
                            .foregroundColor(Color.white)
                        
                    }
                    .padding(.top , -geometry.size.height/6)
                }
                .frame(height: geometry.size.height/2.5)
                
                ZStack{
                    RoundedRectangle(cornerRadius: 25).foregroundColor(.white)
                        .frame(width: geometry.size.width)
                        .frame(height: geometry.size.height/1.1)
                    
                    VStack {
                        TextField("", text: $text)
                            .padding(.leading, geometry.size.width/4)
                            .keyboardType(.decimalPad)
                            .font(.system(size: 30, weight: .thin, design: .default))
                            .padding()
                            .background(Color(.systemGray5))
                            .accentColor(Color("Green"))
                            .foregroundColor(Color("Grey"))
                            .background(Color("Phone"))
                            .cornerRadius(8)
                            .padding(.horizontal)
                            .shadow(radius:10)
                            .overlay(
                                HStack {
                                    Image("UK")
                                        .resizable()
                                        .frame(width: geometry.size.width/10 , height: geometry.size.height/16 )
                                    Text(" 44")
                                        .font(.system(size: 20, weight: .thin, design: .default))
                                    Spacer()

                                }.padding(.horizontal, 30)
                                .foregroundColor(.gray)
                            )
                            
                        

                    }
                    .padding(.bottom , geometry.size.height/1.6)
                    VStack {
                        Button(action: { if text != "" {
                            
                            if text.prefix(1) != "0" {
                                Changedtext = text
                                RegisterMobile(Telephone_Number: CountryCode   text)
                            }
                            else{
                                Changedtext = text
                                Changedtext.remove(at: Changedtext.startIndex)
                                print("\(Changedtext)")
                                RegisterMobile(Telephone_Number: CountryCode   Changedtext)
                                
                            }
                            
                        } }) {
                            
                            Image(systemName: "arrow.forward.circle.fill")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .foregroundColor(Color("Green"))
                                .frame(width: geometry.size.width/5.9)
                        }
                    }
                    .frame(maxWidth: .infinity)
                    .padding(.bottom , geometry.size.height/4)
                }.frame(width: geometry.size.width, height: geometry.size.height/1.5)
                
            }
            NavigationLink(destination: VerificationCodeView(Telephone_Number: CountryCode   Changedtext,profileViewGlobals:profileViewGlobals,comingPath:comingPath), isActive: self.$pushActive) {
                Text("")
            }.hidden()
            .alert(isPresented: self.$showsAlert) {
                Alert(title: Text("Your number seems to be wrong!"), message: Text("Please check your number and try again."), dismissButton: .default(Text("Got it!")))
            }
            
        }.ignoresSafeArea(.keyboard)
    }
    .navigationViewStyle(StackNavigationViewStyle())
    
}

func RegisterMobile(Telephone_Number : String) -> Void {
    
    let deviceData : Parameters = [
        "mobileNumber": Telephone_Number
    ]
    
    APIClient.RegisterMobile(deviceParameters: deviceData, completion:{(result) in
        switch(result){
        
        case .success(let RegisterData):
            
            print(RegisterData)
            
            if (RegisterData.meta.statusCode == 200) {
                
                self.pushActive = true
                print("Successful")
            }
            else{
                
                print("Fail")
                self.showsAlert = true
            }
            
        case .failure(let error):
            print("ERROR",error)
            self.showsAlert = true
        }
    })
}
}

CodePudding user response:

You can directly use UIViewController to swiftUI.

example:

Struct VerifyKitVW: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<VerifyKitVW>) -> StartVerificationViewController {
        let verifyController = StartVerificationViewController()
        verifyController.view.backgroundColor = UIColor.yellow
        return verifyController
    }

    func updateUIViewController(_ uiViewController: StartVerificationViewController, context: UIViewControllerRepresentableContext<VerifyKitVW>) {
        uiViewController.sendVerification(UIButton())
    }
    
}
  • Related