Search code examples
iosobjective-cuiwebviewuipageviewcontrolleribooks

UIPageViewControlerDataSource and UiWebView for displaying on pages


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?


Solution

  • 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..)