IOS Mobile Phone simulates a simple browser

Source: Internet
Author: User

We append four buttons to the toolbar, and call the goBack method, goForward method, reload method, and stopLoading method in sequence ., Basically, we can implement simple browser functions. The most important thing is to use the knowledge point of UIWebView. Now we will find that what we used to feel is actually so simple ., Well, do you increase your write confidence.

I will share with you the code below. If you do not understand it, please read the official documentation or Baidu.

The Code is as follows:

HHLAppDelegate. h

#import 
 
  @class HHLViewController;@interface HHLAppDelegate : UIResponder 
  
   @property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) HHLViewController *viewController;@property (strong,nonatomic) UINavigationController *myNaVC;@end
  
 

HHLAppDelegate. m

#import "HHLAppDelegate.h"#import "HHLViewController.h"@implementation HHLAppDelegate- (void)dealloc{    [_window release];    [_viewController release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];    UINavigationController *pNaVC = [[UINavigationController alloc]initWithRootViewController:self.viewController];    self.myNaVC = pNaVC;    [pNaVC release];            self.window.rootViewController = self.myNaVC;    [self.window makeKeyAndVisible];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

HHLViewController. h

#import 
 
  @interface HHLViewController : UIViewController
  
   {@private    UIWebView *myWebView;    //UIActivityIndicatorView *myIndicatorView;    UIBarButtonItem *reloadButton;    UIBarButtonItem *stopButton;    UIBarButtonItem *backbutton;    UIBarButtonItem *forwardButton;}@end
  
 

HHLViewController. m

# Import "HHLViewController. h "@ interface HHLViewController () @ end @ implementation HHLViewController-(void) dealloc {// [myIndicatorView release]; if (myWebView. loading) {[myWebView stopLoading];} myWebView. delegate = nil; // recommended in the Apple documentation. You need to write [myWebView release]; [reloadButton release]; [stopButton release]; [backbutton release] before release. [forwardButton release]; [super dealloc];}-(void) viewDidLoad {[su Per viewDidLoad]; self. title = @ "UIWebView test"; myWebView = [[UIWebView alloc] init]; myWebView. delegate = self; myWebView. frame = self. view. bounds; myWebView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; myWebView. scalesPageToFit = YES; // set the scalePageToFit attribute of UIWebView to YES, so that the Web page will automatically scale according to the screen size. [Self. view addSubview: myWebView]; [self. navigationController setNavigationBarHidden: NO animated: YES]; [self. navigationController setToolbarHidden: NO animated: YES]; // append an activity indicator to the toolbar // myIndicatorView = [[UIActivityIndicatorView alloc] init]; // myIndicatorView. frame = CGRectMake (0, 0, 50, 50); // UIBarButtonItem * indicator = [[[UIBarButtonItem alloc] initWithCustomView: myIndicatorView] autorelease]; // UIBarButtonItem * adjustment = [[UIBarButtonItem alloc] lower: lower target: nil action: nil] autorelease]; // NSArray * buttons = [NSArray lower: adjustment, indicator, adjustment, nil]; // [self setToolbarItems: buttons animated: YES]; // Add the button reloadButton In the toolbar = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: Custom target: self action: @ selector (reloadDidPush)]; stopButton = [[UIBarButtonItem alloc] Destination: Destination target: self action: @ selector (stopDidPush)]; backbutton = [[UIBarButtonItem alloc] initWithTitle: @ "Back" style: UIBarButtonItemStyleBordered target: self action: @ selector (backDidPush)]; forwardButton = [[UIBarButtonItem alloc] initWithTitle: @ "Forward" style: alert target: self action: @ selector (forwardDidPush)]; NSArray * buttons = [NSArray upload: backbutton, forwardButton, reloadButton, stopButton, nil]; [self setToolbarItems: buttons animated: YES];} -(void) reloadDidPush {[myWebView reload]; // reload the page}-(void) stopDidPush {if (myWebView. loading) {[myWebView stopLoading]; // stop reading}-(void) backDidPush {if (myWebView. canGoBack) {[myWebView goBack]; // return to the previous page}-(void) forwardDidPush {if (myWebView. canGoForward) {[myWebView goForward]; // on the next page}-(void) viewDidAppear :( BOOL) animated {[super viewDidAppear: animated]; // NSURLRequest * myRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http://www.baidu.com/"]; [myWebView loadRequest: myRequest]; [self updateViewConstraints];} -(void) viewWillDisappear :( BOOL) animated {// when the screen is closed, the status bar event prompt is set to OFF [super viewWillDisappear: animated]; [UIApplication sharedApplication]. response = NO;}-(void) webViewDidStartLoad :( UIWebView *) webView {// [myIndicatorView startAnimating]; [self updateViewConstraints];}-(void) webViewDidFinishLoad :( UIWebView *) webView {// [myIndicatorView stopAnimating]; [self updateViewConstraints];} //-(void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) error {// [myIndicatorView stopAnimating]; [self updateViewConstraints];}-(void) handle {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end

The effect after running is as follows:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.