Instagram迅速重定向URL问题

时间:2018-08-07 06:23:36

标签: ios swift instagram-api

我正在将instagram登录到我的iOS应用。我已经成功登录,但是我无法进入我的应用程序,任何人都可以告诉我instagram的有效重定向网址是什么吗?这是我的代码:

代码

  func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        do {
            if let url = request.url {

                print(url)

                if String(describing: url).range(of: "#access_token") != nil {

                    try InstagramEngine.shared().receivedValidAccessToken(from: url)

                    if let accessToken = InstagramEngine.shared().accessToken {
                        print("accessToken: \(accessToken)")
                        //start

                        let URl =  "https://api.instagram.com/v1/users/self/?access_token=\(accessToken)"

                        let parameter = [ "access_token" : accessToken ]

                        //    let URl = "https://api.instagram.com/v1/users/self/"

                      //  appInstance.showLoader()
                        Alamofire.request(URl, method: .get, parameters: parameter, encoding: JSONEncoding.default, headers: nil)
                            .responseJSON { response in
                                //appInstance.hideLoader()
                                print(response.result.value!)

                                if response.result.isSuccess {

                                    let dict = response.result.value!

                                    do {

                                        var result : [String : AnyObject] = [String : AnyObject]()

                                        let data = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.prettyPrinted)

                                        if let json = NSString(data: data, encoding:  String.Encoding.utf8.rawValue){

                                            print(json)
                                            result["result"] = json

                                            if hhSocialDelegate != nil{

                                                hhSocialDelegate.getInstagramLoginResponse(userData : result)
                                                _ =   self.navigationController?.popViewController(animated: true)

                                            }
                                        }

                                    }catch let err as NSError{
                                        print(err.debugDescription)
                                        _ =   self.navigationController?.popViewController(animated: true)
                                    }
                                }
                        }
                    }
                }
            }
        } catch let err as NSError {
            print(err.debugDescription)
            _ =   self.navigationController?.popViewController(animated: true)
        }
        return true
    }

这是我调用instagram api的代码,因此请帮助我解决登录后如何返回我的应用的问题。

2 个答案:

答案 0 :(得分:0)

您需要在应用程序的Info.plist中提及要使用的URI方案。完成后,您可以使用com.myappid.name:/myCallback

这样的URI方案。

Info.plist-> Setting URI Scheme

答案 1 :(得分:0)

尝试一下。例如,您要打开一个Instagram应用程序:

https://stackoverflow.com/a/33932755/4476989 或也阅读此https://github.com/shyambhat/InstagramKit/issues/237

 var instagramHooks = "instagram://user?username=johndoe"
    var instagramUrl = NSURL(string: instagramHooks)
    if UIApplication.sharedApplication().canOpenURL(instagramUrl!)
    {  
        UIApplication.sharedApplication().openURL(instagramUrl!)

     } else {
        //redirect to safari because the user doesn't have Instagram
        UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
    }