Search code examples
iosswiftinstagram-api

Swift Instagram API username isn't displayed on label


I just wanna make an iOS app with Instagram API then I can see the user profile. I just wrote this Code on my Xcode project to make iOS app.

Here is the code :--

    @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)
    }
}

My question is when I run my app and want to see the Instagram user username I can't see on my label.

I can't see the user (full_name, username, followers, etc...)

Here is secondVC Code.

    var text: String = ""
    
    @IBOutlet weak var userNameLabel: UILabel!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        userNameLabel?.text = text
    }

Solution

  • I believe your viewdidload function is being called before the update of your text variable

    Try replacing:

    var text: String = ""
    

    With:

    var text: String = "" {
        didSet {
            userNameLabel?.text = text
        }
    }