热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOSProgrammingUIWebView2

iOSProgrammingUIWebView1InstancesofUIWebViewrenderwebcontent.UIWebView可以显示webcontent。Infac

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

?

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


推荐阅读
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
author-avatar
尕心疼TammyY
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有