0

Hello I have an issue passing the device_token to my parameter swift 4 Xcode 9.2

ViewController

 override func viewDidLoad() {
    print("inside viewDidLoad ")
    super.viewDidLoad()
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.viewController = self
}

func loadRequest(for deviceTokenString : String)
{
    let deviceid = UIDevice.current.identifierForVendor!.uuidString
    print("====== device id =======")
    print(deviceid)
    let testURL = URL(string: "http://www.test.com/user?device=ios&deviceid=" + deviceid + "&devicetoken=" + deviceTokenString

    let testURLRequest = URLRequest(url: testURL!)
    print("before load request")
    print(testURL!)
    RJWebview.loadRequest(testURLRequest)
}

AppDelegate -> didRegisterForRemoteNotificationsWithDeviceToken

viewController?.loadRequest(for: deviceTokenString!)

Send the data to my SQL database I'm using this method

 let URL_USER_REGISTER = "http://api"
 let parameters: Parameters=[
            "first_name":textFieldFirstName.text!,
            "last_name":textFieldLastName.text!,
            "username":textFieldUsername.text!,
            "email":textFieldEmail.text!,
            "device_token":device_token,]


Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters).responseJSON
            {
                response in
                //printing response
                print(response)

                //getting the json value from the server
                if let result = response.result.value {

                    //converting it as NSDictionary
                    let jsonData = result as! NSDictionary

                    //displaying the message in label
                    self.labelMessage.text = jsonData.value(forKey: "message") as! String?
                }
        }

And this is the Output

SUCCESS: {
    code = 402;
    errors =     {
        "device_token" =         (
            "validation.required"
        );
    };
    message = error;
}

Should pass the device_token and send it from the parameter method but here I have 2 issues

1 - If i used "device_token":device_token, will give this error Use of unresolved identifier 'device_token'

should to get it from the func loadRequest

2 - If i used the "device_token":loadRequest, will return in the output this

And this is the Output

SUCCESS: {
    code = 200;

        "device_token" ="(FUNCTION)" 
           }

Please, any better way??

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • show your "didRegisterForRemoteNotificationsWithDeviceToken" method code – Dixit Akabari Mar 13 '18 at 11:19
  • You are getting device UDID not device token, secondly pass that device token in Alamofire request Header hopefully it will solve your issue. – Nauman Malik Mar 13 '18 at 11:21
  • @developer91 this'll help you https://stackoverflow.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift-3-0 – Rocky Mar 13 '18 at 12:05
  • How: secondly pass that device token in Alamofire request Header ? do you mean import Alamofire @NaumanMalik and I saw that link thanks @ Rocky but didnot help me okay not UDID I mean device_token and how to pass it to my parameter thanks all – developer91 Mar 13 '18 at 12:38

1 Answers1

0

you can pass your token in Header like this

    var headers : HTTPHeaders = [:]

    let tokenString = "your token"

    if  (tokenString != nil)
    {
    headers = [
        "Authorization": (tokenString)!
      /// in my case i am passing in Authorization
    ]

    }

    Alamofire.request(serviceName,headers: headers).responseJSON { response in

        //debugPrint(response)
  }
Nauman Malik
  • 1,326
  • 9
  • 27