Instagram在iOS中登录时发出的问题

时间:2019-06-27 11:02:11

标签: swift instagram

我已经在我的应用中实现了instagram登录,登录工作正常,但登录时出现问题,我必须在Instagram登录按钮上单击两次以获取访问令牌。首先,当我单击它时,在我的Web视图中打开instagram登录页面,并对其进行身份验证之后,导航到我的帐户的主屏幕,在这里我没有访问令牌,现在,当我单击后退按钮并再次按instagram登录按钮时,向我显示访问许可后,它会给我访问令牌,我想知道为什么它第一次不给我令牌吗?这是我的登录代码,

func signInRequest()
{

    self.webView.frame  = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    let authURL = String(format: "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@&DEBUG=True", arguments: [InstagramLogin.authUrl,InstagramLogin.clientId,InstagramLogin.redirectUrl,InstagramLogin.scope])
    let urlRequest =  URLRequest.init(url: URL.init(string: authURL)!)
    self.webView.delegate = self
    self.webView.loadRequest(urlRequest)
}

func checkRequestForCallBackUrl(request: URLRequest) -> Bool
{
    let requestURLString = (request.url?.absoluteString)! as String

    if requestURLString.hasPrefix(InstagramLogin.redirectUrl) {
        print (request)
        let range: Range<String.Index> = requestURLString.range(of: "#access_token=")!

        getAuthToken(authToken: requestURLString.substring(from: range.upperBound))
        return false;
    }
    return true
}


func getAuthToken(authToken : String)
{
    self.authToken = authToken
    print("Instagram authentication token ==", authToken)
    getUserInfo(){(data) in
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
                self.alertView.isHidden = false
            }, completion: nil)

            MBProgressHUD.hide(for: self.view, animated: true)
        }
    }
}
func getUserInfo(completion: @escaping ((_ data: Bool) -> Void)){
    MBProgressHUD.showAdded(to: self.view, animated: true)
    let url = String(format:"https://api.instagram.com/v1/users/self/?access_token=%@",self.authToken)
    var request = URLRequest(url: URL(string: url)!)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    let session = URLSession.shared
    let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
        guard error == nil else {
            completion(false)
            //failure
            return
        }
        // make sure we got data
        guard let responseData = data else {
            completion(false)
            //Error: did not receive data
            return
        }
        do {
            guard let dataResponse = try JSONSerialization.jsonObject(with: responseData, options: [])
                as? [String: AnyObject] else {
                    completion(false)
                    //Error: did not receive data
                    return
            }
            let json = JSON(responseData)
            print(json)
            let userData = json["data"].dictionary
            let name = userData!["full_name"]?.string
            let id = userData!["id"]?.intValue
            self.userId = id!
            self.name = name!
            completion(true)

            // success (dataResponse) dataResponse: contains the Instagram data
        } catch let err {
            completion(false)
            //failure
        }
    })
    task.resume()
}

这是视频,它是如何工作的,我想在第一次尝试时获得访问令牌, enter link description here

0 个答案:

没有答案