2013-03-12 11 views
10

我的應用程序創建一個名爲log.txt的文件共享/在另一個應用程序發送的文件(「打開」,在iOS版)

該文件的URL(在Xcode查看)一個簡單的文件是文件://本地主機/無功/移動/應用/ 數中的應用的 /Documents/log.txt

所以我可以看到在取景器這個文件......

我想在「打開」功能添加到我的應用程序提供用戶共享此文件(通過郵件或imessage)或在另一個兼容的應用程序中打開此文件。

這裏是我做的:

-(void) openDocumentIn { 

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:docFile]; //docFile is the path 
//NSLog(@"%@",fileURL); // -> shows the URL in the xcode log window 

UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; 
documentController.delegate = self; 
documentController.UTI = @"public.text"; 
[documentController presentOpenInMenuFromRect:CGRectZero 
             inView:self.view 
            animated:YES]; 
} 

然後調用此函數:

-(IBAction)share:(id)sender { 
    [self openDocumentIn]; 
} 

當我運行應用程序時,我點擊這個「共享」按鈕,但沒有追加,除了向我顯示日誌窗口中URL的路徑...

我錯過了一些東西...

謝謝

編輯:最後,它在我真正的iphone上工作...在模擬器中沒有文本查看器! - 」

編輯2:它顯示可用的應用程序(頁,碰撞......),但最終崩潰:(((see here for the crash picture

+0

你肯定有安裝了應用在你的設備上可以打開文件類型? – 2013-03-12 15:51:35

+0

請參閱我的文章中的編輯和編輯2 – 2013-03-12 16:10:39

+0

打開斷點導航器,單擊右下角的+號,添加異常斷點,完成。再次運行該應用程序,現在調試器將在引發異常時停止。 – 2013-03-12 16:17:49

回答

0

這裏是如何爲我的作品:

!我只是把宣言「UIDocumentInteractionController * documentController」在.h文件和它的作品!

我真的不知道爲什麼,但是....

+2

@fzaziz寫了正確的答案..你應該將其標記爲已接受 – 2015-03-25 07:10:47

41

它的內存管理的問題,最主要的原因是崩潰是因爲對象不是保留。這就是爲什麼如果你在.h文件中聲明它,並且在你賦值時寫入@property作爲保留,那麼對象將被保留。

在您的接口文件(.h)中

所以,你應該有

@property (retain)UIDocumentInteractionController *documentController; 
在您的m(執行文件)

然後,你可以做

@synthesize documentController; 

- (void)openDocumentIn{ 

    // Some code here 

    self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; 
     documentController.delegate = self; 
    documentController.UTI = @"public.text"; 
    [documentController presentOpenInMenuFromRect:CGRectZero 
            inView:self.view 
           animated:YES]; 
    // Some more stuff 
} 
+1

這解決了我的問題。應該是被接受的答案。 – 2014-08-02 13:30:08

+0

是的,謝謝,這是它! – 2015-03-04 18:10:14

+0

關於我的問題的任何想法。 http://stackoverflow.com/questions/30613645/add-edit-in-exel-or-edit-photo-extension – Durgaprasad 2015-06-04 09:40:33

相關問題