打开以http开头但不是www的链接

时间:2014-11-04 05:24:55

标签: ios uiwebview

我想打开以www开头的链接,他们不打开。我的代码只适用于http。请帮助>我是iOS的新手。我非常有责任。

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

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSLog("User tapped a link.");
    }

    if ([request.URL.absoluteString rangeOfString:@"iosscrollposition:"].location != NSNotFound) {
        NSString *positionString=[[[request URL]absoluteString] stringByReplacingOccurrencesOfString:@"iosscrollposition:" withString:@""];
        NSArray *positionArray=[NSArray arrayWithArray:[positionString componentsSeparatedByString:@","]];
        NSInteger x=[[positionArray objectAtIndex:0] integerValue];
        NSInteger y=[[positionArray objectAtIndex:1] integerValue];
        if (initialYPosition != y || y==0) {
            if (initialYPosition!=0 ||x==0) {
                [myScrollView setContentOffset:CGPointMake(myScrollView.contentOffset.x,myScrollView.contentOffset.y+20) animated:NO];
            }
            initialYPosition=y;
        }
        if (x>320) {
            [webViewScrollView setContentOffset:CGPointMake(x-300,webViewScrollView.contentOffset.y) animated:NO];
        } else if(x<320) {
            [webViewScrollView setContentOffset:CGPointMake(0,webViewScrollView.contentOffset.y) animated:NO];
        }
        return NO;
    } else if([request.URL.absoluteString rangeOfString:@"http://"].location == NSNotFound) {
        return YES;
    } else {
        return NO;
    }
}

3 个答案:

答案 0 :(得分:1)

请再添加一个else if

else if([request.URL.absoluteString rangeOfString:@"www://"].location != NSNotFound) {
    return YES;
}

建议您在每个网址之前添加http://,并注意某些网址可能属于安全网络,因此其扩展名可以是https://

答案 1 :(得分:1)

[self.myWebView loadHTMLString:tempString baseURL:[NSURL URLWithString:@"http://"]];

答案 2 :(得分:1)

NSURL *url=[NSURL URLWithString:@"http://"];

[self.myWebView loadHTMLString:tempString baseURL:url];