How to add a CSS file to the iphone project in xcode?

advertisements

I am working on a web based application for iphone, I have created new cssfile to the project this css file doesn't affect the html file, but when I have uploaded the css file a host it worked fine and styled the html

what is the problem ?

edit 1

this the head element .

css.css is working fine and is linked to the html correctly jquery.mobile-1.0.min.css is NOT !

despite the both files exist in the same border

Onotha.com

<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.0.min.css" />

edit 2

This is the objectve-c code, I have an exception, I think in the last line of code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [window makeKeyAndVisible];
    NSString *path= [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"  inDirectory:NO];
//  [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];

//  NSString *path = [[NSBundle mainBundle] bundlePath];
//  NSURL *baseURL = [NSURL fileURLWithPath:path];
//  [webView loadHTMLString:htmlString baseURL:baseURL];

    // load css styles
    NSString *cssPath   = [[NSBundle mainBundle] pathForResource:@"css/jquery.mobile-1.0.min.css" ofType:@"css"];
    NSData *cssData     = [NSData dataWithContentsOfFile:cssPath];
    NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];

    // load js
    NSString *jsPath   = [[NSBundle mainBundle] pathForResource:@"MyJS" ofType:@"js"];
    NSData *jsData     = [NSData dataWithContentsOfFile:jsPath];
    NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];                         

    // compose full html page
    NSString *pageContent = [NSString stringWithFormat:@"%@%@%@", cssString, jsString, path];
    [webView loadHTMLString:pageContent baseURL:baseURL];

    return YES;
}

edit 3

I got this after using code posted by Srikar


You can load CSS from local project directory

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

detail info check this site.

More elaborate code here -

// load css styles
NSString *cssPath   = [[NSBundle mainBundle] pathForResource:@"MyCSS" ofType:@"css"];
NSData *cssData     = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];

// load js
NSString *jsPath   = [[NSBundle mainBundle] pathForResource:@"MyJS" ofType:@"js"];
NSData *jsData     = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];                         

// compose full html page
NSString *pageContent = [NSString stringWithFormat:@"%@%@%@", cssString, jsString, actualPageMarkup];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:@""]];

More info here