我们如何打开以www开头的链接

时间:2014-11-04 06:56:06

标签: ios uiwebview

我是iOS的新手。我想打开以www开头的链接。我正在尝试追加http。但它不起作用。我点击链接开始,http开启但不是用www请帮助。我非常感谢任何帮助......

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (webView==myWebView) {
        if ([request.URL.absoluteString rangeOfString:@"http://"].location != NSNotFound) {
            [[UIApplication sharedApplication] openURL:request.URL];
            return NO;
        }

        else if([request.URL.absoluteString rangeOfString:@"www."].location != NSNotFound) {
                      [request.URL.absoluteString stringByReplacingOccurrencesOfString:@"www." withString:@"http://www."];

            [[UIApplication sharedApplication] openURL:request.URL];
            NSLog(@"%@",request.URL);
            return NO;
        }

        else {
            return YES;
        }
    } else {
        if ([request.URL.absoluteString rangeOfString:@"http://"].location != NSNotFound) {
            NSString *tempString=[request.URL.absoluteString stringByReplacingOccurrencesOfString:@"http://" withString:@""];
            NSArray *itemArray = [tempString componentsSeparatedByString:@","];
            NSDictionary *dict;
            NSString *title=@"";
            if ([[itemArray objectAtIndex:0] integerValue]==0) {
                dict=[toArray objectAtIndex:[[itemArray objectAtIndex:1] integerValue]];
                title=@"Recipient";
            } else if ([[itemArray objectAtIndex:0] integerValue]==1) {
                dict=[ccArray objectAtIndex:[[itemArray objectAtIndex:1] integerValue]];
                title=@"Cc";
            } else if ([[itemArray objectAtIndex:0] integerValue]==2) {
                dict=[bccArray objectAtIndex:[[itemArray objectAtIndex:1] integerValue]];
                title=@"Bcc";
            }
            [appDelegate.navigationController setNavigationBarHidden:NO];
            [self showUnknownPersonViewController:[dict objectForKey:@"address"] :[dict objectForKey:@"name"]:title];
            return NO;
        } else {
            return YES;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在使用'stringByReplacingOccurrencesOfString',但不使用该函数的返回值。使用返回的值创建新URL:

NSString *newURLString = [request.URL.absoluteString stringByReplacingOccurrencesOfString:@"www." withString:@"http://www."];
NSURL *newURL = [NSURL URLWithString:newURLString];
[[UIApplication sharedApplication] openURL:newURL];
NSLog(@"%@",newURL);
return NO;