How to create UIWebView Programatically in iPhone development

Here we are going to create UIWebView programatically,here is code for your reference , as we know webview use to load html strings and url load, Apple provides UIWebView to achieve this task,

-(void)InitWebView
{
// initialize  and create the UIWebView
UIWebView  *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight |                 UIViewAutoresizingFlexibleWidth);

//set the web view delegates for the web view to be itself
aWebView.delegate = self;

//Set the URL to go to for your UIWebView
NSString *urlAddress = @”http://www.google.com”;

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//load the URL into the web view.
[aWebView loadRequest:requestObj];

//add the web view to the content view
[self.view addSubview:aWebView];
}
// delegate methods to handle webview results

– (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@” Request Failed”);
}
– (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@”Web View Finished Loading”);
}
– (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@” Handle URL Request”);
return YES;
}

NOTE: Make sure you have added UIWebViewDelegate in your interface header file like this
@interface yourclassname <UIWebViewDelegate>
{
}
@end
and dont forget to set reference to webview delegate , where you want to implement those method.
yourWebView.delegate = self;

Feedback expected , and if you have any queries feel free to contact me

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *