iOS UIWebView 全属性详解(苹果官方文档翻译整理)

版权声明:本文为博主原创文章,未经博主允许不得转载。原创不易,各位勉之。 https://blog.csdn.net/LL845876425/article/details/51884736

iOS UIWebView 全属性详解(苹果官方文档翻译整理)

前言

我这次之所以特意对这个看起来比较稀松平常的控件进行详细说明呢,是基于这么几点考虑的:首先,我们都知道这段时间HTML5是特别的火,对于移动端开发的冲击还是蛮大的,(各位要是对HTML5不是很了解的话,请点击HTML5 百度百科进行查看。)那么对于移动端来说,HTML5要想在移动端进行使用,那么必然需要一个载体,这个载体毋庸置疑的就是UIWebVIew了。所以我们就有必要对UIWebVIew有一个全面并且清晰的认识了。
其次之前对于UIWebVIew的另一大重要用途就是js的混编使用,实现更为高级的交互使用,这一块由于我之前并没有相关的需求,所以了解的不是很多,在这里就不多说了。
最后那就是苹果官方的前言说明了:开发者可以使用UIWebView类在自己的应用程序中嵌入web内容。为此,创建一个UIWebView对象,将它附加到一个窗口,并将其发送请求加载web内容。您还可以使用这个类来推动,在网页的历史,甚至可以以编程方式设置一些web内容属性。
另外我这次将与UIWebVIew交互的几个枚举放到了文章的最前方,主要是由于在我个人的开发经历中,我发现这几个变量还是有很大的作用的,是与UIWebVIew的基本交互。

支持文件格式

除了HTML内容,UIWebView对象可以用来显示其他内容类型,如主题、PDF、和页面文件。最好的平原和富文本的呈现在你的应用程序,但是,您应该使用UITextView代替。

Data Types

UIWebViewNavigationType

Constant indicating the user’s action.
常数说明用户的行动。

SWIFT

enum UIWebViewNavigationType : Int {
    case LinkClicked
    case FormSubmitted
    case BackForward
    case Reload
    case FormResubmitted
    case Other
}

OBJECTIVE-C

enum {
   UIWebViewNavigationTypeLinkClicked,
   UIWebViewNavigationTypeFormSubmitted,
   UIWebViewNavigationTypeBackForward,
   UIWebViewNavigationTypeReload,
   UIWebViewNavigationTypeFormResubmitted,
   UIWebViewNavigationTypeOther
};

UIWebViewNavigationTypeLinkClicked
User tapped a link.(用户利用一个链接。)
UIWebViewNavigationTypeFormSubmitted
User submitted a form.(用户提交表单。)
UIWebViewNavigationTypeBackForward
User tapped the back or forward button.(用户利用后退或前进按钮。)
UIWebViewNavigationTypeReload
User tapped the reload button.(用户利用重载按钮。)
UIWebViewNavigationTypeFormResubmitted
User resubmitted a form.(用户提交表单。)
UIWebViewNavigationTypeOther
Some other action occurred.(其他一些行动发生。)

Constants (常量)

UIWebPaginationBreakingMode

The manner in which column- or page-breaking occurs.
列或分页符的方式发生。
SWIFT

enum UIWebPaginationBreakingMode : Int {
    case Page
    case Column
}

OBJECTIVE-C

typedef NS_ENUM (NSInteger,
   UIWebPaginationBreakingMode ) {
   UIWebPaginationBreakingModePage,
   UIWebPaginationBreakingModeColumn 
};

UIWebPaginationBreakingModePage
Content respects CSS properties related to page-breaking.(内容方面分页符相关的CSS属性。)
UIWebPaginationBreakingModeColumn
Content respects CSS properties related to column-breaking.(内容方面与column-breaking相关的CSS属性。)

UIWebPaginationMode

The layout of content in the web view, which determines the direction that the pages flow.
在web视图的布局内容,决定了页面流的方向。

SWIFT

enum UIWebPaginationMode : Int {
    case Unpaginated
    case LeftToRight
    case TopToBottom
    case BottomToTop
    case RightToLeft
}

OBJECTIVE-C

