Advertisement
priore

a nice Web Browser with basic UI not invasive functions

Apr 22nd, 2012
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  WebBrowserLite.h
  3. //
  4. //  Created by Danilo Priore on 21/04/12.
  5. //  Copyright (c) 2012 Prioregroup.com. All rights reserved.
  6. //
  7.  
  8. // a nice Web Browser with basic UI not invasive functions
  9. // import "WebBrowserLite.h"
  10. // NSURL *url = [NSURL URLWithString:@"https://www.facebook.com/pages/Prioregroupcom/161635751419"];
  11. // WebBrowserLite *webBrowser = [[WebBrowserLite alloc] initWithURL:url];
  12. // [webBrowser show];
  13.  
  14. //
  15. //  Permission is hereby granted, free of charge, to any person obtaining a copy
  16. //  of this software and associated documentation files (the "Software"), to deal
  17. //  in the Software without restriction, including without limitation the rights
  18. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. //  copies of the Software, and to permit persons to whom the Software is
  20. //  furnished to do so, subject to the following conditions:
  21. //
  22. //  The above copyright notice and this permission notice shall be included in
  23. //  all copies or substantial portions of the Software.
  24. //
  25. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  31. //  THE SOFTWARE.
  32. //
  33. //
  34.  
  35. #import <UIKit/UIKit.h>
  36.  
  37. @protocol WebBrowserLiteDelegate;
  38.  
  39. @interface WebBrowserLite : UIViewController
  40.  
  41. @property (nonatomic, assign) id<WebBrowserLiteDelegate> delegate;
  42.  
  43. - (id)initWithFrame:(CGRect)frame;
  44. - (id)initWithURL:(NSURL*)url;
  45.  
  46. - (void)show;
  47. - (void)hide;
  48. - (void)loadURL:(NSURL*)url;
  49.  
  50. @end
  51.  
  52. @protocol WebBrowserLiteDelegate <NSObject>
  53.  
  54. @optional
  55.  
  56. - (BOOL)webBrowserLite:(WebBrowserLite*)webBrowserLite viewDidClose:(id)sender;
  57.  
  58. @end
  59.  
  60. //
  61. //  WebBrowserLite.m
  62. //
  63. //  Created by Danilo Priore on 21/04/12.
  64. //  Copyright (c) 2012 Prioregroup.com. All rights reserved.
  65. //
  66.  
  67. // a nice Web Browser with basic UI not invasive functions
  68. // import "WebBrowserLite.h"
  69. // NSURL *url = [NSURL URLWithString:@"https://www.facebook.com/pages/Prioregroupcom/161635751419"];
  70. // WebBrowserLite *webBrowser = [[WebBrowserLite alloc] initWithURL:url];
  71. // [webBrowser show];
  72.  
  73. //
  74. //  Permission is hereby granted, free of charge, to any person obtaining a copy
  75. //  of this software and associated documentation files (the "Software"), to deal
  76. //  in the Software without restriction, including without limitation the rights
  77. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  78. //  copies of the Software, and to permit persons to whom the Software is
  79. //  furnished to do so, subject to the following conditions:
  80. //
  81. //  The above copyright notice and this permission notice shall be included in
  82. //  all copies or substantial portions of the Software.
  83. //
  84. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  85. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  86. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  87. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  88. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  89. //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  90. //  THE SOFTWARE.
  91. //
  92. //
  93.  
  94. #define BUTTON_MARGIN   5.0f
  95. #define BUTTON_SIZE     CGSizeMake(30, 30)
  96.  
  97. #import <QuartzCore/QuartzCore.h>
  98. #import "WebBrowserLite.h"
  99.  
  100. @interface WebBrowserLite() <UIWebViewDelegate>
  101. {
  102.     UIWebView *webView;
  103.    
  104.     UIImageView *lockView;
  105.    
  106.     UIButton *backButton;
  107.     UIButton *forwardButton;
  108.    
  109.     UIView *activity;
  110.     UIActivityIndicatorView *spinner;
  111. }
  112.  
  113. - (void)hideToLeft:(BOOL)toLeft hidden:(BOOL)hidden view:(UIView*)view;
  114. - (void)hideActivityIndicator:(BOOL)hidden;
  115. - (UIViewController *)getCurrentRootViewController;
  116.  
  117. @end
  118.  
  119. @implementation WebBrowserLite
  120.  
  121. @synthesize delegate;
  122.  
  123. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  124.    
  125.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  126.         self.view.frame = [UIScreen mainScreen].bounds;
  127.         self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  128.     }
  129.    
  130.     return self;
  131.    
  132. }
  133.  
  134. - (id)initWithFrame:(CGRect)frame {
  135.    
  136.     if (self = [super init]) {
  137.         self.view.frame = frame;
  138.     }
  139.    
  140.     return self;
  141. }
  142.  
  143. - (id)initWithURL:(NSURL*)url {
  144.    
  145.     if (self = [self init]) {
  146.         self.view.frame = [UIScreen mainScreen].bounds;
  147.         self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  148.         [self loadURL:url];
  149.     }
  150.    
  151.     return self;
  152. }
  153.  
  154. - (void)viewDidLoad {
  155.    
  156.     CGRect frame = self.view.frame;
  157.     self.view.autoresizesSubviews = YES;
  158.     self.view.contentMode = UIViewContentModeRedraw;
  159.     self.view.backgroundColor = [UIColor clearColor];
  160.    
  161.     // webview full view
  162.     webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  163.     webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  164.     webView.scalesPageToFit = YES;
  165.     webView.delegate = self;
  166.     [self.view addSubview:webView];
  167.    
  168.     // close button on top right
  169.     UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  170.     closeButton.frame = CGRectMake(frame.size.width - BUTTON_SIZE.width - BUTTON_MARGIN, BUTTON_MARGIN, BUTTON_SIZE.width, BUTTON_SIZE.height);
  171.     closeButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0.1 alpha:0.7];
  172.     closeButton.showsTouchWhenHighlighted = YES;
  173.     closeButton.layer.borderColor = [UIColor whiteColor].CGColor;
  174.     closeButton.layer.borderWidth = 1;
  175.     closeButton.layer.cornerRadius = 10;
  176.     closeButton.layer.shadowColor = [UIColor blackColor].CGColor;
  177.     closeButton.layer.shadowOpacity = 0.5;
  178.     closeButton.layer.shadowOffset = CGSizeMake(0, -1);
  179.     closeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
  180.     [closeButton setImage:[UIImage imageNamed:@"WebBrowserLite.bundle/close.png"] forState:UIControlStateNormal];
  181.     [closeButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  182.     [self.view addSubview:closeButton];
  183.    
  184.     // lock image for https pages (initial out of view)
  185.     lockView = [[UIImageView alloc] initWithFrame:CGRectMake(-BUTTON_SIZE.width, BUTTON_MARGIN, BUTTON_SIZE.width, BUTTON_SIZE.height)];
  186.     lockView.contentMode = UIViewContentModeCenter;
  187.     lockView.image = [UIImage imageNamed:@"WebBrowserLite.bundle/lock.png"];
  188.     lockView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0.1 alpha:0.7];
  189.     lockView.layer.borderColor = [UIColor whiteColor].CGColor;
  190.     lockView.layer.borderWidth = 1;
  191.     lockView.layer.cornerRadius = 10;
  192.     lockView.layer.shadowColor = [UIColor blackColor].CGColor;
  193.     lockView.layer.shadowOpacity = 0.5;
  194.     lockView.layer.shadowOffset = CGSizeMake(0, -1);
  195.     lockView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
  196.     [self.view addSubview:lockView];
  197.    
  198.     // back button on bottom left (initial out of view)
  199.     backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  200.     backButton.frame = CGRectMake(-BUTTON_SIZE.width, frame.size.height - BUTTON_SIZE.height - BUTTON_MARGIN, BUTTON_SIZE.width, BUTTON_SIZE.height);
  201.     backButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0.1 alpha:0.7];
  202.     backButton.showsTouchWhenHighlighted = YES;
  203.     backButton.layer.borderColor = [UIColor whiteColor].CGColor;
  204.     backButton.layer.borderWidth = 1;
  205.     backButton.layer.cornerRadius = 10;
  206.     backButton.layer.shadowColor = [UIColor blackColor].CGColor;
  207.     backButton.layer.shadowOpacity = 0.5;
  208.     backButton.layer.shadowOffset = CGSizeMake(0, -1);
  209.     backButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  210.     [backButton setImage:[UIImage imageNamed:@"WebBrowserLite.bundle/arrow-left.png"] forState:UIControlStateNormal];
  211.     [backButton addTarget:webView action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
  212.     [self.view addSubview:backButton];
  213.    
  214.     // forward button on bottom right (initial out of view)
  215.     forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
  216.     forwardButton.frame = CGRectMake(frame.size.width, backButton.frame.origin.y, BUTTON_SIZE.width, BUTTON_SIZE.height);
  217.     forwardButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0.1 alpha:0.7];
  218.     forwardButton.showsTouchWhenHighlighted = YES;
  219.     forwardButton.layer.borderColor = [UIColor whiteColor].CGColor;
  220.     forwardButton.layer.borderWidth = 1;
  221.     forwardButton.layer.cornerRadius = 10;
  222.     forwardButton.layer.shadowColor = [UIColor blackColor].CGColor;
  223.     forwardButton.layer.shadowOpacity = 0.5;
  224.     forwardButton.layer.shadowOffset = CGSizeMake(0, -1);
  225.     forwardButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
  226.     [forwardButton setImage:[UIImage imageNamed:@"WebBrowserLite.bundle/arrow-right.png"] forState:UIControlStateNormal];
  227.     [forwardButton addTarget:webView action:@selector(goForward) forControlEvents:UIControlEventTouchUpInside];
  228.     [self.view addSubview:forwardButton];
  229.    
  230.     // activity indicator container (initial hidden)
  231.     activity = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
  232.     activity.alpha = 0;
  233.     activity.opaque = NO;
  234.     activity.layer.cornerRadius = 10;      
  235.     activity.autoresizesSubviews = YES;
  236.     activity.userInteractionEnabled = NO;
  237.     activity.layer.shadowColor = [UIColor blackColor].CGColor;
  238.     activity.layer.shadowOpacity = 0.5;
  239.     activity.layer.shadowOffset = CGSizeMake(0, -1);
  240.     activity.center = CGPointMake(frame.size.width / 2, frame.size.height / 2);
  241.     activity.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0.1 alpha:0.7];
  242.     activity.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |  UIViewAutoresizingFlexibleTopMargin |  UIViewAutoresizingFlexibleBottomMargin;     
  243.     [self.view addSubview:activity];
  244.    
  245.     // activity indicator
  246.     spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  247.     spinner.center = CGPointMake(activity.frame.size.width / 2, activity.frame.size.height / 2);
  248.     [activity addSubview:spinner];
  249.  
  250. }
  251.  
  252. - (void)show {
  253.     // show view
  254.     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(show) object:nil];
  255.     UIViewController *root = [self getCurrentRootViewController];
  256.     [UIView beginAnimations:nil context:nil];
  257.     [UIView setAnimationDuration:1.0];
  258.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
  259.                            forView:root.view
  260.                              cache:YES];
  261.     [root.view addSubview:self.view];
  262.     [UIView commitAnimations];
  263. }
  264.  
  265. - (void)hide {
  266.     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hide) object:nil];
  267.  
  268.     // generate the delegate and check if stop default animation
  269.     if (delegate != nil && [delegate respondsToSelector:@selector(webBrowserLite:viewDidClose:)]) {
  270.         BOOL ret = [delegate webBrowserLite:self viewDidClose:self];
  271.         if (!ret) return;
  272.     }
  273.    
  274.     UIViewController *root = [self getCurrentRootViewController];
  275.     [UIView beginAnimations:nil context:nil];
  276.     [UIView setAnimationDuration:1.0];
  277.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
  278.                            forView:root.view
  279.                              cache:YES];
  280.     [self.view removeFromSuperview];
  281.     [UIView commitAnimations];
  282. }
  283.  
  284. - (void)loadURL:(NSURL *)url {
  285.     // start navigation
  286.     NSURLRequest *request = [NSURLRequest requestWithURL:url];
  287.     [webView loadRequest:request];
  288. }
  289.  
  290. - (void)hideToLeft:(BOOL)toLeft hidden:(BOOL)hidden view:(UIView*)view {
  291.    
  292.     // show/hide back button with animation
  293.     BOOL visible = CGRectContainsPoint(self.view.bounds, view.frame.origin);
  294.     if (visible && hidden) {
  295.         // hide to left
  296.         [UIView beginAnimations:nil context:nil];
  297.         [UIView setAnimationDuration:0.4];
  298.         CGFloat x = toLeft ? -BUTTON_SIZE.width - BUTTON_MARGIN : BUTTON_SIZE.width + BUTTON_MARGIN;
  299.         view.transform = CGAffineTransformMakeTranslation(x,  0);
  300.         [UIView commitAnimations];
  301.     } else if (!visible && !hidden) {
  302.         // show from left
  303.         [UIView beginAnimations:nil context:nil];
  304.         [UIView setAnimationDuration:0.4];
  305.         CGFloat x = toLeft ? BUTTON_SIZE.width + BUTTON_MARGIN : -BUTTON_SIZE.width - BUTTON_MARGIN;
  306.         view.transform = CGAffineTransformMakeTranslation(x, 0);
  307.         [UIView commitAnimations];
  308.     }
  309. }
  310.  
  311. - (void)hideActivityIndicator:(BOOL)hidden {
  312.     // show/hide activity indicator
  313.     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideActivityIndicator) object:nil];
  314.    
  315.     [spinner startAnimating];
  316.     [UIView beginAnimations:nil context:NULL];
  317.     [UIView setAnimationBeginsFromCurrentState:YES];
  318.     [UIView setAnimationDuration:0.1];
  319.    
  320.     if (hidden) {
  321.         // stop spinner after end animation
  322.         [UIView setAnimationDelegate:spinner];
  323.         [UIView setAnimationDidStopSelector:@selector(stopAnimating)];
  324.     }
  325.  
  326.     activity.alpha = hidden ? 0 : 1;
  327.     [UIView commitAnimations];
  328. }
  329.  
  330. - (UIViewController *)getCurrentRootViewController {
  331.    
  332.     UIViewController *result = nil;
  333.    
  334.     // Try to find the root view controller programmically
  335.    
  336.     // Find the top window (that is not an alert view or other window)
  337.     UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
  338.     if (topWindow.windowLevel != UIWindowLevelNormal)
  339.     {
  340.         NSArray *windows = [[UIApplication sharedApplication] windows];
  341.         for(topWindow in windows)
  342.         {
  343.             if (topWindow.windowLevel == UIWindowLevelNormal)
  344.                 break;
  345.         }
  346.     }
  347.    
  348.     UIView *rootView = [[topWindow subviews] objectAtIndex:0]
  349.     id nextResponder = [rootView nextResponder];
  350.    
  351.     if ([nextResponder isKindOfClass:[UIViewController class]])
  352.         result = nextResponder;
  353.    
  354.     else
  355.         NSAssert(NO, @"WebBrowserLite: Could not find a root view controller.");
  356.    
  357.     return result;    
  358. }
  359.  
  360. #pragma mark - WebView Delegate
  361.  
  362. - (void)webViewDidStartLoad:(UIWebView *)aWebView {
  363.     [self hideActivityIndicator:NO];
  364. }
  365.  
  366. - (void)webViewDidFinishLoad:(UIWebView *)aWebView {
  367.     [self hideActivityIndicator:YES];
  368. }
  369.  
  370. - (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error {
  371.     [self hideActivityIndicator:YES];
  372. }
  373.  
  374. - (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  375.    
  376.     // show/hide back/forward buttons
  377.     [self hideToLeft:YES hidden:!aWebView.canGoBack view:backButton];
  378.     [self hideToLeft:NO hidden:!aWebView.canGoForward view:forwardButton];
  379.    
  380.     // show/hide lock image for secure pages
  381.     NSURL *url = request.URL;
  382.     [self hideToLeft:YES hidden:![url.scheme isEqual:@"https"] view:lockView];
  383.    
  384.     return YES;
  385. }
  386.  
  387. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  388. {
  389.     // support full device orientation
  390.     return YES;
  391. }
  392.  
  393. - (void)dealloc {
  394.    
  395.     [webView release];
  396.     [lockView release];
  397.     [backButton release];
  398.     [forwardButton release];
  399.     [activity release];
  400.     [spinner release];
  401.     [super dealloc];
  402. }
  403.  
  404. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement