2012-11-14 29 views
8

Tôi đang phát triển một ứng dụng trình bao bọc cho một trang web. Về cơ bản, nó sẽ mở phiên bản di động của một trang web trong UIWebView. Một số liên kết trên trang web trỏ tới tệp PDF.Thêm "Mở trong iBooks" vào UIWebView

Khi cùng một trang web được mở trong Safari và liên kết tới PDF được khai thác một sọc đen đẹp với "Mở trong iBooks" được hiển thị trên PDF. Giống như trên hình dưới đây:

enter image description here

Làm thế nào tôi có thể thực hiện các sọc nhìn nhau trong ứng dụng của tôi?

EDIT:

Tôi không hỏi về làm thế nào để tạo ra một nút màu đen trên nền mờ.

Tôi quan tâm đến tái tạo toàn bộ quy trình làm việc:

  • dùng điều hướng đến một PDF
  • Một sọc (xem) popup khi và chỉ khi có iBooks ứng dụng (hoặc bất kỳ xem PDF khác) Cài đặt.
  • Nhấn vào một nút trong cửa sổ bật lên sẽ chuyển tài liệu sang ứng dụng đó và ứng dụng sẽ mở ra.
+0

VIEW (các sọc?) Hoặc các chức năng của nút? –

+0

@ Daij-Djan vui lòng xem cập nhật cho câu hỏi – Bobrovsky

+0

những gì tôi lo ngại: D jk u có thể sử dụng sơ đồ url iBook –

Trả lời

24

Để kiểm tra xem iBooks được cài đặt bạn có thể gọi:

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

Bạn có thể trình bày một danh sách các ứng dụng (tại sao giới hạn để chỉ iBooks;?)) Sử dụng:

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

Lưu ý rằng thao tác này không hoạt động trên Trình mô phỏng iOS trừ khi bạn tạo ứng dụng đọc tệp PDF!

Nếu bạn thực sự muốn chỉ cung cấp tùy chọn để mở tệp PDF trong iBooks, bạn có thể thử thêm URL của tệp vào lược đồ @ "ibooks: //" hoặc một trong hai lược đồ khác iBooks cung cấp (mà làm việc cho các sách trong iBook Store nhưng tôi không chắc chắn nó cũng hoạt động cho các URL khác) là @ "itms-books: //" và @ "itms-bookss: //". Sau đó bạn có thể làm điều gì đó như:

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

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

Cảm ơn bạn đã trả lời chi tiết. – Bobrovsky

+0

Tôi biết đây là một câu hỏi cũ nhưng nếu bạn sử dụng cách này theo ARC, bạn sẽ nhận được một lỗi. self.docInteractionController = [Tương tác UIDocumentInteractionControllerControllerWithURL: url]; –

+1

Chỉ cần một lưu ý phụ: url cho UIDocumentInteractionController phải là địa phương, tập tin url (không phải là một được sử dụng để downlaod pdf để UIWebView). Vì vậy, bạn cần phải lưu dữ liệu pdf tải về với phần mở rộng pdf đầu tiên (tốt nhất để thư mục tài liệu của bạn) và sau đó tạo url với [NSURL fileURLWithPath: pdfPath]. – Lukasz

1

(đáp lại là câu trả lời trước đây của tôi không có mã Xin lỗi.)

Đối với một giải pháp mà cố định vấn đề của tôi Tôi tìm thấy một ví dụ tuyệt vời here.

Tôi đã cắt và dán nó ở đây trong trường hợp nó giúp ai đó. tín dụng đầy đủ để absoluteripple.com

Giả sử lớp học của bạn được gọi là ViewController, sau đó trong tập tin ViewController.h:

  
@interface ViewController : UIViewController 
            { 
                UIDocumentInteractionController *docController; 
            } 

Thêm phương pháp sau đây trong ViewController.m: // - thiết lập bộ điều khiển UIDocumentInteraction và thiết lập đại biểu của mình để tự vì vậy chúng tôi có thể xử lý các sự kiện callback

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

// - phương pháp dụ mấu chốt ở đây là presentOptionsMenuFromBarBUttonItem // - nó được giả định ở đây rằng có một BarButtonItem được gọi là _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. Thêm một phương pháp để gọi trên khi bạn muốn hiển thị các ứng dụng mà bạn có thể gửi hồ sơ cho Trong ví dụ này, một UIBarButton là dây lên đến IBActions sau:
 
    - (IBAction)ActionButtonClicked:(id)sender { 
            [self showOptionsMenu];} 

Đó Là nó. Khi nút được nhấp vào một trang tính hành động sẽ xuất hiện (tất cả được hỗ trợ bởi lớp UIDocumentInteractionController của Apple) hiển thị các ứng dụng (nếu có) mà bạn có thể gửi tệp đến.

Bạn có thể tùy chọn thực hiện các phương pháp đại biểu sau đây:

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

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

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller 
Các vấn đề liên quan