typedef NS_ENUM (NSInteger,
   UIWebPaginationMode ) {
   UIWebPaginationModeUnpaginated,
   UIWebPaginationModeLeftToRight,
   UIWebPaginationModeTopToBottom,
   UIWebPaginationModeBottomToTop,
   UIWebPaginationModeRightToLeft 
};

UIWebPaginationModeUnpaginated
Content appears as one long scrolling view with no distinct pages.(内容显示为一个长的滚动视图没有不同的页面。)
UIWebPaginationModeLeftToRight
Content is broken up into pages that flow from left to right.(内容分为页面流从左到右。)
UIWebPaginationModeTopToBottom
Content is broken up into pages that flow from top to bottom.
UIWebPaginationModeBottomToTop
Content is broken up into pages that flow from bottom to top.
UIWebPaginationModeRightToLeft
Content is broken up into pages that flow from right to left.

UIWebView 常用方法

- loadData:MIMEType:textEncodingName:baseURL:

设置主页内容、MIME类型、内容编码,基本URL。

Sets the main page contents, MIME type, content encoding, and base URL.

SWIFT
   func loadData(_ data: NSData,
        MIMEType MIMEType: String,
textEncodingName textEncodingName: String,
         baseURL baseURL: NSURL)
OBJECTIVE-C
- (void)loadData:(NSData *)data
        MIMEType:(NSString *)MIMEType
textEncodingName:(NSString *)encodingName
         baseURL:(NSURL *)baseURL

- loadHTMLString:baseURL:

Sets the main page content and base URL.

设置主页内容和基本URL。

SWIFT
func loadHTMLString(_ string: String,
            baseURL baseURL: NSURL?)
OBJECTIVE-C
- (void)loadHTMLString:(NSString *)string
               baseURL:(NSURL *)baseURL

为了帮助你避免容易受到安全攻击,一定要使用这个方法来加载本地HTML文件;不要使用loadRequest:。


- loadRequest:

Connects to a given URL by initiating an asynchronous client request.

连接到一个给定的URL启动异步客户机请求。

SWIFT

func loadRequest(_ request: NSURLRequest)

OBJECTIVE-C

- (void)loadRequest:(NSURLRequest *)request

不要用这种方法来加载本地HTML文件;相反,使用loadHTMLString:baseURL:。停止这个负载,使用stopLoading方法。接收方是否完成加载内容,使用加载属性。


request

The URL request identifying the location of the content to load. (read-only)

URL请求识别的位置加载内容。(只读)

SWIFT

var request: NSURLRequest? { get }

OBJECTIVE-C

@property(nonatomic, readonly, strong) NSURLRequest *request

loading

A Boolean value indicating whether the receiver is done loading content. (read-only)

一个布尔值,指示是否接收方完成加载内容。(只读)

SWIFT

var loading: Bool { get }

OBJECTIVE-C

@property(nonatomic, readonly, getter=isLoading) BOOL loading

If YES, the receiver is still loading content; otherwise, NO.如果是的,接收机还加载内容;否则,没有。


- stopLoading

Stops the loading of any web content managed by the receiver.

停止加载的web内容管理的接收器。

SWIFT

func stopLoading()

OBJECTIVE-C

- (void)stopLoading

Stops any content in the process of being loaded by the main frame or any of its children frames. Does nothing if no content is being loaded.停止任何内容的过程中由主框架加载或它的任何孩子帧。没有如果没有被加载内容。


- reload

Reloads the current page.

重新加载当前页面。

SWIFT

func reload()

OBJECTIVE-C

- (void)reload

canGoBack

A Boolean value indicating whether the receiver can move backward. (read-only)

一个布尔值表示接收方是否能向后移动。(只读)

SWIFT

var canGoBack: Bool { get }

OBJECTIVE-C

@property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack

If YES, able to move backward; otherwise, NO.如果是的,能向后移动;否则,没有。


canGoForward

A Boolean value indicating whether the receiver can move forward. (read-only)

一个布尔值表示接收方是否可以前进。(只读)

SWIFT

var canGoForward: Bool { get }

OBJECTIVE-C

@property(nonatomic, readonly, getter=canGoForward) BOOL canGoForward

If YES, able to move forward; otherwise, NO .如果是的,能够前进;否则,没有。


