iPhone控件之UIWebView2

本文介绍了一个简单的iOS应用示例,该应用使用UIWebView加载本地HTML文件,并通过代理方法处理页面点击链接的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 #import <UIKit/UIKit.h>
2
3 @interface UITestViewController : UIViewController <UIWebViewDelegate>
4 {
5
6 }
7
8 @end
9
10
11 //
12 // UITestViewController.m
13 // UITest
14 //
15
16 #import "UITestViewController.h"
17
18 @implementation UITestViewController
19
20 - (void)viewDidLoad {
21
22 [super viewDidLoad];
23
24 CGRect webRect = CGRectMake(10,10,300,400);
25 UIWebView *myWebView = [[UIWebView alloc] initWithFrame:webRect];
26
27 myWebView.delegate = self;
28
29 myWebView.scalesPageToFit = NO;
30 NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"myPage" ofType:@"html"];
31 NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
32
33 [myWebView loadHTMLString:htmlContent baseURL:nil];
34
35 [self.view addSubview:myWebView];
36
37 [myWebView release];
38 }
39
40 - (BOOL)webView:(UIWebView *)webView
41 shouldStartLoadWithRequest:(NSURLRequest *)request
42 navigationType:(UIWebViewNavigationType)navigationType {
43 NSURL *pageURL = [request URL];
44
45 if ( ([[pageURL scheme] isEqualToString: @"http"]) && (navigationType == UIWebViewNavigationTypeLinkClicked ))
46 {
47 [[UIApplication sharedApplication] openURL:pageURL];
48 return NO;
49 }
50
51 return YES;
52 }
53
54
55 - (void)didReceiveMemoryWarning {
56 // Releases the view if it doesn't have a superview.
57 [super didReceiveMemoryWarning];
58
59 // Release any cached data, images, etc that aren't in use.
60 }
61
62 - (void)viewDidUnload {
63 // Release any retained subviews of the main view.
64 // e.g. self.myOutlet = nil;
65 }
66
67
68 - (void)dealloc {
69
70 [super dealloc];
71 }
72
73 @end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值