Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home ยป UIWebView example (iOS webview) using objective c

UIWebView example (iOS webview) using objective c

December 28, 2022 by Source Freeze Leave a Comment

Some applications need to display online web content, for iOS development that needs “UIWebView” to display online sites as well as  local html files or even displaying a PDF documents we will use UIWebview. In this tutorial we will see how to create “uiwebview example” using objective-c.

Open XCode, create a single view application using File –> New –> Project, select as universal project.

create webview

then enter the project name, Organization and other fields like below.

webview project

we can add the webview into viewcontroller in two ways. First – to create programmatically and Second – by using storyboard. Initially, we shall discuss about creating  uiwebview example using story board.At right bottom of xcode you can find  utility area and select object library, drag and drop  the uiwebview control into storyboard viewcontroller page.

uiwebview load url

now uiwebview gets added into viewcontroller and the storyboard view will looks as below.

uiwebview storyboard

then add the uiwebview reference into viewcontroller.h file by clicking the assistant editor,

Screen Shot 2014-07-10 at 10.04.57 pm

then control + click the storyboard uiwebview then drag into viewcontroller.h file and place just below @interface, one  editor window will open like below. just enter the webview name and click connect.

Screen Shot 2014-07-10 at 2.36.09 am

uiwebview load url :

open the viewcontroller.m file and add the below code snippets into viewDidLoad method.

   
   [super viewDidLoad];
    NSString *urlString = @"http://www.sourcefreeze.com";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:urlRequest];

select the iphone simulator Build and Run  the application you should see the requested url loaded into uiwebview, you can run the same  application in iPad simulator also.

uiwebview iphone simulator

at the top of the right bar click the show attribute inspector you can see the ios webview properties, select  Scales Page To Fit  which automatically scale the webpage which is inserted into the view it shows entire site instead of zoomed view. Detection section which is used to analyse content having any links, address and phone number and events(adds into calendar).  change the uiwebview background using Background  section.

show ios properties.

UIWebView load local file

we have seen how to load online url, let’s we will see about how to load local html files into uiwebview. create new file by right click and add New file, name it as index.html file. add some html content in the index.html file. then add  the below code snippet into load local html file method Build and Run the project.

/* load local html file 

[super viewDidLoad]; 
NSString *localURL = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:nil]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:localURL]]; 
[_webView loadRequest:urlRequest];

Create uiwebview programmatically

using the below code you can create uiwebview programmatically and add into view,

[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
UIWebView *webView = [[UIWebView alloc]init]; 
NSString *urlString = @"http://www.sourcefreeze.com"; NSURL *url = [NSURL URLWithString:urlString]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
[webView loadRequest:urlRequest]; 
[self.view addSubview:webView];

Build and Run the application you can get the same output as earlier.

Reached the end of the tutorial, for your reference you can download the entire Xcode project here. leave me a comment still if you have any questions.

Filed Under: ios-tutorial Tagged With: objective-c

Leave a Reply Cancel reply

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

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Comments

    Tags

    beginner colon cross-platform es6 function html ionic iOS javascript mobile application development nextjs objective-c swift switch ternary typescript uiwebview Visual Studio plugin web developer

    Copyright © 2025 Source Freeze