Search This Blog

Thursday, November 28, 2013

How to Implement Web View


UIWebView

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController
{
    IBOutlet UIWebView *webView1;
    IBOutlet UITextField *txtField;

}
-(IBAction)goClick:(id)sender;

@end



.m

#import "ViewController.h"
@implementation ViewController


-(IBAction)goClick:(id)sender;
{
    NSString *str = txtField.text;
    NSString *urlString;
    NSRange range = [str rangeOfString:@"http://"];
    if(range.location == NSNotFound)
    {
        urlString = [NSString stringWithFormat:@"http://%@",str];
     }
    else
        urlString = str;
    
    txtField.text = urlString;
    
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView1  loadRequest:request];
    [webView1 setScalesPageToFit:YES];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidStartLoad called");
    

}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   NSLog(@"webViewDidFinishLoad called");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Error"message:[error localizedDescriptiondelegate:nilcancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
  NSLog(@"didFailLoadWithError called and error info is %@",[error userInfo]);
}

-(void)viewDidLoad
{
    //Url loading in the WebView
    NSString *urlString = @"http://www.google.com";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView1 setDelegate:self];
    [webView1  loadRequest:request];
    [webView1 setScalesPageToFit:YES];
    

    /*
    //HTML Content
    [webView1 loadHTMLString:@"<html><body><h1>Hello World</h1></body></html>" baseURL:nil];
    

    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"b" ofType:@"png"];
    [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
    
    */
}

@end

No comments:

Post a Comment