We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Mark L.45
10 years agoCollaborator | Level 9
URI HTTPS request
Hi,
I am trying to follow this dropbox tutorial; but with success
https://blogs.dropbox.com/developers/2013/07/using-oauth-2-0-with-the-core-api/
I tried this call using the parameters shown an...
Mark L.45
10 years agoCollaborator | Level 9
Hi Gregory,
Here is a code; based on an app coda tutorial by Joyce Echessa; I hope she doesn't mind me posting it here. This is the View Controller: The App Delegate is a simply the template code. I have commented out the clientID since this is a public post.
If I use localhost I find the data I need in WKNavigation error field; if I use the dropbox URL you gave me; I don't get an error... but don't know where I can find the access_token using Swift.
import UIKit
import WebKit
// Created by Joyce Echessa on 1/6/15.
// Copyright (c) 2015 Appcoda. All rights reserved.
class ViewController: UIViewController, UITextFieldDelegate, WKNavigationDelegate {
var webView: WKWebView
@IBOutlet weak var barView: UIView!
@IBOutlet weak var urlField: UITextField!
@IBOutlet weak var backButton: UIBarButtonItem!
@IBOutlet weak var forwardButton: UIBarButtonItem!
@IBOutlet weak var reloadButton: UIBarButtonItem!
@IBOutlet weak var progressView: UIProgressView!
required init?(coder aDecoder: NSCoder) {
self.webView = WKWebView(frame: CGRectZero)
super.init(coder: aDecoder)
self.webView.navigationDelegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
barView.frame = CGRect(x:0, y: 0, width: view.frame.width, height: 30)
view.insertSubview(webView, belowSubview: progressView)
webView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: webView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: -44)
let width = NSLayoutConstraint(item: webView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0)
view.addConstraints([height, width])
webView.addObserver(self, forKeyPath: "loading", options: .New, context: nil)
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
// let url = NSURL(string:"https://www.dropbox.com/oauth2/authorize?client_id=X&response_type=token&redirect_uri=https://www.dropbox.com/1/oauth2/redirect_receiver&state=MTIzNDEyNeW=")
let url = NSURL(string:"https://www.dropbox.com/oauth2/authorize?client_id=X&response_type=token&redirect_uri=https://localhost&state=MTIzNDEyNeW=")
let request = NSURLRequest(URL:url!)
print("request \(request)")
webView.loadRequest(request)
//backButton.enabled = false
//forwardButton.enabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
barView.frame = CGRect(x:0, y: 0, width: size.width, height: 30)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
urlField.resignFirstResponder()
webView.loadRequest(NSURLRequest(URL:NSURL(string: urlField.text!)!))
print("urlField.text \(urlField.text!)")
return false
}
@IBAction func back(sender: UIBarButtonItem) {
webView.goBack()
}
@IBAction func forward(sender: UIBarButtonItem) {
webView.goForward()
}
@IBAction func reload(sender: UIBarButtonItem) {
let request = NSURLRequest(URL:webView.URL!)
print("reload \(request)")
webView.loadRequest(request)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<()>) {
if (keyPath == "loading") {
backButton.enabled = webView.canGoBack
forwardButton.enabled = webView.canGoForward
}
if (keyPath == "estimatedProgress") {
progressView.hidden = webView.estimatedProgress == 1
progressView.setProgress(Float(webView.estimatedProgress), animated: true)
}
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print("WKNavigation \(error)")
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
progressView.setProgress(0.0, animated: false)
}
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!