0

I want to develop an ePub-reader application using UIPageViewController and UIWebView for displaying book pages. So I can make pages from different chapters by perform offset with JavaScript-strings.

The question is how can I implement the UIPageViewControlerDataSource delegate working in rightful way? When I prepare some page for showing I should use UIWebView methods and UIWebView delegate methods which are working only if webView is on the screen yet. I tried to make underlying webView under pageViewController for making calculus linked with ePub chapters paging but I can't drag loaded content on the page.

Does anyone know how is it implemented in iBooks or the same apps?

agoncharov
  • 822
  • 1
  • 6
  • 15

1 Answers1

1

One easy solution could be:

  • subclass UIPageViewController, make it conform to UIPageViewControllerDataSource and UIWebViewDelegate protocols and set self as datasource in the -viewDidLoad override
  • implement pageViewController:viewControllerAfterViewController: and pageViewController:viewControllerBeforeViewController: (UIPageViewControllerDataSource protocol methods) so that they return a new UIViewController containing a UIWebView displaying your data.

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
    {
        UIViewController *vc = [[UIViewController alloc] init];
        UIWebView *webview = [[UIWebView alloc] init];
        webview.delegate = self;
    
        // Here you can call - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL
        // to load your web page as you need
    
        vc.view = webview;
    
        return vc;
    }
    

Of course you have to complete the code stub. You should setup a mechanism to distinguish between viewControllers to be able to return the next and the previous page (i.e. I do it with a UIPageViewController subclass declaring a pageNumber property, but I am sure there are more elegant ways to do it..)

Andrea Cremaschi
  • 533
  • 3
  • 19
  • But what about this line in Apple's UIPageViewController Class Reference: "This class is not intended to be subclassed." ? – agoncharov Feb 02 '13 at 19:29
  • Sorry, I meant "UIViewController subclass", not UIPageViewController (now corrected in the answer). Anyway, the sentence you quoted with iOS 6 documentation has become: `This class is generally used as-is but may be subclassed in iOS 6 and later.` - a softer policy.. More in general, as far as I understand it, this usually just means that one should not try to change the default behaviour of the controller in subclasses. – Andrea Cremaschi Feb 02 '13 at 19:50
  • Ah no ok sorry again, I really meant to subclass UIPageViewController :) So, the rest of the previous comment should answer your doubt – Andrea Cremaschi Feb 02 '13 at 19:57
  • Thanks for your answer, I'll try implement this solution. Seems to be legit =) – agoncharov Feb 02 '13 at 20:06