Skip to content

Instantly share code, notes, and snippets.

@alex-zige
Last active August 29, 2015 14:11
Show Gist options
  • Save alex-zige/a1054743ee7026804657 to your computer and use it in GitHub Desktop.
Save alex-zige/a1054743ee7026804657 to your computer and use it in GitHub Desktop.
Basic htmls merging
//
// MCHWebViewController.m
// Gallipoli
//
// Created by Matt on 10/09/14.
// Copyright (c) 2014 Touchtech Limited. All rights reserved.
//
#import "MCHWebViewController.h"
// Third party
#import "MMDrawerBarButtonItem.h"
@interface MCHWebViewController () <UIWebViewDelegate>
@property (nonatomic, weak) IBOutlet UIWebView *webView;
@end
@implementation MCHWebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//
if (_useCustomBackButton == YES) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"< Back" style:UIBarButtonItemStylePlain target:self action:@selector(_back)];
}
// attempt to stop the loading flash
_webView.opaque = NO;
_webView.backgroundColor = [UIColor clearColor];
_webView.delegate = self;
// ADD Basic Tempalte Layout
NSString *layoutPath = @"htmls/templates/layout_ios.html";
NSString *layoutHtmlString = [NSString stringWithContentsOfFile:[self _pathInBundleForResource:layoutPath] encoding:NSUTF8StringEncoding error:nil];
if (_path != nil) {
NSString *htmlString = [NSString stringWithContentsOfFile:_path encoding:NSUTF8StringEncoding error:nil];
htmlString = [layoutHtmlString stringByReplacingOccurrencesOfString:@"<body></body>" withString:htmlString];
NSLog(@"%@",htmlString);
NSURL *baseURL = [NSURL fileURLWithPath:[self _pathInBundleForResource:layoutPath].stringByDeletingLastPathComponent];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
} else {
NSString *htmlString = @"<body><div class='container'><h1>works</h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris id mauris aliquet sapien rhoncus imperdiet nec ac magna. Morbi rutrum tortor vitae metus malesuada lobortis. Nam pulvinar, nisl sit amet lobortis pellentesque, justo diam volutpat elit, id blandit neque leo ut nulla. Aliquam rutrum tempor libero, ut porta enim molestie et. Curabitur elementum massa lacus, nec rutrum nulla accumsan in. Suspendisse potenti. Ut ut elementum leo. Cras semper egestas lorem, ut scelerisque magna sagittis molestie. Nullam porttitor arcu vel justo interdum, at volutpat dui interdum. Nulla euismod ipsum sapien, volutpat elementum risus luctus non. Nam et iaculis eros.\
\
Phasellus ultricies mauris vitae orci iaculis iaculis. Maecenas rutrum et turpis ac consectetur. Pellentesque ac ligula quis risus viverra placerat. Etiam nec neque velit. Fusce sapien lacus, rhoncus in suscipit eu, tempus non lorem. Mauris fringilla est eu tempor volutpat. Praesent maximus cursus turpis, sed condimentum turpis consectetur faucibus. Pellentesque dapibus tempor nibh id gravida.\
\
Morbi porta nec justo pellentesque malesuada. Curabitur tincidunt dapibus risus sed pharetra. Aenean porta elit sed justo iaculis ultrices. Cras ut iaculis nunc. Integer leo ex, lacinia sed placerat id, iaculis a felis. Etiam porta nisi nec arcu dapibus rhoncus. Suspendisse nec magna vitae est semper elementum et vel mi. Sed et risus ut tortor gravida condimentum. Mauris neque turpis, interdum vitae elit quis, volutpat gravida dui. Vivamus dictum, mi non imperdiet tristique, ipsum velit maximus ante, facilisis tincidunt lorem turpis nec nulla. Sed velit mi, ultrices malesuada suscipit at, mattis quis arcu.\
\
In imperdiet odio ut augue laoreet posuere. Mauris quis diam imperdiet, tempor lorem ac, venenatis eros. Nunc iaculis sem varius, dictum nisl in, pharetra odio. Nullam eget congue ex, ac aliquam orci. Fusce vehicula vel erat ut efficitur. Nunc dapibus convallis suscipit. Integer dapibus metus et turpis eleifend, tristique lacinia eros feugiat. Curabitur cursus condimentum tincidunt. Integer aliquam purus quis felis molestie rhoncus. Aliquam et pretium elit, sed mollis nisl. Pellentesque ut congue lectus. Proin vulputate massa in nunc cursus, sed tempus nibh ullamcorper. Aliquam sit amet magna massa. Nulla vel vestibulum tellus. Suspendisse potenti. Cras sed laoreet justo.\
\
Aliquam nec mi diam. Praesent ac libero mauris. Morbi at tellus nec sapien feugiat gravida. Donec eget est at justo luctus auctor. Duis quis dapibus erat, vitae luctus turpis. Duis metus urna, sagittis eget aliquet non, dapibus ac nibh. Duis non rhoncus mauris, quis rutrum nulla. Quisque porta, nisl eget vulputate lobortis, tellus sapien ultrices sapien, non placerat magna sapien non libero. Cras eget nunc ac lacus placerat pretium ut vel nisi. Integer laoreet non mauris at volutpat. Fusce molestie et tellus nec laoreet. Nulla vel auctor nulla. Mauris dignissim varius sem vel feugiat.</div></body>";
htmlString = [layoutHtmlString stringByReplacingOccurrencesOfString:@"<body></body>" withString:htmlString];
NSLog(@"%@",htmlString);
NSURL *baseURL = [NSURL fileURLWithPath:[self _pathInBundleForResource:layoutPath].stringByDeletingLastPathComponent];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
}
}
- (NSString *)_pathInBundleForResource:(NSString *)resource
{
if (resource == nil)
return nil;
return [[NSBundle mainBundle] pathForResource:[resource.lastPathComponent stringByDeletingPathExtension]
ofType:resource.pathExtension
inDirectory:resource.stringByDeletingLastPathComponent];
}
#pragma mark - UIWebviewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
{
NSLog(@"didFailLoadWithError: %@", error.localizedDescription);
}
#pragma mark - Private
- (void)_back
{
[[NSNotificationCenter defaultCenter] postNotificationName:MCHTrailNavigationPopNotification object:nil];
[self.navigationController popViewControllerAnimated:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment