IOS Programming UIWebView 2, iosuiwebview

Source: Internet
Author: User

IOS Programming UIWebView 2, iosuiwebview

IOS Programming UIWebView

1 Instances of UIWebView render web content.

UIWebView can display web content.

In fact, the Safari application on your device uses a UIWebView to render its web content.

In fact, Safari application uses a UIWebView to display its web content.

In this part of the chapter, you will create a view controller whose view is an instance of UIWebView.

When one of the items is selected from the table view of courses, you will push the web view's controller onto the navigation stack and have it load the URL string stored in the NSDictionary.

When an item is selected from table view of courses, you push the controller of the web view to the navigation stack to load the URL stored in NSDictionary.

Create a new NSObject subclass and name it BNRWebViewController. In BNRWebViewController. h, add a property and change the superclass to UIViewController:

@ Interface BNRWebViewController: UIViewController

@ Property (nonatomic) NSURL * URL;

@ End

 

In BNRWebViewController. m, write the following implementation.

-(Void) loadView

{

UIWebView * webView = [[UIWebView alloc] init];

WebView. scalesPageToFit = YES;

Self. view = webView;

}

-(Void) setURL :( NSURL *) URL

{

_ URL = URL;

If (_ URL ){

NSURLRequest * req = [NSURLRequest requestWithURL: _ URL];

[(UIWebView *) self. view loadRequest: req];

}

}

@ End

 

In BNRCoursesViewController. h, add a new property to hang on to an instance of BNRWebViewController.

@ Class BNRWebViewController;

@ Interface BNRCoursesViewController: UITableViewController

@ Property (nonatomic) BNRWebViewController * webViewController;

@ End

In BNRAppDelegate. m, import the header for BNRWebViewController, create an instance

BNRWebViewController, and set it as the BNRWebViewController of the BNRCoursesViewController.

# Import "BNRWebViewController. h"

 

BNRWebViewController * wvc = [[BNRWebViewController alloc] init];

Cvc. webViewController = wvc;

 

In BNRCoursesViewController. m, import the header files for BNRWebViewController and then implement tableView: didSelectRowAtIndexPath: to configure and push the webViewController onto the navigation stack when a row is tapped.

Implement tableView: didSelectRowAtIndexPath to configure and push the webViewController to the navigation stack when the row is clicked.

# Import "BNRWebViewController. h"

-(Void) tableView :( UITableView *) tableView

DidSelectRowAtIndexPath :( NSIndexPath *) indexPath

