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
{
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"];
iOS Programming UIWebView 2的更多相关文章
- iOS Programming Web Services and UIWebView
iOS Programming Web Services and UIWebView The work is divided into two parts. The first is connecti ...
- iOS Programming State Restoration 状态存储
iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...
- IOS客户端UIwebview下web页面闪屏问题
基于ios客户端uiwebview下的web页面,在其内容高度大于视窗高度时,如果点击超过视窗下文档的底部按钮,收缩内容高度,会发生闪屏问题. 外因是由文档的高度大于视窗的高度所致,本质原因未知. 解 ...
- ios上uiwebview的一些实用技巧
前几个星期接到公司一个项目,要用webview在客户端上播视频,作为一个前端实习生,这种需求真是蛋疼……一不知webview是何方神圣,二不知咋调试…… 下面就是蛋疼的开始: 寻找调试工具:好,非w ...
- iOS中UIWebView执行JS代码(UIWebView)
iOS中UIWebView执行JS代码(UIWebView) 有时候iOS开发过程中使用 UIWebView 经常需要加载网页,但是网页中有很多明显的标记让人一眼就能看出来是加载的网页,而我们又不想被 ...
- Head First iOS Programming
内部分享: Head First iOS Programming http://www.slideshare.net/tedzhaoxa/head-first-ios-programming-4606 ...
- iOS Programming Recipe 6: Creating a custom UIView using a Nib
iOS Programming Recipe 6: Creating a custom UIView using a Nib JANUARY 7, 2013 BY MIKETT 12 COMMENTS ...
- iOS中用UIWebView的loadHTMLString后图片和文字失调解决方法
iOS中用UIWebView的loadHTMLString后图片和文字失调,图片过大,超过屏幕,文字太小.或者图片太小.文字太大,总之就是不协调. 我们的需求是让图片的大小跟着屏幕的变化而变化.就是动 ...
- iOS Programming Autorotation, Popover Controllers, and Modal View Controllers
iOS Programming Autorotation, Popover Controllers, and Modal View Controllers 自动旋转,Popover 控制器,Moda ...
随机推荐
- DRF的认证,频率,权限
1,DRF的认证 初识认证:浏览器是无状态的,一次导致每次发的请求都是新的请求,所以每次请求,服务器都会进行校验,这样就很繁琐,这趟我们就需要给每一个用户登录后一个新的标识,浏览器每次都会带着这个唯一 ...
- my.cnf配置详解[转载]
先粘贴一份mac下的mysql5.6.22的配置文件 # Example MySQL config file for medium systems. # # This is for a system ...
- codeforces 682C C. Alyona and the Tree(dfs)
题目链接: C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input ...
- bzoj 2169 连边 —— DP+容斥
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2169 就和这篇博客说的一样:https://blog.csdn.net/WerKeyTom_ ...
- 《Spring Boot官方指南》(二)入门
如果您刚开始接触Spring Boot,或者简称’Spring’, 这个部分对您来说非常有用.这个部分提供了“是什么?”,“怎么做?”和 “为什么?”的解释.您除了会阅读一份附带Spring Boot ...
- Python+页面元素高亮源码实例
简单写了一个页面元素高亮的方法,原理就是在python中调用js实现元素高亮,分享一下源码如下: 1.元素高亮源码 Js调用 js = "var q=document.getElementB ...
- HDU2262;Where is the canteen(高斯消元+期望)
传送门 题意 给出一张图,LL从一个点等概率走到上下左右位置,询问LL从宿舍走到餐厅的步数期望 分析 该题是一道高斯消元+期望的题目 难点在于构造矩阵,我们发现以下结论 设某点走到餐厅的期望为Ek 1 ...
- bzoj 3206: [Apio2013]道路费用【最小生成树+并查集】
参考:http://hzwer.com/6888.html 把k条道路权值设为0,和其他边一起跑MST,然后把此时选中的其他边设为必选,在新图中加上必选变缩成k个点,把所有边重标号,枚举k跳边的选取情 ...
- poj 3281 Dining【最大流】
记得把牛拆掉!拆成两个点i和i'在中间连一条流量为1的边,来限制每头牛只能选一组 一般来讲是一种物品一个消费者各占一边,但是这里有两个物品,所以考虑把牛放在中间,s向所有的食物连流量为1的边,所有食物 ...
- Luogu P2327 [SCOI2005]扫雷【递推/数学】By cellur925
题目传送门 推了好久啊.看来以后要多玩扫雷了qwq. 其实本题只有三种答案:0.1.2. 对于所有第一列,只要第一个数和第二个数确定后,其实整个数列就确定了,我们可以通过这个递推式得出 sec[i-] ...