- goBack

Loads the previous location in the back-forward list.

加载之前回到前面列表中的位置。

SWIFT

func goBack()

OBJECTIVE-C

- (void)goBack

- goForward

Loads the next location in the back-forward list.

回到前面的加载下一个位置列表。

SWIFT

func goForward()

OBJECTIVE-C

- (void)goForward

allowsLinkPreview

A Boolean value that determines whether pressing on a link displays a preview of the destination for the link.

一个布尔值来决定是否紧迫的一个链接显示的目的地的预览链接。

SWIFT

var allowsLinkPreview: Bool

OBJECTIVE-C

@property(nonatomic) BOOL allowsLinkPreview

This property is available on devices that support 3D Touch. Default value is NO.

If you set this value to YES for a web view, users (with devices that support 3D Touch) can preview link destinations, and can preview detected data such as addresses, by pressing on links. Such previews are known to users as peeks. If a user presses deeper, the preview navigates (or pops, in user terminology) to the destination. Because pop navigation switches the user from your app to Safari, it is opt-in, by way of this property, rather default behavior for this class.

If you want to support link preview but also want to keep users within your app, you can switch from using the UIWebView class to the SFSafariViewController class. If you are using a web view as an in-app browser, making this change is best practice. The Safari view controller class automatically supports link previews.

此属性可用设备上支持3d Touch。默认值是否定的。如果你将这个值设置为YES web视图,用户(设备支持3 d)可以预览链接的目的地,并且可以预览检测数据,如地址,按上链接。这些预览用户窥探。如果用户按下更深,预览导航(或持久性有机污染物,在用户的术语)的目的地。因为流行导航切换用户从应用程序到Safari,选择,这个属性,而这类的默认行为。如果你想支持链接预览,还想让用户在你的应用程序,您可以切换使用UIWebView类SFSafariViewController类。如果您正在使用一个web浏览器视图作为应用内,这种变化是最佳实践。Safari视图控制器类自动支持链接预览。


scalesPageToFit

A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.

一个布尔值判断网页规模以适应视图,用户可以改变的规模。

SWIFT

var scalesPageToFit: Bool

OBJECTIVE-C

@property(nonatomic) BOOL scalesPageToFit

If YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.
如果是,网页缩放以适合,用户可以放大和缩小。如果没有,用户缩放是禁用的。默认值是否定的。

scrollView

The scroll view associated with the web view. (read-only)

滚动视图与web相关的观点。(只读)

SWIFT

var scrollView: UIScrollView { get }

OBJECTIVE-C

@property(nonatomic, readonly, strong) UIScrollView *scrollView

Your app can access the scroll view if it wants to customize the scrolling behavior of the web view.

你的应用可以访问滚动视图如果它想自定义滚动网页视图的行为。


suppressesIncrementalRendering

A Boolean value indicating whether the web view suppresses content rendering until it is fully loaded into memory.

一个布尔值,指示是否web视图抑制内容呈现,直到它完全被加载到内存中。

SWIFT

var suppressesIncrementalRendering: Bool

OBJECTIVE-C

@property(nonatomic) BOOL suppressesIncrementalRendering

When set to YES, the web view does not attempt to render incoming content as it arrives. Instead, the view’s current contents remain in place until all of the new content has been received, at which point the new content is rendered. This property does not affect the rendering of content retrieved after a frame finishes loading.The value of this property is NO by default.
当设置为YES,web视图并不试图呈现的内容,因为它的到来。相反,当前视图的内容仍然存在,直到所有的新内容已经收到,此时呈现新内容。这个属性不会影响渲染帧完成加载后检索的内容。这个属性的值是没有默认情况下。


keyboardDisplayRequiresUserAction

A Boolean value indicating whether web content can programmatically display the keyboard.

一个布尔值表示web内容是否能以编程方式显示键盘。

SWIFT

var keyboardDisplayRequiresUserAction: Bool

OBJECTIVE-C

@property(nonatomic) BOOL keyboardDisplayRequiresUserAction

