2014-06-30 22 views
2

我已經爲我的應用激活了「打開」功能,以便用戶可以在我的應用中打開他們的文檔。該應用程序顯示在打開的列表下,當用戶按下該圖標時,我的應用程序打開。問題是,我似乎無法理解如何繼續保存文件。這是我從衆多的StackOverflow的問題獲悉:保存從「打開」返回的文檔IOS

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation 
{ 
    if ([[DBChooser defaultChooser] handleOpenURL:url]) { 
     return YES; 
    } 

    if (url != nil && [url isFileURL]) { 

     NSError *error = nil; 
     NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error]; 
     if (error) { 
      NSLog(@"%@", [error localizedDescription]); 
     } else { 
      NSLog(@"Data has loaded successfully."); 
     } 
     return YES; 
    } 

    return NO; 
} 

我試圖抓住以下列方式中的數據,但有一個錯誤。此外,我想知道這種方法是否會對我現有的Dropbox導入程序造成任何干擾...我已經讀過這些文件會自動保存在文檔/收件箱路徑中。

作爲文件類型,我可以打開我設置public.content(文檔交互控制器可以打開的所有文件,我相信)。

+0

什麼是你得到的錯誤? – valheru

+0

NSLog之一(@「%@」,[error localizedDescription]); – Alessandro

+0

但是從那個日誌中顯示的實際錯誤是什麼? – valheru

回答

1

使用下面的代碼

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ 

if(url !=nil) 
{ 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 
    [urlData writeToFile:YourDocumentsDirectoryPath atomically:YES]; 
} 
    return YES; 
    } 
1

使用下面的代碼:

#define DocumentsDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0] 

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    NSLog(@"URL : %@", url); 

    NSArray *tempArray = [[NSString stringWithFormat:@"%@",url] componentsSeparatedByString:@"/"]; 
    NSString *aStrPath = [NSString stringWithFormat:@"%@/%@",DocumentsDirectory, [tempArray lastObject]]; 

    NSData *aData = [NSData dataWithContentsOfURL:url]; 
    [aData writeToFile:aStrPath atomically:YES]; 

    return YES; 
} 

本準則的工作...!

0

這是我在代碼中打開的工作。應該指出的是,這些都是pdf的,可能問題出在您打開的文件類型上。 另請注意,如果要在本地複製文件,清理收件箱文件非常重要。否則,你會節省數據和佔用空間。

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 

    if ([url isFileURL]) 

    NSData *data = [NSData dataWithContentsOfURL:inboxPath]; 
    [data writeToFile:scorePath atomically:YES]; 
    [[NSFileManager defaultManager] removeItemAtURL:inboxPath error:nil]; 
0

當您打開文檔時,文檔會保存到您的應用程序目錄中。事情是您必須獲取url並執行必要的操作。

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    // your code 

    return YES; 
} 

在您的應用程序中打開任何文檔時,使用此代理方法在Appdelegate.m.this方法中調用。

下載iExplorer,使用它可以查看應用程序中的文檔和文件。使用open-in feature打開文件後,應用程序的文檔目錄得到更新。