热门标签 | 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


推荐阅读
  • 自动验证时页面显示问题的解决方法
    在使用自动验证功能时,页面未能正确显示错误信息。通过使用 `dump($info->getError())` 可以帮助诊断和解决问题。 ... [详细]
  • Framework7:构建跨平台移动应用的高效框架
    Framework7 是一个开源免费的框架,适用于开发混合移动应用(原生与HTML混合)或iOS&Android风格的Web应用。此外,它还可以作为原型开发工具,帮助开发者快速创建应用原型。 ... [详细]
  • 本文介绍了如何使用 CMD 批处理脚本进行文件操作,包括将指定目录下的 PHP 文件重命名为 HTML 文件,并将这些文件复制到另一个目录。 ... [详细]
  • 本文详细介绍了DMA控制器如何通过映射表处理来自外设的请求,包括映射表的设计和实现方法。 ... [详细]
  • Spark中使用map或flatMap将DataSet[A]转换为DataSet[B]时Schema变为Binary的问题及解决方案
    本文探讨了在使用Spark的map或flatMap算子将一个数据集转换为另一个数据集时,遇到的Schema变为Binary的问题,并提供了详细的解决方案。 ... [详细]
  • 在使用Eclipse进行调试时,如果遇到未解析的断点(unresolved breakpoint)并显示“未加载符号表,请使用‘file’命令加载目标文件以进行调试”的错误提示,这通常是因为调试器未能正确加载符号表。解决此问题的方法是通过GDB的`file`命令手动加载目标文件,以便调试器能够识别和解析断点。具体操作为在GDB命令行中输入 `(gdb) file `。这一步骤确保了调试环境能够正确访问和解析程序中的符号信息,从而实现有效的调试。 ... [详细]
  • MySQL的查询执行流程涉及多个关键组件,包括连接器、查询缓存、分析器和优化器。在服务层,连接器负责建立与客户端的连接,查询缓存用于存储和检索常用查询结果,以提高性能。分析器则解析SQL语句,生成语法树,而优化器负责选择最优的查询执行计划。这一流程确保了MySQL能够高效地处理各种复杂的查询请求。 ... [详细]
  • 两个条件,组合控制#if($query_string~*modviewthread&t(&extra(.*)))?$)#{#set$itid$1;#rewrite^ ... [详细]
  • 解决Win10下MySQL连接问题:Navicat 2003无法连接到本地MySQL服务器(10061)
    本文介绍如何在Windows 10环境下解决Navicat 2003无法连接到本地MySQL服务器的问题,包括启动MySQL服务和检查配置文件的方法。 ... [详细]
  • 本文详细介绍了如何利用Duilib界面库开发窗体动画效果,包括基本思路和技术细节。这些方法不仅适用于Duilib,还可以扩展到其他类似的界面开发工具。 ... [详细]
  • 第二十五天接口、多态
    1.java是面向对象的语言。设计模式:接口接口类是从java里衍生出来的,不是python原生支持的主要用于继承里多继承抽象类是python原生支持的主要用于继承里的单继承但是接 ... [详细]
  • 解决Parallels Desktop错误15265的方法
    本文详细介绍了在使用Parallels Desktop时遇到错误15265的多种解决方案,包括检查网络连接、关闭代理服务器和修改主机文件等步骤。 ... [详细]
  • 解决 Windows Server 2016 网络连接问题
    本文详细介绍了如何解决 Windows Server 2016 在使用无线网络 (WLAN) 和有线网络 (以太网) 时遇到的连接问题。包括添加必要的功能和安装正确的驱动程序。 ... [详细]
  • 使用Jsoup解析并遍历HTML文档时,该库能够高效地生成一个清晰、规范的解析树,即使源HTML文档存在格式问题。Jsoup具备强大的容错能力,能够处理多种异常情况,如未闭合的标签等,确保解析结果的准确性和完整性。 ... [详细]
  • 系统数据实体验证异常:多个实体验证失败的错误处理与分析
    在使用MVC和EF框架进行数据保存时,遇到了 `System.Data.Entity.Validation.DbEntityValidationException` 错误,表明存在一个或多个实体验证失败的情况。本文详细分析了该错误的成因,并提出了有效的处理方法,包括检查实体属性的约束条件、调试日志的使用以及优化数据验证逻辑,以确保数据的一致性和完整性。 ... [详细]
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社区 版权所有