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

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

第一个参数是你要提供的credentials的类型。

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

因为你要提供username 和password,authentication 的类型是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 should be valid for.

第二个参数是credentials 自身,一个NSURLCredential 实例,将创建username ,password和一个enumeration 指明这些credentials 将有效多长时间。

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.

当NSURLSessionTask 与一个web server,它使用HTTP protocol.这个协议说你要发送或接受的任何数据需要遵守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有许多方法允许你指定请求的一个片段然后恰当的format 。

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

任何service request 有三个部分:request-line,HTTP headers 和HTTP body 。

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

request-ling(apple 称为status line) 是请求的第一行,告诉server ,client打算做什么。

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

在这个请求里client 打算get 资源在/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.

现在我们仍然用Request-URI来指明server 实现指定的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 for.

你仍然在GETting something,但是server 将会更聪明在interperting .

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

另外一种情况是你想发送给server一些东西。

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

在这种情况下,你使用HTTP 方法POST ,这就indicates  to the server 你将including 可选择的HTTP body.

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

request body 是你能包括的请求数据:XML,JSON,或者Base-64 encoded data.

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

当request 有body时,它必须有content-length header。

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

方便的是,NSURLRequest 将会计算body 的大小并把为你添加这个header.

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"];

转载于:https://www.cnblogs.com/ljlkfx/p/4527489.html

iOS Programming UIWebView 2相关推荐

  1. iOS programming Code Snippet Library

    iOS programming  Code Snippet Library The freebie code comes from the code snippet library. 代码来自code ...

  2. IOS开发 - UIWebView(用法全面介绍,含最全的JS交互)

    前两天我写了一套关于JS与OC交互的三种方法的文章,但是发现自己对UIWebView理解的还是比较浅,一直认为只能简单的当一个webView来使用,具体很多细节都忽略,如果想看我之前UIWebView ...

  3. iOS 翻译-UIWebView的基本简介-官方文档翻译

    继承关系:NSObject-UIResponder-UIView-UIWebView 遵循:NSCoding NSObject UIAppearance UIAppearanceContainer U ...

  4. iOS判断UIWebView加载完成的方法

    UIWebview不同于WkWebview没有加载进度属性来判断加载是否完成了 它有个回调方法 - (void)webViewDidFinishLoad:(UIWebView *)webView; 这 ...

  5. iOS设置UIWebView的UserAgent

    接入第三方时,别人又需求,要求传入我们的信息. // 获取 iOS 默认的 UserAgent,可以很巧妙地创建一个空的UIWebView来获取:NSString *userAgent = [[[UI ...

  6. uiwebview 编辑html5,【iOS】UIWebView HTML5 扩展

    对于不少iOS开发者来说,HTML5的内容比较陌生. 尤其是UIWebView类的stringByEvaluatingJavaScriptFromString 方法 让很多人觉得又得学一种新的语言. ...

  7. iOS关于UIWebView和Javascript相互调用及WebViewJavascriptBridge分析

    iOS中 JS与OC通信 1,    基本原理. iOS中展示html文档的控件为UIWebView和WKWebView,都可以直接执行js代码:但是JS不能直接执行OC的代码,好在UIWebView ...

  8. iOS中UIWebview中网页宽度自适应的问题

    有的网页中会使用"<meta name="viewport" content="width=device-width, initial-scale=1.0 ...

  9. iOS:UIWebView scrollView 的分页滑动问题

    最近在弄一个native webview+html的项目,感觉这种尝试还是挺不错的,特被是适合内容类app.如杂志.电子书等.其实native搭的就是一个框架,主体还是在html的内容上,所以花在ht ...

最新文章

  1. R语言使用pie函数可视化饼图(pie chart)、为饼图添加百分比信息、使用plotrix包可视化3D饼图、使用plotrix包可视化扇形饼图
  2. ArcEngine编辑功能的实现(二)
  3. 如何提升各开发角色效率,既保证产品质量又能快速上线?
  4. boost::fusion::result_of::empty用法的测试程序
  5. js 判断 undefined,单选 以及下拉框选中状态
  6. android 启动器开发,Android启动器(Launcher)开发详解
  7. Highcharts 配置选项详细说明
  8. 工业以太网交换机的重要技术参数分析
  9. Jenkins的安装和卸载(转载)
  10. 开环增益的频率依赖性
  11. 《延世大学韩国语教程2》第十九课 生病(下)
  12. Smoothy将于4月22日-27日在BSCPad等4个平台进行IDO
  13. AMFPHP基本安全问题
  14. Linux进程、线程、任务调度(1)贵在坚持
  15. TML5期末大作业:美丽家乡网站设计——美丽家乡-含论文(4页) HTML+CSS+JavaScript web前端设计与开发期末作品/期末大作业
  16. 北邮计算机考研科目2022,2022考研:北京邮电大学计算机专业考研经验复习指导...
  17. 免费WiFi上网软件是什么?怎么用?
  18. Flixel横板游戏制作教程(八)—MovingPlatforms(移动平台)
  19. 【数据分析】- 操作步骤大总结
  20. three.js重新计算UV

热门文章

  1. oracle EXP-00028
  2. jquery源码解析:proxy,access,swap,isArraylike详解
  3. mysql数据库,当数据类型是float时,查询居然查询不出数据来
  4. IOSelect模块
  5. 织梦后台对应的php文件,织梦DedeCMS后台文件列表按文件名排序的方法
  6. devops的重要性_为什么DevOps是当今最重要的技术战略
  7. 苹果开发者修改团队名称_鼓励您的团队对项目进行更改的5步计划
  8. 压缩包加密后门_加密后门:还有更多争论吗?
  9. 好的物联网开源框架_通过开源文化实现更好的物联网
  10. 贪吃蛇C语言源码与算法分析