Declared in UIApplication+NativeCSS.h

Overview

nativeCSS styles your app and manages your style content, by downloading and caching remote assets such as images and fonts.

Applying the CSS

Styling is applied to your application using styleWithCSSString: we recommend calling this when your application becomes active. - (void)applicationDidBecomeActive:(UIApplication *)application{

    [UIApplication styleWithCSSString:@"label{background-color:red}"];

Using remote CSS

Styling can be applied from a remote CSS file and nativeCSS handles all the caching for you. For most users we recommend our simple call to load a remote URL, update periodically and have an initial CSS file included in your app for the first startup.

- (void)applicationDidBecomeActive:(UIApplication *)application{

    NSURL *cssURL = [NSURL URLWithString:@"http://localhost:8000/styles.css"];

    [UIApplication styleWithCSSFile:@"initial.css"
                             updateFromURL:cssURL
                             refreshPeriod:NCRemoteContentRefreshEverySecond];

More advanced options for managing CSS content are also provided.

Managing the CSS content manually

There are many ways to download and cache your CSS content. nativeCSS provides helper functions to simplify this. These helper functions are useful at different time of the project lifecycle.

For development: we recommend regularly refreshing the CSS content with updateCSSFromURL:repeatInterval:completionBlock: - (void)applicationDidBecomeActive:(UIApplication *)application{

    [UIApplication updateCSSFromURL: yourCSSURL] repeatInterval:1.0
                   completionBlock:^(BOOL success,  BOOL cssIsDifferent, NSString *cssContent) {
        if(success && cssIsDifferent)
            [UIApplication styleWithCSSString:cssContent];
}];

For release: we recommend using a bundled CSS file and updating your CSS with fetchMissingCSSAndAssetsFromURL:completionBlock:. This can be safely called multiple times, and will always return cached versions. - (void)applicationDidBecomeActive:(UIApplication *)application{

    NSString *initialCSSPath = [[NSBundle mainBundle] pathForResource:@"initial.css" ofType:nil];
    NSString *initialCSS = [NSString stringWithContentsOfFile:initialCSSPath encoding:NSASCIIStringEncoding error:nil];
    [UIApplication styleWithCSSString:initialCSS]; // apply initial css

    // obtain new CSS and all remote assets:
    [UIApplication fetchMissingCSSAndAssetsFromURL: yourCSSURL 
                   completionBlock:^(BOOL success, BOOL cssIsDifferent, NSString* cssContent, NSArray *unavailableAssets) {
        if(success && cssIsDifferent){
            [self showLoadingScreen]; // implement this yourself
            [UIApplication styleWithCSSString:cssContent];
            [self hideLoadingScreen]; // implement this yourself
    }];

Important: fetchMissingCSSAndAssetsFromURL:completionBlock: will never update its cached content.

To update a file regularly we recommend either updateCSSFromURL:completionBlock: or updatedCSSAndAssetsFromURL:completionBlock:, obviously reloading the assets will take alot more time and bandwidth. It’s up to you how frequently to call this method. Presumably updating every day or week will be sufficient for most applications.

[UIApplication updateCSSAndAssetsFromURL: yourCSSURL 
               completionBlock:^(BOOL success,  BOOL cssIsDifferent, NSString* cssContent, NSArray *unavailableAssets) {
    if(success && cssIsDifferent){
        [self showWaitView]; // implement this yourself
        [UIApplication styleWithCSSString:cssContent];
        [self hideWaitView]; // implement this yourself
    }
}];

We use a standard cache implementation https://github.com/rs/SDWebImage, but there will be no conflicts if you have this already installed, as we’ve renamed the relevant files.

Class Methods

assetURLsFromURL:

Extracts all image and font assets in the target CSS URL. The url is expected The specified URL is expected to return CSS content containing references to images or fonts.

+ (NSArray *)assetURLsFromURL:(NSURL *)url

Parameters

url

location of the remote CSS

Discussion

Extracts all image and font assets in the target CSS URL. The url is expected The specified URL is expected to return CSS content containing references to images or fonts.

Declared In

UIApplication+NativeCSS.h

cachedAssetForURL:

Returns a file reference to a locally cached CSS file or asset.

+ (id)cachedAssetForURL:(NSURL *)url

Parameters

url

location of remote CSS or asset

Return Value

File reference if cached, otherwise null

Discussion

Returns a file reference to a locally cached CSS file or asset.

Declared In

UIApplication+NativeCSS.h

clearCSSAssetCache

Deletes all cached assets.

+ (void)clearCSSAssetCache

Discussion

Deletes all cached assets.

Declared In

UIApplication+NativeCSS.h

deleteCachedAssetForURL:

Deletes the cached asset downloaded from the specified URL. The specified URL is expected to return an asset, such as an image.

+ (void)deleteCachedAssetForURL:(NSURL *)url

Parameters

url

location of a remote asset.

Discussion

Deletes the cached asset downloaded from the specified URL. The specified URL is expected to return an asset, such as an image.

Declared In

UIApplication+NativeCSS.h

deleteCachedAssetsFromURL:

Deletes all the cached assets referenced by the CSS content at the specified URL. The specified URL is expected to return CSS content containing references to images or fonts.

+ (void)deleteCachedAssetsFromURL:(NSURL *)url

Parameters

url

location of CSS content, which references remote assets.

Discussion

Deletes all the cached assets referenced by the CSS content at the specified URL. The specified URL is expected to return CSS content containing references to images or fonts.

Declared In

UIApplication+NativeCSS.h

fetchMissingCSSAndAssetsFromURL:completionBlock:

Download and cache, if missing, remote CSS file and associated styling assets like fonts, images etc. After all assets are downloaded the completion block is called.

+ (void)fetchMissingCSSAndAssetsFromURL:(NSURL *)url completionBlock:(void ( ^ ) ( BOOL success , BOOL cssIsDifferent , NSString *cssContent , NSArray *unavailableAssets ))completionBlock

Parameters

url

location of CSS file.

completionBlock

Callback block with success as true, if the CSS and all the assets were downloaded otherwise success as false with CSS content, if available, and a list of remote failed assets.

Discussion

Download and cache, if missing, remote CSS file and associated styling assets like fonts, images etc. After all assets are downloaded the completion block is called.

This method should be used with styleWithCSSString: to display a loading screen:

[self showWaitView];
[UIApplication fetchMissingCSSAndAssetsFromURL:url 
                                 completionBlock:^(BOOL success, BOOL cssIsDifferent, NSString* cssContent, NSArray *unavailableAssets) {
    [UIApplication styleWithCSSString:cssContent];
    [self hideWaitView];
}];

Warning: Remote assets are only downloaded once, to update any remote assets call updateCSSAndAssetsFromURL:completionBlock:

Declared In

UIApplication+NativeCSS.h

isAssetCachedForURL:

Tests if a CSS file or asset as been cached locally.

+ (BOOL)isAssetCachedForURL:(NSURL *)url

Parameters

url

location of remote CSS or asset

Return Value

true if cached, otherwise false

Discussion

Tests if a CSS file or asset as been cached locally.

Declared In

UIApplication+NativeCSS.h

lastModifiedDateForURL:

Returns the last modified date for asset url.

+ (NSDate *)lastModifiedDateForURL:(NSURL *)url

Parameters

url

location of remote CSS or asset

Return Value

date of last download, otherwise nil

Discussion

Returns the last modified date for asset url.

Declared In

UIApplication+NativeCSS.h

setCSSDebugLogging:

Sets debug logging.

+ (void)setCSSDebugLogging:(BOOL)debugLoggingEnabled

Discussion

Sets debug logging.

Declared In

UIApplication+NativeCSS.h

styleWithCSSFile:

 Style application and any new views with a named CSS file.
+ (void)styleWithCSSFile:(NSString *)filename

Parameters

filename

CSS styles to be applied

Discussion

 Style application and any new views with a named CSS file.

Declared In

UIApplication+NativeCSS.h

styleWithCSSFile:debugLogging:

 Style application and any new views with named CSS file.
+ (void)styleWithCSSFile:(NSString *)filename debugLogging:(BOOL)debugLogging

Parameters

filename

CSS stylesheet to be applied

debugLogging

if true library will output log messages.

Discussion

 Style application and any new views with named CSS file.

Declared In

UIApplication+NativeCSS.h

styleWithCSSFile:updateFromURL:refreshPeriod:

Style application and any new views with CSS from a remote URL. On the first load the initial CSS file will be used, whilst the remote CSS and its assets are downloaded.

+ (void)styleWithCSSFile:(NSString *)filename updateFromURL:(NSURL *)remoteCSSURL refreshPeriod:(NCRemoteContentRefreshPeriod)refreshPeriod

Parameters

filename

an initial CSS file to apply before remote CSS is downloaded.

remoteCSSURL

the remote CSS to download and apply.

refreshPeriod

the time before the CSS is automatically updated.

Discussion

Style application and any new views with CSS from a remote URL. On the first load the initial CSS file will be used, whilst the remote CSS and its assets are downloaded.

Declared In

UIApplication+NativeCSS.h

styleWithCSSFile:withRemoteHost:

 Style application with CSS file using images with remote relative paths.

 Any image url beginning with a '/' will be appended to the supplied remote host. Note any existing path on the remote host will be ignored, so http://nativecss.com/styles/ will be read as http://nativecss.com/

 For example, a remote host of http://nativecss.com/ and css

 background-image: url('/path/img.png/')

 will be rendered using the image url('http://nativecss.com/path/img.png/')
+ (void)styleWithCSSFile:(NSString *)filename withRemoteHost:(NSURL *)remoteHost

Parameters

filename

CSS stylesheet to be applied

remoteHost

remote host to prepend relative image urls with.

Discussion

 Style application with CSS file using images with remote relative paths.

 Any image url beginning with a '/' will be appended to the supplied remote host. Note any existing path on the remote host will be ignored, so http://nativecss.com/styles/ will be read as http://nativecss.com/

 For example, a remote host of http://nativecss.com/ and css

 background-image: url('/path/img.png/')

 will be rendered using the image url('http://nativecss.com/path/img.png/')

Declared In

UIApplication+NativeCSS.h

styleWithCSSString:

Style application and any new views with CSS.

+ (void)styleWithCSSString:(NSString *)cssContent

Parameters

cssContent

CSS styles to be applied

Discussion

Style application and any new views with CSS.

Warning: Views can be presented whilst assets are loading, as remote assets will be downloaded on demand. For a smooth page style use fetchMissingCSSAndAssetsFromURL:completionBlock:

Warning: Remote assets are only downloaded once, to update any remote assets call updateCSSAndAssetsFromURL:completionBlock:

Declared In

UIApplication+NativeCSS.h

styleWithCSSString:debugLogging:

Style application and any new views with CSS.

+ (void)styleWithCSSString:(NSString *)cssContent debugLogging:(BOOL)debugLogging

Parameters

cssContent

CSS styles to be applied

debugLogging

if true library will output log messages.

Discussion

Style application and any new views with CSS.

Declared In

UIApplication+NativeCSS.h

styleWithCSSString:withRemoteHost:

Style application with CSS using images with remote relative paths.

+ (void)styleWithCSSString:(NSString *)cssContent withRemoteHost:(NSURL *)remoteHost

Parameters

cssContent

CSS styles to be applied

remoteHost

remote host to prepend relative image urls with.

Discussion

Style application with CSS using images with remote relative paths.

Any image url beginning with a ‘/’ will be appended to the supplied remote host. Note any existing path on the remote host will be ignored, so http://nativecss.com/styles/ will be read as http://nativecss.com/

For example, a remote host of http://nativecss.com/ and css

background-image: url('/path/img.png/')

will be rendered using the image url(‘http://nativecss.com/path/img.png/‘))

Warning: Views can be presented whilst assets are loading, as remote assets will be downloaded on demand. For a smooth page style use fetchMissingCSSAndAssetsFromURL:completionBlock:

Warning: Remote assets are only downloaded once, to update any remote assets call updateCSSAndAssetsFromURL:completionBlock:

Declared In

UIApplication+NativeCSS.h

styleWithLESSString:

+ (void)styleWithLESSString:(NSString *)cssContent

styleWithLESSString:debugLogging:

+ (void)styleWithLESSString:(NSString *)cssContent debugLogging:(BOOL)debugLogging

updateCSSAndAssetsFromURL:completionBlock:

Download and cache remote CSS file and associated styling assets like fonts, images etc. After all assets are downloaded the completion block is called.

+ (void)updateCSSAndAssetsFromURL:(NSURL *)url completionBlock:(void ( ^ ) ( BOOL success , BOOL cssIsDifferent , NSString *cssContent , NSArray *unavailableAssets ))completionBlock

Parameters

url

location of CSS file.

completionBlock

Callback block with success as true, if the CSS and all the assets were downloaded otherwise success as false with CSS content, if available, and a list of remote failed assets.

Discussion

Download and cache remote CSS file and associated styling assets like fonts, images etc. After all assets are downloaded the completion block is called.

Declared In

UIApplication+NativeCSS.h

updateCSSFromURL:completionBlock:

Download and cache remote CSS file. After the CSS file is downloaded the completion block is called.

+ (void)updateCSSFromURL:(NSURL *)url completionBlock:(void ( ^ ) ( BOOL success , BOOL cssIsDifferent , NSString *cssContent ))completionBlock

Parameters

url

location of CSS file.

completionBlock

Callback block with success as true, if the CSS was downloaded otherwise success as false.

Discussion

Download and cache remote CSS file. After the CSS file is downloaded the completion block is called.

Warning: Remote assets are not downloaded with this method, to reload any remote assets call updateCSSAndAssetsFromURL:completionBlock:

Declared In

UIApplication+NativeCSS.h

updateCSSFromURL:repeatInterval:completionBlock:

Download and cache remote CSS file and repeat after the repeat interval. After the CSS file is downloaded the completion block is called every time.

+ (void)updateCSSFromURL:(NSURL *)url repeatInterval:(double)repeatInterval completionBlock:(void ( ^ ) ( BOOL success , BOOL cssIsDifferent , NSString *cssContent ))completionBlock

Parameters

url

location of CSS file.

repeatInterval

number of seconds between redownloading and caching remote CSS file and calling completion block.

completionBlock

Callback block with success as true, if the CSS was downloaded otherwise success as false.

Discussion

Download and cache remote CSS file and repeat after the repeat interval. After the CSS file is downloaded the completion block is called every time.

Warning: This method is not recommended for released apps, as it can be data intensive. Consider calling updateCSSFromURL:completionBlock: when the application is activated.

Warning: Remote assets are not downloaded with this method, to reload any remote assets call updateCSSAndAssetsFromURL:completionBlock:

Declared In

UIApplication+NativeCSS.h