你的位置:首页 > 软件开发 > 操作系统 > iOS Programming UIWebView 2

iOS Programming UIWebView 2

发布时间:2015-05-26 00:00:58
iOS Programming  UIWebView 1 Instances of UIWebView render web content. UIWebView可以显示web content。In fact, the Safari application on ...

iOS Programming  UIWebView  2

iOS Programming  UIWebView

1 Instances of UIWebView render web content.

UIWebView可以显示web content。

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

事实上,Safari application 用了一个UIWebView 显示它的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.

当item的一个从table view of courses 被选中,你push the  web view的controller 到navigation stack,让它加载存储在NSDictionary中得URL。

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 of

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.

实现tableView:didSelectRowAtIndexPath来配置和推送webViewController到navigation stack 当row 被点击时。

#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资格

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.

当你尝试access  a web service,它有时候响应一个authentication challenge 意思是说:"你到底是谁

"。在server 发送本来的响应之前,你需要发送一个username and password(a credential).

 

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

当authentication challenge 被收到后,NSURLSession delegate 被要求认authenticate that challenge,这个delegate 将会通过提供一个username 和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.

NSURLSession 现在需要知道它的delegate来设置创造。

_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.

为了authorize 这个request,你需要实现authentication challenge delegate method.

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

这个方**提供了一个你要调用的block,传递一个credentials 作为参数。

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

{

原标题:iOS Programming UIWebView 2

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。