Swift Instagram API 用户名未显示在标签上

问题描述 投票:0回答:1

我只想用 Instagram API 制作一个 iOS 应用程序,然后我可以看到用户个人资料。 我刚刚在我的 Xcode 项目上编写了这段代码来制作 iOS 应用程序。

这是代码:--

    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.delegate = self
        signInRequest()
    }
    
    func signInRequest() {
        let getURL = String(format: "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@&DEBUG=True", arguments: [API.INSTAGRAM_AUTHURL,API.INSTAGRAM_CLIENT_ID,API.INSTAGRAM_REDIRECT_URI,API.INSTAGRAM_SCOPE])
        let request = URLRequest.init(url: URL.init(string: getURL)!)
        webView.loadRequest(request)
    }
    
    func checkRequestForCallbackURL(request: URLRequest) -> Bool {
        
        let requestURLString = (request.url?.absoluteString)! as String
        if requestURLString.hasPrefix(API.INSTAGRAM_REDIRECT_URI) {
            let range: Range<String.Index> = requestURLString.range(of: "#access_token=")!
            handleAuth(authToken: requestURLString.substring(from: range.upperBound))
            return false
        }
        return true
    }
    func handleAuth(authToken: String) {
        let url = String(format: "https://api.instagram.com/v1/users/self/?access_token=%@", authToken)
        let request: NSMutableURLRequest = NSMutableURLRequest(url: URL(string: url)!)
        request.httpMethod = "GET"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        
        
        
        let session = URLSession(configuration: .default)

        session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
            if let data = data {
                let json = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
                let strFullName = (json?.value(forKey: "data") as AnyObject).value(forKey: "full_name") as! String


                let secondVC: SecondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "secondSeg") as! SecondViewController

                secondVC.text = strFullName

                self.present(secondVC
                    , animated: true, completion: nil)

            }
        }.resume()
    }
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        return checkRequestForCallbackURL(request: request)
    }
}

我的问题是,当我运行我的应用程序并想要查看我在标签上看不到的 Instagram 用户用户名时。

我看不到用户(全名、用户名、关注者等...)

这是第二个VC代码。

    var text: String = ""
    
    @IBOutlet weak var userNameLabel: UILabel!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        userNameLabel?.text = text
    }
ios swift instagram-api
1个回答
0
投票

我相信您的 viewdidload 函数在更新文本变量之前被调用

尝试更换:

var text: String = ""

与:

var text: String = "" {
    didSet {
        userNameLabel?.text = text
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.