Tuesday, 27 August 2013

Introduction to UIWebView

Why we are using UIWebView ??


Provides a view to embed web content. A UIWebView object can be attached to a window, and sent requests to load web content. This class can also be used to move back and forward in the history of webpages, and set some web content properties programmatically.

OutPut of the example code are shown in below image





Programmatically create a UIWebView


 UIWebView *view = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,320,480)];
    NSString *url=@"http://www.google.com";  // URL that user want to open,currently its a string.

    NSURL *nsurl=[NSURL URLWithString:url]; // Converting that String to URL format

    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
// Making a URL request with current URL

    [view loadRequest:nsrequest];
//Loading that URL page in WebView 


    [self.view addSubview:view];

// Adding that WebView to our View 





Below contents are from Apple docs 


Overview

You use the UIWebView class to embed web content in your application. To do so, you simply create a UIWebView object, attach it to a window, and send it a request to load web content. You can also use this class to move back and forward in the history of webpages, and you can even set some web content properties programmatically.
Use the loadRequest: method to begin loading web content, the stopLoading method to stop loading, and the loading property to find out if a web view is in the process of loading.
If you allow the user to move back and forward through the webpage history, then you can use the goBack and goForward methods as actions for buttons. Use the canGoBack and canGoForward properties to disable the buttons when the user can’t move in a direction.
By default, a web view automatically converts telephone numbers that appear in web content to Phone links. When a Phone link is tapped, the Phone application launches and dials the number. Set the detectsPhoneNumbers property to NO to turn off this default behavior.
You can also use the scalesPageToFit property to programmatically set the scale of web content the first time it is displayed in a web view. Thereafter, the user can change the scale using gestures.
Set the delegate property to an object conforming to the UIWebViewDelegate protocol if you want to track the loading of web content.
You can easily debug the HTML, CSS, and JavaScript contained inside a UIWebView with Web Inspector. Read “Debugging Web Content on iOS” to learn how to configure Web Inspector for iOS. Read the rest of Safari Web Content Guide to learn how to create web content that is optimized for Safari on iPhone and iPad.
For information about basic view behaviors, see View Programming Guide for iOS.

Supported File Formats

In addition to HTML content, UIWebView objects can be used to display other content types. For more information, see Using UIWebView to display select document types.

State Preservation

In iOS 6 and later, if you assign a value to this view’s restorationIdentifier property, it attempts to preserve its URL history, the scaling and scrolling positions for each page, and information about which page is currently being viewed. During restoration, the view restores these values so that the web content appears just as it did before. For more information about how state preservation and restoration works, see iOS App Programming Guide.

Subclassing Notes

The UIWebView class should not be subclassed.

Tasks

Setting the Delegate

Loading Content

Moving Back and Forward

Setting Web Content Properties

Running JavaScript

Detecting Types of Data

Managing Media Playback

Properties

allowsInlineMediaPlayback

A Boolean value that determines whether HTML5 videos play inline or use the native full-screen controller.
@property(nonatomic) BOOL allowsInlineMediaPlayback
Discussion
The default value on iPhone is NO.
In order for video to play inline, not only does this property need to be set on the view, but the video element in the HTML document must also include the webkit-playsinline attribute.
Availability
  • Available in iOS 4.0 and later.
Declared In
UIWebView.h

canGoBack

A Boolean value indicating whether the receiver can move backward. (read-only)
@property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack
Discussion
If YES, able to move backward; otherwise, NO.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

canGoForward

A Boolean value indicating whether the receiver can move forward. (read-only)
@property(nonatomic, readonly, getter=canGoForward) BOOL canGoForward
Discussion
If YES, able to move forward; otherwise, NO .
Availability
  • Available in iOS 2.0 and later.
See Also
Declared In
UIWebView.h

dataDetectorTypes

The types of data converted to clickable URLs in the web view’s content.
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes
Discussion
You can use this property to specify the types of data (phone numbers, http links, email address, and so on) that should be automatically converted to clickable URLs in the web view. When clicked, the web view opens the application responsible for handling the URL type and passes it the URL.
Availability
  • Available in iOS 3.0 and later.
Declared In
UIWebView.h

delegate

The receiver’s delegate.
@property(nonatomic, assign) id<UIWebViewDelegate> delegate
Discussion
The delegate is sent messages when content is loading. See UIWebViewDelegate Protocol Reference for the optional methods this delegate may implement.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

keyboardDisplayRequiresUserAction

A Boolean value indicating whether web content can programmatically display the keyboard.
@property (nonatomic) BOOL keyboardDisplayRequiresUserAction
Discussion
When this property is set to YES, the user must explicitly tap the elements in the web view to display the keyboard (or other relevant input view) for that element. When set to NO, a focus event on an element causes the input view to be displayed and associated with that element automatically.
The default value for this property is YES.
Availability
  • Available in iOS 6.0 and later.
Declared In
UIWebView.h

loading

A Boolean value indicating whether the receiver is done loading content. (read-only)
@property(nonatomic, readonly, getter=isLoading) BOOL loading
Discussion
If YES, the receiver is still loading content; otherwise, NO.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