{

NSDictionary * course = self. courses [indexPath. row];

NSURL * URL = [NSURL URLWithString: course [@ "url"];

Self. webViewController. title = course [@ "title"];

Self. webViewController. URL = URL;

[Self. navigationController pushViewController: self. webViewController animated: YES];

}

2 Credentials qualifications

When you try to access a web service, it will sometimes respond with an authentication challenge, which means "Who the heck are you? "You then need to send a username and password (a credential) before the server will send its genuine response.

When you try to access a web service, it sometimes responds to an authentication challenge, which means: "Who are you ?"

". Before the server sends the original response, you need to send a username and password (a credential ).

 

When the challenge is wrongly Ed, the NSURLSession delegate is asked to authenticate that challenge, and the delegate will respond by supplying a username and password.

When authentication challenge is received, NSURLSession delegate is required to recognize authenticate that challenge. This delegate will respond by providing a username and password.

Open BNRCoursesViewController. m and update fetchFeed to hit a secure Big Nerd Ranch courses web service.

 

NSString * requestString =

@ "Https://bookapi.bignerdranch.com/private/courses.json ";

The NSURLSession now needs its delegate to be set upon creation. Update initWithStyle: to set the delegate of the session.

Now NSURLSession needs to know its delegate to set creation.

_ Session = [NSURLSession sessionWithConfiguration: config

Delegate: self delegateQueue: nil];

 

Then update the class extension in BNRCoursesViewController. m to conform to the NSURLSessionDataDelegate protocol.

 

@ Interface BNRCoursesViewController () <NSURLSessionDataDelegate>

 

To authorize this request, you will need to implement the authentication challenge delegate method.

To authorize this request, you need to implement authentication challenge delegate method.

This method will supply a block that you can call, passing in the credentials as an argument.

This method provides a block you want to call and passes a credentials as the parameter.

In BNRCoursesViewController. m implement the NSURLSessionDataDelegate method to handle the authentication challenge.

-(Void) URLSession :( NSURLSession *) session task :( NSURLSessionTask *) task didReceiveChallenge :( NSURLAuthenticationChallenge *) challenge completionHandler:

(Void (^) (NSURLSessionAuthChallengeDisposition, NSURLCredential *) completionHandler

{
NSURLCredential * cred =

[NSURLCredential credentialWithUser: @ "BigNerdRanch" password: @ "AchieveNerdvana"

Persistence: NSURLCredentialPersistenceForSession]; completionHandler (NSURLSessionAuthChallengeUseCredential, cred );

}

 

The completion handler takes in two arguments.

The first argument is the type of credentials you are supplying.

The first parameter is the credentials type you want to provide.

Since you are supplying a username and password, the type of authentication is NSURLSessionAuthChallengeUseCredential.

Because you need to provide username and password, the authentication type is NSURLSessionAuthChallengeUseCredential.

The second argument is the credentials themselves, an instance of NSURLCredential, which is created with the username, password, and an enumeration specifying how long these credentials shocould be valid.

The second parameter is credentials itself. For an NSURLCredential instance, username, password, and enumeration are created to indicate how long these credentials will be valid.

3 The Request Body

When NSURLSessionTask talks to a web server, it uses the HTTP protocol. This protocol says that any data you send or receive must follow the HTTP specification.

When NSURLSessionTask is associated with a web server, it uses HTTP protocol. This protocol indicates that any data you want to send or receive must comply with HTTP specification.

NSURLRequest has a number of methods that allow you to specify a piece of the request and then properly format it for you.

NSURLRequest has many methods that allow you to specify a part of the request and then use the appropriate format.

Any service request has three parts: a request-line, the HTTP headers, and the HTTP body, which is optional.

Any service request has three parts: request-line, HTTP headers, and HTTP body.

The request-line (which Apple calla status line) is the first line of the request and tells the server what the client is trying to do.

Request-ling (apple called status line) is the first line of the request, telling the server what the client is going to do.

In this request, the client is trying to GET the resource at/courses. json. (It also specifies the HTTP specification version that the data is in .)

In this request, the client intends to get resources in/courses. json.

While there are a number of supported HTTP methods, you most commonly see GET and POST. the default of NSURLRequest, GET, indicates that the client wants something from the server. the thing that it wants is called the Request-URI (/courses. json ).

Today, we also use the Request-URI to specify a service that the server implements.

Now we still use Request-URI to specify the server to implement the specified service.

For example, in this chapter, you accessed the courses service, supplied parameters to it, and were returned a JSON document.

You are still GETting something, but the server is more clever in interpreting what you are asking.

You are still GETting something, but the server will be smarter at interperting.

In addition to getting things from a server, you can send it information. for example, publish web servers allow you to upload photos. A client application wocould pass the image data to the server through a service request.

Another scenario is that you want to send something to the server.

In this situation, you use the HTTP method POST, which indicates to the server that you are including the optional HTTP body.

In this case, you use the HTTP Method POST, which is indicates to the server.

The body of a request is data you can include with the request-typically XML, JSON, or Base-64 encoded data.

The request body is the request data you can include: XML, JSON, or Base-64 encoded data.

When the request has a body, it must also have the Content-Length header.

When a request has a body, it must have a content-length header.

Handily enough, NSURLRequest will compute the size of the body and add this header for you.

Conveniently, NSURLRequest will calculate the body size and add this header for you.

 

NSURL * someURL = [NSURL URLWithString: @ "http://www.photos.com/upload"];

UIImage * image = [self profilePicture];

NSData * data = UIImagePNGRepresentation (image );

 

NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL: someURL

CachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 90];

// This adds the HTTP body data and automatically sets the Content-Length header

Req. HTTPBody = data;

// This changes the HTTP Method in the request-line

Req. HTTPMethod = @ "POST ";

// If you wanted to set the Content-Length programmatically...

[Req setValue: [NSString stringWithFormat: @ "% d", data. length]

ForHTTPHeaderField: @ "Content-Length"];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.