2014-01-09 23 views
0

我可以同時加載iPhone和iPad加載相同的HTML文件,但是我想根據加載程序加載哪個設備來加載更優化的頁面。在iOS中加載不同的HTML UIWebView for iPhone和iPad

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 

我試圖創建一個新的出路,但我似乎遇到了相互重疊的代碼。

如何爲通用版本的iPad和iPhone加載不同的HTML文件?

回答

1

只需使用一個Web視圖。所有你需要的是兩個不同的HTML文件。

那麼你的代碼可以是這樣的:

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"]; 
} 

這裏假設你有一個都和index-iphone.html一個index-ipad.html文件在您的應用程序包。

+0

這太好了!我注意到的一件事是,第一行需要關閉,導致'NSURL * url;'謝謝,我會接受這個作爲最佳答案,一旦SO允許我。 – block14

+0

糟糕 - 這是一個錯字。我已經添加了分號。謝謝。 – rmaddy