When this property is set to YES, the user must explicitly tap the elements in the web view to display the keyboard (or other relevant input view) for that element. When set to NO, a focus event on an element causes the input view to be displayed and associated with that element automatically.
The default value for this property is YES.
当这个属性被设置为YES,用户必须显式地利用web视图中的元素来显示键盘(或其他相关输入视图)的元素。当设置为不,关注事件对一个元素使输入视图显示并自动与元素相关联。这个属性的默认值是肯定的。


dataDetectorTypes

The types of data converted to clickable URLs in the web view’s content.

类型的数据转换为可点击的网址在web视图的内容。

SWIFT

var dataDetectorTypes: UIDataDetectorTypes

OBJECTIVE-C


@property(nonatomic) UIDataDetectorTypes dataDetectorTypes

Use this property to specify the types of data (phone numbers, HTTP links, email address, and so on) that should be automatically converted to clickable URLs in the web view. When clicked, the web view opens the app responsible for handling the URL type and passes it the URL.
See the UIDataDetectorTypes enumeration for the types of data available for automatic detection.
使用这个属性来指定类型的数据(HTTP链接,电话号码,电子邮件地址,等等)应该自动转换为web视图中点击url。当点击时,web视图中打开应用程序负责处理URL类型和通过它的URL。看到的UIDataDetectorTypes枚举类型的数据可以自动检测。


运行JavaScript

- stringByEvaluatingJavaScriptFromString:

Returns the result of running a JavaScript script. Although this method is not deprecated, best practice is to use the evaluateJavaScript:completionHandler: method of the WKWebView class instead.

返回运行JavaScript脚本的结果。虽然这种方法不是弃用,最佳实践是使用evaluateJavaScript:completionHandler:WKWebView类的方法。

SWIFT

func stringByEvaluatingJavaScriptFromString(_ script: String) -> String?

OBJECTIVE-C

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

==The stringByEvaluatingJavaScriptFromString: method waits synchronously for JavaScript evaluation to complete. If you load web content whose JavaScript code you have not vetted, invoking this method could hang your app. Best practice is to adopt the WKWebView class and use its evaluateJavaScript:completionHandler: method instead.==

==stringByEvaluatingJavaScriptFromString:同步等待JavaScript方法评估完成。如果你加载JavaScript代码的web内容没有审查,调用这个方法可以挂你的应用,最佳实践是采用WKWebView类和使用其evaluateJavaScript:completionHandler:方法代替。==


Managing Media Playback (管理媒体播放)

allowsInlineMediaPlayback

A Boolean value that determines whether HTML5 videos play inline or use the native full-screen controller.

一个布尔值来决定是否HTML5视频玩内联或使用本机全屏控制器。

SWIFT

var allowsInlineMediaPlayback: Bool

OBJECTIVE-C

@property(nonatomic) BOOL allowsInlineMediaPlayback

The default value on iPhone is NO.
In order for video to play inline, not only does this property need to be set on the view, but the video element in the HTML document must also include the webkit-playsinline attribute.
iPhone上的默认值是否定的。为了使视频玩内联,不仅需要设置这个属性视图,但是视频元素的HTML文档必须包含webkit-playsinline属性。

mediaPlaybackRequiresUserAction

A Boolean value that determines whether HTML5 videos can play automatically or require the user to start playing them.
一个布尔值来决定是否HTML5视频可以自动或要求用户开始播放。

SWIFT

var mediaPlaybackRequiresUserAction: Bool

OBJECTIVE-C

@property(nonatomic) BOOL mediaPlaybackRequiresUserAction

The default value on both iPad and iPhone is YES. To make media play automatically when loaded, set this property to NO and ensure the or element you want to play has the autoplay attribute set.
该值在iPhone与iPad上默认设置为YES,使媒体播放时自动加载。

mediaPlaybackAllowsAirPlay

A Boolean value that determines whether Air Play is allowed from this view.
该布尔值决定了是否允许来自视图的进行播放

SWIFT
var mediaPlaybackAllowsAirPlay: Bool
OBJECTIVE-C
@property(nonatomic) BOOL mediaPlaybackAllowsAirPlay

The default value on both iPad and iPhone is YES.

allowsPictureInPictureMediaPlayback

A Boolean value that determines whether Picture in Picture playback is allowed from this view.
一个布尔值,确定是否允许从画中画播放这一观点。

