iOS界面编程-UIWebView
来源:互联网 发布:c语言define怎么用 编辑:程序博客网 时间:2024/06/02 00:15
一、介绍
使用UIWebView类在你的应用中嵌入web内容,只需要简单的三个步骤,第一步创建一个UIWebView对象,第二步把UIWebView与一个window联系起来,第三步,发送一个请求去加载web内容。你也能使用UIWebView向前、向后加载历史web页面。你甚至可以通过编程方式设置一些Web内容的属性。
使用loadRequest:方法开始加载网页内容,stopLoading方法去停止加载,loading属性去查看一个webview 是否在加载中。
如果你允许用户前进和回退历史浏览页面,这时你可以为按钮动作使用goBack和goForward方法去操作。也可以设置canGoBack和canGoForward 布尔属性去控制用户是否能前进否则回退操作。
默认的,web view自动把电话号码成一个电话链接的形式。当一个电话连接被点击后,手机应用就会启动然后拨打号码。可以把detectsPhoneNumbers属性设置成NO去关掉这个默认的行为。
你可以使用scalesPageToFit属性去设程序化置网页内容的缩放,设置后用户可以使用手势去改变缩放。
如果你想跟踪网页加载的内容,可以给对象去设置委托属性,去实现UIWebViewDelegate协议,
注意:
你不应该在UIScrollView对象中嵌入UIWebView或者UITableView对象。如果你这么做,可能会导致一些不可预期的行为,这是因为两个对象的混合和错误处理。
UIWebView类不应该被子类化。
二、相关属性和方法
@property(nonatomic,assign)id<UIWebViewDelegate>delegate
当网页内容加载时,会给委托消息发送对象。在你在指定了UIWebView委托对象中释放UIWebView时候,你必须先把委托属性设置为nil。例如这可以在你的dealloc方法中去实现。
- (void)loadData:(NSData * nonnull)data MIMEType:(NSString * nonnull)MIMEType textEncodingName:(NSString * nonnull)encodingName
baseURL:(NSURL * nonnull)baseURL
设置主页的内容,文本格式,内容编码以及基础URL
- (void)loadHTMLString:(NSString * nonnull)string baseURL:(NSURL * nullable)baseURL
设置主页内容和基本的URL- (void)loadRequest:(NSURLRequest * nonnull)request
通过初始化一个异步客户端请求去连接一个给定的URL地址
@property(nonatomic,readonly,strong)NSURLRequest*request 一个只读属性,URL请求属性,标记加载网页的位置。
@property(nonatomic,readonly,getter=isLoading)BOOLloading 指示接收器是否正在加载页面内容
- (void)stopLoading 停止加载
- (void)reload 重新加载当前页面
@property(nonatomic,readonly,getter=canGoBack)BOOLcanGoBack 布尔只读变量,判断是否能后退
@property(nonatomic,readonly,getter=canGoForward)BOOLcanGoForward 布尔只读变量,判断是否能前进
- (void)goBack后退页面
- (void)goForward 前进页面
@property(nonatomic)BOOLscalesPageToFit 布尔值决定是否网页内容缩放去适配视图,用户可以改变这个缩放
@property(nonatomic,readonly,strong)UIScrollView*scrollView 只读属性,获取与UIWebView相关的scrollview
@property(nonatomic)BOOLsuppressesIncrementalRendering 一个布尔值指示web view是否抑制内容渲染直到被充分加载到内存中。
@property(nonatomic)BOOLkeyboardDisplayRequiresUserAction 布尔值指示键盘显示是否需要用户点击激活
- (NSString * nullable)stringByEvaluatingJavaScriptFromString:(NSString * nonnull)script 返回一个运行script的结果
检测数据类型
@property(nonatomic)UIDataDetectorTypesdataDetectorTypes 把web view的内容数据类型转换成可点击的URL
@property(nonatomic)BOOLallowsInlineMediaPlayback
@property(nonatomic)BOOLmediaPlaybackRequiresUserAction
@property(nonatomic)BOOLmediaPlaybackAllowsAirPlay
@property(nonatomic)CGFloatgapBetweenPages
@property(nonatomic,readonly)NSUInteger pageCount
@property(nonatomic)CGFloatpageLength
enum {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
};
typedef NSUIntegerUIWebViewNavigationType;
三、实际例子webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-10)]; NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sina.com"]]; [self.view addSubview: webView]; [webView loadRequest:request]; webView.delegate = self; activityView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(150, 200, 20, 20)]; activityView.activityIndicatorViewStyle =UIActivityIndicatorViewStyleWhiteLarge; activityView.hidesWhenStopped= YES; [self.view addSubview:activityView]; UIToolbar *bar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, 44)]; [self.view addSubview:bar]; bar.barStyle = UIBarStyleBlack; UIBarButtonItem *tabBarItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(backClicked:)]; tabBarItem1.tag = URL_BACK; UIBarButtonItem *tabBarItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(backClicked:)]; tabBarItem2.tag = URL_FORWARD; UIBarButtonItem *tabBarItem3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(backClicked:)]; tabBarItem3.tag = URL_REFRESH; UIBarButtonItem *tabBarItem4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(backClicked:)]; UIBarButtonItem *flexibleButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; NSArray *tabBarItemArray = [[NSArray alloc] initWithObjects: tabBarItem1, tabBarItem4, tabBarItem2,flexibleButton,tabBarItem3 ,nil]; [bar setItems: tabBarItemArray];
-(void)backClicked:(UIBarButtonItem *)sender{ switch (sender.tag) { case URL_BACK: if (webView.canGoBack) { [webView goBack]; } break; case URL_FORWARD: if (webView.canGoForward) { [webView goForward]; } break; default: [webView reload]; break; } }#pragma mark -UIWebViewDelegate- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ return YES;}- (void)webViewDidStartLoad:(UIWebView *)webView{ [activityView startAnimating];}- (void)webViewDidFinishLoad:(UIWebView *)webView{ [activityView stopAnimating];}- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{ [activityView stopAnimating];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
- iOS界面编程-UIWebView
- iOS界面编程-UILabel
- iOS界面编程-UIImageView
- iOS界面编程-UIPickerView
- iOS界面编程-UIProgressView
- iOS界面编程-UIActivityIndicatorView
- iOS界面编程-UIAlertView
- iOS界面编程-UIButton
- iOS界面编程-UITextField
- iOS界面编程-UIDatePicker
- iOS界面编程-UIPageControl
- UIWebView---iOS中使用模板引擎渲染HTML界面
- UIWebView---iOS中使用模板引擎渲染HTML界面
- iOS开发--UIWebView
- Swift开发IOS-UIWebView
- iOS界面编程-UIActionSheet
- iOS界面编程-UITextView
- iOS界面编程-UIScrollView
- Android Studio导入SlidingMenu类库的方法(其他类库应该也适用)
- [Leetcode] Majority Element
- C#如何分割有IP+端口的字符串?
- 我的android足迹
- 摘录:Linux下安装Nginx详细图解教程 .
- iOS界面编程-UIWebView
- Meanshift understanding
- Spring 配置文件报错 springmvc.xml
- 当虚函数遇到多继承
- Stereo Matching文献笔记之(七):《Spatial-Depth Super Resolution for Range Images》读后感~
- CSS 的4中定位方式
- 【Android-001】【快速入门】
- C++的动态内存分配
- BUILD问题“Class not found : Lorg/apache/commons/logging/Log”