Skip to content
This repository was archived by the owner on Nov 15, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source 'https://github.com/CocoaPods/Specs.git'

target 'JBWebViewController' do
use_frameworks!
pod 'ARChromeActivity', '~> 1.0'
pod 'ARSafariActivity', '~> 0.0.1'
pod 'NJKWebViewProgress', '~> 0.2'
end
10 changes: 9 additions & 1 deletion JBWebViewController/JBWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#import <NJKWebViewProgress/NJKWebViewProgress.h>
#import <NJKWebViewProgress/NJKWebViewProgressView.h>

typedef enum : NSUInteger {
JBWebViewTitleModeDefault=0,
JBWebViewTitleModeNative,
} JBWebViewTitleMode;

@interface JBWebViewController : UIViewController <UIWebViewDelegate, NJKWebViewProgressDelegate>

// Typedef for completion block
Expand All @@ -27,9 +32,10 @@ typedef void (^completion)(JBWebViewController *controller);
// Public variables
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, assign) BOOL hideAddressBar;

@property (nonatomic) JBWebViewTitleMode mode;
// Public header methods
- (id)initWithUrl:(NSURL *)url;
- (id)initWithUrl:(NSURL *)url mode:(JBWebViewTitleMode)mode;
- (void)show;
- (void)showFromController:(UIViewController*)controller;
- (void)dismiss;
Expand All @@ -42,6 +48,8 @@ typedef void (^completion)(JBWebViewController *controller);
- (void)navigateToURL:(NSURL *)url;
- (void)loadRequest:(NSURLRequest *)request;

- (void)showFromNavigationController:(UINavigationController*)navigationController;

// Public return methods
- (NSString *)getWebTitle;
- (NSString *)getWebSubtitle;
Expand Down
156 changes: 117 additions & 39 deletions JBWebViewController/JBWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ @implementation JBWebViewController
#pragma mark - "Standards"

- (id)initWithUrl:(NSURL *)url {
if (self = [self init]) {
// Set url and init views
_url = url;
[self setup];
if (self = [self initWithUrl:url mode:JBWebViewTitleModeDefault]) {
}

// Return self
return self;
}

- (id)initWithUrl:(NSURL *)url mode:(JBWebViewTitleMode)mode
{
if (self = [self init]) {
// Set url and init views
_url = url;
_mode = mode;
[self setup];
}

// Return self
return self;
}

- (void)viewDidLoad {
// Standard super class stuff
[super viewDidLoad];
Expand Down Expand Up @@ -94,33 +104,48 @@ - (void)setup {
self.edgesForExtendedLayout = UIRectEdgeTop;

// Create title & subtitle labels
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[_titleLabel setBackgroundColor:[UIColor clearColor]];
[_titleLabel setTextColor:[UIColor blackColor]];
[_titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[_titleLabel setTextAlignment:NSTextAlignmentNatural];
[_titleLabel setText:_loadingString];
[_titleLabel sizeToFit];

_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 0, 0)];
[_subtitleLabel setBackgroundColor:[UIColor clearColor]];
[_subtitleLabel setTextColor:[UIColor blackColor]];
[_subtitleLabel setFont:[UIFont systemFontOfSize:12]];
[_subtitleLabel setTextAlignment:NSTextAlignmentLeft];
[_subtitleLabel setText:[self getDomainFromString:[NSString stringWithFormat:@"%@", _url]]];
[_subtitleLabel sizeToFit];

// Correct frame sizes after sizeToFit
[self adjustNavigationbar];

// Add new titleview with labels
_titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
[_titleView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

[_titleView addSubview:_titleLabel];
[_titleView addSubview:_subtitleLabel];
switch (_mode) {
case JBWebViewTitleModeDefault:
{
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[_titleLabel setBackgroundColor:[UIColor clearColor]];
[_titleLabel setTextColor:[UIColor blackColor]];
[_titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[_titleLabel setTextAlignment:NSTextAlignmentNatural];
[_titleLabel setText:_loadingString];
[_titleLabel sizeToFit];

_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 0, 0)];
[_subtitleLabel setBackgroundColor:[UIColor clearColor]];
[_subtitleLabel setTextColor:[UIColor blackColor]];
[_subtitleLabel setFont:[UIFont systemFontOfSize:12]];
[_subtitleLabel setTextAlignment:NSTextAlignmentLeft];
[_subtitleLabel setText:[self getDomainFromString:[NSString stringWithFormat:@"%@", _url]]];
[_subtitleLabel sizeToFit];

// Correct frame sizes after sizeToFit
[self adjustNavigationbar];

// Add new titleview with labels
_titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
[_titleView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

[_titleView addSubview:_titleLabel];
[_titleView addSubview:_subtitleLabel];
self.navigationItem.titleView = _titleView;
break;
}
case JBWebViewTitleModeNative:
{
_titleLabel = nil;
_subtitleLabel = nil;
_titleView = nil;
break;
}
default:
break;
}

self.navigationItem.titleView = _titleView;

// Inset right buttons
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Share"] style:UIBarButtonItemStylePlain target:self action:@selector(share)];
Expand Down Expand Up @@ -190,7 +215,10 @@ - (void)showFromController:(UIViewController*)controller WithCompletion:(complet
}
}];
}

