diff --git a/Example/Podfile b/Example/Podfile index 406fd4b..94ca5ae 100644 --- a/Example/Podfile +++ b/Example/Podfile @@ -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 diff --git a/JBWebViewController/JBWebViewController.h b/JBWebViewController/JBWebViewController.h index e495eab..b0817f1 100644 --- a/JBWebViewController/JBWebViewController.h +++ b/JBWebViewController/JBWebViewController.h @@ -16,6 +16,11 @@ #import #import +typedef enum : NSUInteger { + JBWebViewTitleModeDefault=0, + JBWebViewTitleModeNative, +} JBWebViewTitleMode; + @interface JBWebViewController : UIViewController // Typedef for completion block @@ -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; @@ -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; diff --git a/JBWebViewController/JBWebViewController.m b/JBWebViewController/JBWebViewController.m index c536021..49b7179 100644 --- a/JBWebViewController/JBWebViewController.m +++ b/JBWebViewController/JBWebViewController.m @@ -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]; @@ -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)]; @@ -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 { @@ -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" @@ -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" diff --git a/README.md b/README.md index 0ffbfc4..0f04cab 100644 --- a/README.md +++ b/README.md @@ -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/"]];