Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » objective-c

UIWebView example (iOS webview) using objective c

December 28, 2022 by Source Freeze 4 Comments

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

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Comments

  • zulfi on iOS UIPickerView Example using Swift
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework

Tags

cross-platform ionic iOS javascript mobile application development nextjs objective-c swift uiwebview Visual Studio plugin

Copyright © 2023 Source Freeze

x