mediaPlaybackAllowsAirPlay

A Boolean value that determines whether Air Play is allowed from this view.
@property(nonatomic) BOOL mediaPlaybackAllowsAirPlay
Discussion
The default value on both iPad and iPhone is YES.
Availability
  • Available in iOS 5.0 and later.
Declared In
UIWebView.h

mediaPlaybackRequiresUserAction

A Boolean value that determines whether HTML5 videos can play automatically or require the user to start playing them.
@property(nonatomic) BOOL mediaPlaybackRequiresUserAction
Discussion
The default value on both iPad and iPhone is YES.
Availability
  • Available in iOS 4.0 and later.
Declared In
UIWebView.h

request

The URL request identifying the location of the content to load. (read-only)
@property(nonatomic, readonly, retain) NSURLRequest *request
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

scalesPageToFit

A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.
@property(nonatomic) BOOL scalesPageToFit
Discussion
If YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

scrollView

The scroll view associated with the web view. (read-only)
@property(nonatomic, readonly, retain) UIScrollView *scrollView
Discussion
Your application can access the scroll view if it wants to customize the scrolling behavior of the web view.
Availability
  • Available in iOS 5.0 and later.
Declared In
UIWebView.h

suppressesIncrementalRendering

A Boolean value indicating whether the web view suppresses content rendering until it is fully loaded into memory.
@property(nonatomic) BOOL suppressesIncrementalRendering
Discussion
When set to YES, the web view does not attempt to render incoming content as it arrives. Instead, the view’s current contents remain in place until all of the new content has been received, at which point the new content is rendered. This property does not affect the rendering of content retrieved after a frame finishes loading.
The value of this property is NO by default.
Availability
  • Available in iOS 6.0 and later.
Declared In
UIWebView.h

Instance Methods

goBack

Loads the previous location in the back-forward list.
- (void)goBack
Availability
  • Available in iOS 2.0 and later.
See Also
Declared In
UIWebView.h

goForward

Loads the next location in the back-forward list.
- (void)goForward
Availability
  • Available in iOS 2.0 and later.
See Also
Declared In
UIWebView.h

loadData:MIMEType:textEncodingName:baseURL:

Sets the main page contents, MIME type, content encoding, and base URL.
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL
Parameters
data
The content for the main page.
MIMEType
The MIME type of the content.
encodingName
The IANA encoding name as in utf-8 or utf-16.
baseURL
The base URL for the content.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

loadHTMLString:baseURL:

Sets the main page content and base URL.
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
Parameters
string
The content for the main page.
baseURL
The base URL for the content.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

loadRequest:

Connects to a given URL by initiating an asynchronous client request.
- (void)loadRequest:(NSURLRequest *)request
Parameters
request
A URL request identifying the location of the content to load.
Discussion
To stop this load, use the stopLoading method. To see whether the receiver is done loading the content, use the loading property.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

reload

Reloads the current page.
- (void)reload
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

stopLoading

Stops the loading of any web content managed by the receiver.
- (void)stopLoading
Discussion
Stops any content in the process of being loaded by the main frame or any of its children frames. Does nothing if no content is being loaded.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

stringByEvaluatingJavaScriptFromString:

Returns the result of running a script.
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
Parameters
script
The script to run.
Return Value
The result of running script or nil if it fails.
Discussion
JavaScript execution time is limited to 10 seconds for each top-level entry point. If your script executes for more than 10 seconds, the web view stops executing the script. This is likely to occur at a random place in your code, so unintended consequences may result. This limit is imposed because JavaScript execution may cause the main thread to block, so when scripts are running, the user is not able to interact with the webpage.
JavaScript allocations are also limited to 10 MB. The web view raises an exception if you exceed this limit on the total memory allocation for JavaScript.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

Constants

UIWebViewNavigationType

Constant indicating the user’s action.
enum {
   UIWebViewNavigationTypeLinkClicked,
   UIWebViewNavigationTypeFormSubmitted,
   UIWebViewNavigationTypeBackForward,
   UIWebViewNavigationTypeReload,
   UIWebViewNavigationTypeFormResubmitted,
   UIWebViewNavigationTypeOther
};
typedef NSUInteger UIWebViewNavigationType;
Constants
UIWebViewNavigationTypeLinkClicked
User tapped a link.
Available in iOS 2.0 and later.
Declared in UIWebView.h.
UIWebViewNavigationTypeFormSubmitted
User submitted a form.
Available in iOS 2.0 and later.
Declared in UIWebView.h.
UIWebViewNavigationTypeBackForward
User tapped the back or forward button.
Available in iOS 2.0 and later.
Declared in UIWebView.h.
UIWebViewNavigationTypeReload
User tapped the reload button.
Available in iOS 2.0 and later.
Declared in UIWebView.h.
UIWebViewNavigationTypeFormResubmitted
User resubmitted a form.
Available in iOS 2.0 and later.
Declared in UIWebView.h.
UIWebViewNavigationTypeOther
Some other action occurred.
Available in iOS 2.0 and later.
Declared in UIWebView.h.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIWebView.h

No comments:

Post a Comment