John
John

Reputation: 764

How can access the documents from DropBox in my ios application

I am trying to implement an IOS application. The need of my application is to access the documents(pdf,photo etc) from DropBox Application(My iphone has DropBox Application ) .I am new in iphone development ,so i have no idea about access the document from DropBox.

If anybody know please help me . Thanks in advance.

Upvotes: 4

Views: 4406

Answers (2)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

You can use UIDocumentMenuViewController class to access the files from other apps that shares their files. You can find all UTI's here

import MobileCoreServices

class ViewController: UIViewController, UITextFieldDelegate, UIDocumentPickerDelegate, UIDocumentMenuDelegate {
  override func viewDidLoad() {
    super.viewDidLoad()
  }


  @IBAction func handleImportPickerPressed(sender: AnyObject) {
    let documentPicker = UIDocumentMenuViewController(documentTypes: [kUTTypePDF as String], in: .import)
    documentPicker.delegate = self
    present(documentPicker, animated: true, completion: nil)
  }

  // MARK:- UIDocumentMenuDelegate
  func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
    documentPicker.delegate = self
    present(documentPicker, animated: true, completion: nil)
  }

  // MARK:- UIDocumentPickerDelegate
  func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    // Do something
    print("\(url)")
  }
}

You will see this kind of screen for choosing the document

Upvotes: 0

Maggie
Maggie

Reputation: 8101

First, you will need official Dropbox iOS SDK. Next, you will need an application key, which you can obtain from Dropbox website (choose MyApps). You will notice that the Dropbox iOS SDK comes with a bundled demo application, so have a look there. Also, good starting tutorial can be found here.
To access a file, your code will look something like:

 NSString* consumerKey; //fill your key
 NSString* consumerSecret ; //fill your secret
 DBSession* session = [[DBSession alloc] initWithConsumerKey:consumerKey 
       consumerSecret:consumerSecret];
 session.delegate = self;
 [DBSession setSharedSession:session];
 [session release];
 if (![[DBSession sharedSession] isLinked])
 {
        DBLoginController* controller = [[DBLoginController new] autorelease];
        controller.delegate = self;
        [controller presentFromController:self];
 }
 DBRestClient *rc = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
 self.restClient = rc;
 [rc release];
 self.restClient.delegate = self;

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"SampleFile.txt"];
 [self.restClient loadFile:@"/example/SampleFile.txt" intoPath:filePath];

Please note that iOS Dropbox SDK requires iOS 4.2 or greater.

Upvotes: 8

Related Questions