iOS开发学习43 使用UIWebView

加载本地文件代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect bounds = [[UIScreen mainScreen]bounds];
    UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
    
    
    webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    
    [self.view addSubview:webView];
    
    NSURL *url =[NSURL URLWithString:[[NSBundle mainBundle]pathForResource:@"2-2" ofType:@"png" inDirectory:@"assets"]];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

说明:

1.拖动文件到Xcode,提示两个选择,“create groups”和“create folder references”,默认情况下为第一种,即所有加入到项目的文件都会在mainBundle根路径下,即不管加入项目的文件的目录结构如何,在APP中都可以通过mainBundlePath/filename来访问到;如果采用第二种方式,则就会保留相对路径,需要通过mainBundlePath/path/filename来访问。通过这两种方式到项目的文件夹显示具有不同的颜色。 选择“create groups”,文件夹颜色为黄色。

html里有一个路径的问题( link href= “”),在Xcode中, Xcode不能找到像url("…/images/photo.png")这种路径,所以应该在Xcode中调整下html文件访问本页面图片和css样式文件的路径。

加载网页

  CGRect bounds = [[UIScreen mainScreen]applicationFrame];
    UIWebView* webview = [[UIWebView alloc]initWithFrame:bounds];
    [_container addSubview:webview];
    
    NSURL* url =[NSURL URLWithString:@"https://www.baidu.com"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [webview loadRequest:request];

参考文章:
https://www.jianshu.com/p/8f3c47c24e29
https://www.jianshu.com/p/173b7f371bf7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程圈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值