- (void)showFromNavigationController:(UINavigationController *)navigationController
{
[navigationController pushViewController:self animated:YES];
}
#pragma mark - "Navigation"

- (void)navigateToURL:(NSURL *)url {
Expand Down Expand Up @@ -245,9 +273,15 @@ - (void)share {
}

- (void)dismiss {
if (self.navigationController.viewControllers.count>1) {
//Is push from user-defined navigationController
[self.navigationController popViewControllerAnimated:YES];
}
else{
[self dismissViewControllerAnimated:YES completion:^{
// Code
}];
}
}

#pragma mark - "Navigationbar"
Expand Down Expand Up @@ -311,26 +345,70 @@ - (void)updateNavigationButtons {

- (void)setWebTitle:(NSString *)title {
// Set title & update frame
[_titleLabel setText:title];
[_titleLabel sizeToFit];
[self adjustNavigationbar];
switch (_mode) {
case JBWebViewTitleModeNative:
{
self.title = title;
break;
}
case JBWebViewTitleModeDefault:
{
[_titleLabel setText:title];
[_titleLabel sizeToFit];
[self adjustNavigationbar];
break;
}
default:
break;
}

}

- (void)setWebSubtitle:(NSString *)subtitle {
// Set subtitle & update frame
[_subtitleLabel setText:subtitle];
[_subtitleLabel sizeToFit];
[self adjustNavigationbar];
switch (_mode) {
case JBWebViewTitleModeNative:
{
break;
}
case JBWebViewTitleModeDefault:
{
[_subtitleLabel setText:subtitle];
[_subtitleLabel sizeToFit];
[self adjustNavigationbar];
break;
}
default:
break;
}

}

// Get title
- (NSString *)getWebTitle {
return _titleLabel.text;
switch (_mode) {
case JBWebViewTitleModeDefault:
return _titleLabel.text;
break;
case JBWebViewTitleModeNative:
return self.title;
break;
default:
return nil;
break;
}
}

// Get subtitle
- (NSString *)getWebSubtitle {
return _subtitleLabel.text;
switch (_mode) {
case JBWebViewTitleModeDefault:
return _subtitleLabel.text;
break;
default:
return nil;
break;
}
}

#pragma mark - "Helpers"
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ Feel free to add your app to the list.
## How to use
JBWebViewController is ment to be shown modally, which is recommended to be down with it's built in show functionality. Whilst not being recommended, it is however possible to present JBWebViewController outside a modal view controller. JBWebViewController should always be connected to a UINavigationController.

#### Presenting JBWebViewController
#### Presenting JBWebViewController in default mode
```objectivec
JBWebViewController *controller = [[JBWebViewController alloc] initWithUrl:[NSURL URLWithString:@"http://www.apple.com/iphone/"]];

[controller show];
```

#### Presenting JBWebViewController in Native mode
```objectivec
JBWebViewController* webController = [[JBWebViewController alloc] initWithUrl:[NSURL URLWithString:@"http://www.apple.com/iphone/"] mode:JBWebViewTitleModeNative];
[webController showFromNavigationController:self.navigationController];
```
#### Presenting JBWebViewController with block
```objectivec
JBWebViewController *controller = [[JBWebViewController alloc] initWithUrl:[NSURL URLWithString:@"http://www.apple.com/iphone/"]];
Expand Down