2011-11-11 28 views
4

我試圖實現一個IOS應用程序。我的應用程序的需要是從DropBox應用程序(我的iphone有DropBox應用程序)訪問文檔(pdf,照片等)。我是iphone開發新手,所以我不知道從DropBox訪問文檔。如何從DropBox訪問我的ios應用程序中的文檔

如果有人知道請幫助我。提前致謝。

+0

您不能訪問某些其他應用程序的文件。你應該使用Dropbox API http://stackoverflow.com/questions/3502649/accessing-dropbox-in-your-iphone-app –

+0

感謝您的重播。 – John

回答

8

首先,您需要官方的Dropbox iOS SDK。接下來,您將需要一個可從Dropbox網站獲取的應用程序密鑰(選擇MyApps)。您會注意到Dropbox iOS SDK隨附了一個捆綁演示應用程序,所以請看看那裏。另外,可以找到好的入門教程here
訪問一個文件,你的代碼看起來是這樣的:

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]; 

請注意,iOS版的Dropbox SDK需要iOS 4.2或更高版本。

+0

感謝您的回覆 – John

+0

如果您覺得滿意,請隨時接受答案。 – Maggie

0

您可以使用UIDocumentMenuViewController類從其他共享文件的應用程序訪問文件。你可以找到所有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)") 
    } 
} 

你會看到這樣的畫面,供選擇的文件

相關問題