美文网首页iOS实战开发Ios@IONIC
iOS-WebView加载HTML5且截取自定义URL

iOS-WebView加载HTML5且截取自定义URL

作者: 暖游丶 | 来源:发表于2015-12-28 15:50 被阅读849次

第一.遵循webView的代理

@interface SocialContactController ()<UIScrollViewDelegate,UIWebViewDelegate>{

BOOL isfirst;

}

第二.记录url

@property (strong,nonatomic) NSString *urlS;

第三.webView使用常规设置

- (void)viewDidLoad {

[super viewDidLoad];

if (!_isRootNavJump) {

[self leftButtonWithImage:[UIImage imageNamed:@"back_button.png"]

highlightImage:[UIImage imageNamed:@"back_button.png"]

title:nil

size:CGSizeMake(25, 25)

action:@selector(back)

target:self];

}

[self setupTitle:_titleName];

[self rightButtonWithImage:nil

highlightImage:nil

title:@""

size:CGSizeMake(70,40)

action:nil

target:self];

NSURLRequest *request;

if (_URLString) {

request = [NSURLRequest requestWithURL:[NSURL URLWithString:_URLString]];

}else{

self.urlS = @"此处Url根据服务器提供的地址,不便透露]";

request = [NSURLRequest requestWithURL:[NSURL URLWithString:_urlS]];

}

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WIDTH, HEIGHT-64)];

[self.webView loadRequest:request];

self.webView.scrollView.showsHorizontalScrollIndicator = NO;

self.webView.delegate              = self;

self.webView.scrollView.delegate    = self;

self.webView.scrollView.bounces    = YES;

_webView.scalesPageToFit            = YES;

[self.view addSubview:self.webView];

}

第四:在WebView的代理方法截取请求的URL地址,进行截取

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

if (_isRootNavJump) {

NSString *urlStr = request.URL.absoluteString;

if ([urlStr isEqualToString:self.urlS]) {

return YES;

}else {

NSRange range = [urlStr rangeOfString:@"data="];

if (range.length > 0)

{NSString *str = [urlStr substringFromIndex:range.location + range.length];

CompanySocialVC *company = [[CompanySocialVC alloc] init];

company.parameterStr = str;

company.hidesBottomBarWhenPushed = YES;

[self.navigationController pushViewController:company animated:YES];

//            [self.navigationController ]

NSLog(@"%@--- url",str);

return NO;

}}}return YES;}

最后,就可以拿到截取的参数去请求服务器拿数据了,一般服务器那边会处理解码,我们只需要传一个字符串给服务器就行了

相关文章

网友评论

    本文标题:iOS-WebView加载HTML5且截取自定义URL

    本文链接:https://www.haomeiwen.com/subject/xtnuhttx.html