block14
block14

Reputation: 619

Load Different HTML in iOS UIWebView for iPhone and iPad

I can get both iPhone and iPad to load the same HTML file, however I would like to load a more optimized page based on which device the program is loading on.

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIWebView *webView;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
    NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];

    [_webView loadHTMLString:html baseURL:baseURL];

    // disable scrolling
    _webView.scrollView.scrollEnabled = NO;
    _webView.scrollView.bounces = NO;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I have tried to create a new outlet, but I seem to be running into overlapping code.

How can I load different HTML files for iPad and iPhone with a universal build?

Upvotes: 0

Views: 384

Answers (1)

rmaddy
rmaddy

Reputation: 318774

Just use one web view. All you need is two different HTML files.

Then you code can be something like:

NSURL *url;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    url = [[NSBundle mainBundle] URLForResource:@"index-iphone" withExtension:@"html"];
} else {
    url = [[NSBundle mainBundle] URLForResource:@"index-ipad" withExtension:@"html"];
}

This assumes you have both an index-iphone.html and a index-ipad.html file in your app bundle.

Upvotes: 1

Related Questions