Search code examples
iosswiftxcodealamofireopenurl

Redirect back to app with Alamofire.request


I'm developing an iOS app in Swift 3 (I'm a complete Swift n00b.) I am authenticating against an external app, to get an access token back, but the code is not redirecting back to my app after authenticating against the external app.

AppDelegate.swift

func application(_ application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: Any) -> Bool {
    print(url)
    DispatchQueue.main.async {
        print(url.scheme)
        if (url.scheme == "myapp"){
            print("i just caught a url and i feel fine and the url be says ", url)
        }
    }
    return true
}

in class that handles response from external app:

            Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
                .responseJSON { response in
                    print(response)
                    if((response.result.value) != nil) {
                        let swiftyJsonVar = JSON(response.result.value!)
                        if let resData = swiftyJsonVar[key].string {
                            let autostart_data = swiftyJsonVar[key].string          
... snip ...
                            let url = URL(string: "extapp:///?key=\(data!)&redirect=\(myapp)://key")
                            UIApplication.shared.open(url!, options: [:])
... snip ...
                        }
                    }
            }

Is it possible to redirect back to my app, with Alamofire, once the response is received? I have been researching online with Google for the past two days and have tried out many possible solutions, but have come to nothing so far. I'd appreciate any help!


Solution

  • Problem is not with Alamofire. It's to do with the external app's API and my iOS app not being able to redirect back.