SWIFT
var allowsPictureInPictureMediaPlayback: Bool
OBJECTIVE-C
@property(nonatomic) BOOL allowsPictureInPictureMediaPlayback

The default value is YES on devices that support Picture in Picture (PiP) mode and NO on all other devices.

Available in iOS 9.0 and later.

Managing Pages

gapBetweenPages

The size of the gap, in points, between pages.
之间的差距的大小,点页面。

SWIFT
var gapBetweenPages: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat gapBetweenPages

The default value is 0.
Available in iOS 7.0 and later.

pageCount

The number of pages produced by the layout of the web view. (read-only)
由WebVIew布局的页面数量

SWIFT
var pageCount: Int { get }
OBJECTIVE-C
@property(nonatomic, readonly) NSUInteger pageCount

Available in iOS 7.0 and later.

pageLength

The size of each page, in points, in the direction that the pages flow.
每个页面的大小,在点,在页面流的方向。

SWIFT
var pageLength: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat pageLength

Discussion

When paginationMode is right to left or left to right, this property represents the width of each page. When paginationMode is top to bottom or bottom to top, this property represents the height of each page.

The default value is 0, which means the layout uses the size of the viewport to determine the dimensions of the page. Adjusting the value of this property causes a relayout.
当paginationMode右到左或从左到右,这个属性代表了每个页面的宽度。当paginationMode从上到下或从下到上,这个属性代表了每个页面的高度。默认值为0,这意味着布局使用视窗的大小来确定页面的尺寸。调整这个属性的值会导致relayout。
Available in iOS 7.0 and later.

paginationBreakingMode

The manner in which column- or page-breaking occurs.
列或分页符的方式发生。

SWIFT
var paginationBreakingMode: UIWebPaginationBreakingMode
OBJECTIVE-C
@property(nonatomic) UIWebPaginationBreakingMode paginationBreakingMode

This property determines whether certain CSS properties regarding column- and page-breaking are honored or ignored. When this property is set to UIWebPaginationBreakingModeColumn, the content respects the CSS properties related to column-breaking in place of page-breaking.

See UIWebPaginationBreakingMode for possible values. The default value is UIWebPaginationBreakingModePage.、
这个属性决定是否特定CSS属性列和分页符是尊敬或忽略。当这个属性设置为UIWebPaginationBreakingModeColumn,内容方面相关的CSS属性column-breaking分页符。看到UIWebPaginationBreakingMode可能值。默认值是UIWebPaginationBreakingModePage。
Available in iOS 7.0 and later.

paginationMode

The layout of content in the web view.
在web内容的布局视图。

SWIFT
var paginationMode: UIWebPaginationMode
OBJECTIVE-C
@property(nonatomic) UIWebPaginationMode paginationMode

This property determines whether content in the web view is broken up into pages that fill the view one screen at a time, or shown as one long scrolling view. If set to a paginated form, this property toggles a paginated layout on the content, causing the web view to use the values of pageLength and gapBetweenPages to relayout its content.

See UIWebPaginationMode for possible values. The default value is UIWebPaginationModeUnpaginated.
这个属性决定内容的web视图分为页面填充视图一个屏幕上,或显示为一个长的滚动视图。如果设置为一个分页的形式,这个属性切换分页的布局内容,导致网页视图使用的值pageLength和gapBetweenPages relayout其内容。看到UIWebPaginationMode可能值。默认值是UIWebPaginationModeUnpaginated。
Available in iOS 7.0 and later.

Deprecated Properties (弃用属性)

detectsPhoneNumbers

(iOS 3.0) Property

A Boolean value indicating whether telephone number detection is on.
一个布尔值,指示是否电话号码检测。

OBJECTIVE-C
@property(nonatomic) BOOL detectsPhoneNumbers

If YES, telephone number detection is on; otherwise, NO. If a webpage contains numbers that can be interpreted as phone numbers, but are not phone numbers, you can turn off telephone number detection by setting this property to NO. The default value is YES on devices that have phone capabilities.

Special Considerations
The functionality provided by this property has been superseded by the dataDetectorTypes property.

Availability
Available in iOS 2.0 and later.
Deprecated in iOS 3.0.

没有更多推荐了,返回首页