2012-11-14 36 views
8

我正在開發一個網站的包裝應用程序。基本上,它會在UIWebView中打開移動版網站。網站上的某些鏈接指向PDF。添加「打開iBooks」UIWebView

當在Safari中打開同一站點並點擊PDF鏈接時,PDF上會顯示一個帶有「在iBooks中打開」的漂亮黑條。像下面的圖片:

enter image description here

我怎麼能實現我的應用程序相同的條紋看?

編輯:

詢問如何創建半透明背景上的黑色按鈕。

我對整個工作流再現:

  • 用戶導航到一個PDF
  • 條紋(視圖)彈窗當且僅當有iBooks應用程序(或任何其它PDF查看器)安裝。
  • 點擊彈出按鈕將文檔傳輸到該應用程序,並打開應用程序。
+0

的VIEW(條紋?)或按鈕的功能? –

+0

@ Daij-Djan請參閱問題 – Bobrovsky

+0

的更新內容我擔心:您可以使用ibooks url scheme –

回答

24

要檢查是否安裝了iBooks的您可以撥打:

BOOL iBooksInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"ibooks://"]]; 

就能呈現的應用程序列表(爲什麼限制到只iBooks的;?))使用:

//use the UIDocInteractionController API to get list of devices that support the file type 
NSURL *pdfURL = // your pdf link. 
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:pdfURL]; 

//present a drop down list of the apps that support the file type, click an item in the list will open that app while passing in the file. 
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 

請注意,這不適用於iOS模擬器,除非您創建了一個讀取PDF的應用程序!

如果您確實只想讓PDF在iBooks中打開,您可以嘗試將該文件的URL附加到@「ibooks://」方案或其他兩個方案中的一個即iBooks提供(它適用於iBook Store中的書籍,但我不確定它是否也適用於其他URL),它們是@「itms-books://」和@「itms-bookss://」。然後,您可以這樣做:

NSURL *iBooksURLScheme = [NSURL URLWithString:@"ibooks://"]; 
NSString *fileURLString = // your file URL as *string* 
NSURL *finalURL = [iBooksURLScheme URLByAppendingPathComponent:fileURLString]; 

[[UIApplication sharedApplication] openURL:finalURL]; 
+0

謝謝你的詳細解答。 – Bobrovsky

+0

我知道這是一個老問題,但如果你在ARC下使用這種方式,你會得到一個錯誤。 self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url]; –

+1

只需要注意一點:UIDocumentInteractionController的url必須是本地文件url(不是用於將pdf下載到UIWebView的文件)。因此,您需要首先以pdf擴展名保存下載的pdf數據(最好到您的文檔目錄),然後使用[NSURL fileURLWithPath:pdfPath]創建網址。 – Lukasz

1

對於固定我的問題,我發現了一個很好的例子here的解決方案(如我以前的答案沒有包括代碼道歉的話回答)。

我已經剪切並粘貼在此處,幫助某人。充分肯定absoluteripple.com

假設你的類被稱爲視圖控制器,然後在ViewController.h文件:

  
@interface ViewController : UIViewController 
            { 
                UIDocumentInteractionController *docController; 
            } 

添加以下方法,在視圖控制器。L: // - 建立UIDocumentInteraction控制器及其委託設置爲self,所以我們可以處理回調事件

- (UIDocumentInteractionController *) setupControllerWithURL:(NSURL *)fileURL 
                                               usingDelegate:(id <UIDocumentInteractionControllerDelegate>)         interactionDelegate { 
    
            UIDocumentInteractionController *interactionController = 
            [UIDocumentInteractionController interactionControllerWithURL:fileURL]; 
            interactionController.delegate = interactionDelegate; 
    
            return interactionController; 
            } 

// - 這裏的關鍵實例方法是presentOptionsMenuFromBarBUttonItem // - 這裏假定有一個名爲BarButtonItem _btnActions

  

    - (void)showOptionsMenu 
            { 
                NSURL *fileURL = [NSURL fileURLWithPath:@"THE_FILE_URL_PATH"]; 
                docController = [self setupControllerWithURL:fileURL 
                                       usingDelegate:self]; 
                bool didShow = [docController presentOptionsMenuFromBarButtonItem:_btnActions 
                                                                 animated:YES]; 
                if (!didShow) { 
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                                                            message:@"Sorry. The appropriate apps are not found on this device." 
                                                           delegate:nil 
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles: nil]; 
                        [alert show]; 
                } 
            } 
  1. 添加一個方法來調用上面,當你想證明你可以將文件發送到 在這個例子中的應用程序,一個UIBarButton連線到以下IBActions:
 
    - (IBAction)ActionButtonClicked:(id)sender { 
            [self showOptionsMenu];} 

那是嗎。點擊按鈕後,會出現一個操作手冊(全部由Apple的UIDocumentInteractionController類支持),顯示您可以將文件發送到的應用程序(如果有的話)。

您可以根據需要實現以下的委託方法:

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application 

- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application 

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller 
相關問題