在新的webview视图控制器中打开外部URL

时间:2013-07-22 14:14:31

标签: ios url webview

我有一个webview应用程序,可以正常加载我的网站。我想要实现的是确定点击的网址是否属于我的网站。如果没有它打开一个新的视图控制器。

我知道我必须对此做点什么。

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

但我不知道最好的方法。根据我搜索的内容,它仅显示如何在webview中加载URL(没有问题),或者启动safari(不是我想要的)。

任何信息或指导将不胜感激。

更新8/26:

//Download manager
- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {

        #define kHostName @"http://www.mysite.com"
        NSString *host = [[request URL] host];
        if ([host isEqualToString:kHostName])
            return YES;
        else
        {
            WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
            [self.navigationController pushViewController:vc animated:YES];
            [vc release];
            return NO;
        }


        //External file links
        NSURL *externalURL = [request URL];
        NSString *externalFileExtension = [[externalURL absoluteString] pathExtension];

        //External file extensions
        NSLog(@"fileExtension is: %@", externalFileExtension);
        if ([externalFileExtension hasSuffix:@"zip"] || [externalFileExtension hasSuffix:@"deb"] || [externalFileExtension hasSuffix:@"rar"] || [externalFileExtension hasSuffix:@"mp3"] || [externalFileExtension hasSuffix:@"pdf"] || [externalFileExtension hasSuffix:@"exe"] || [externalFileExtension hasSuffix:@"mp4"] || [externalFileExtension hasSuffix:@"flv"] || [externalFileExtension hasSuffix:@"torrent"] || [externalFileExtension hasSuffix:@"aiff"] || [externalFileExtension hasSuffix:@"css"] || [externalFileExtension hasSuffix:@"doc"] || [externalFileExtension hasSuffix:@"gz"] || [externalFileExtension hasSuffix:@"hqx"] || [externalFileExtension hasSuffix:@"htm"] || [externalFileExtension hasSuffix:@"html"] || [externalFileExtension hasSuffix:@"ico"] || [externalFileExtension hasSuffix:@"ipa"] || [externalFileExtension hasSuffix:@"mov"] || [externalFileExtension hasSuffix:@"mpg"] || [externalFileExtension hasSuffix:@"ppt"] || [externalFileExtension hasSuffix:@"ps"] || [externalFileExtension hasSuffix:@"ram"] || [externalFileExtension hasSuffix:@"rtf"] || [externalFileExtension hasSuffix:@"swf"] || [externalFileExtension hasSuffix:@"tar"] || [externalFileExtension hasSuffix:@"tiff"] || [externalFileExtension hasSuffix:@"txt"] || [externalFileExtension hasSuffix:@"viv"] || [externalFileExtension hasSuffix:@"wav"] || [externalFileExtension hasSuffix:@"wmv"] || [externalFileExtension hasSuffix:@"xml"] || [externalFileExtension hasSuffix:@"ipsw"]) {



            NSError *error = nil; //error setting
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
            NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];

            if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
                [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

            //Download manager
            HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
            [dlvc downloadURL:externalURL userInfo:nil];

            [self.navigationController setNavigationBarHidden:NO];
            self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
            [self.navigationController pushViewController:dlvc animated:YES];
            dlvc.delegate = self;

        }else{

            //Internal file links
            NSURL *internalURl = [request URL];
            NSString *internalFileExtension = [internalURl pathExtension];

            //Internal file extensions
            NSLog(@"fileExtension is: %@", internalFileExtension);
            if ([internalFileExtension hasSuffix:@"zip"] || [internalFileExtension hasSuffix:@"deb"] || [internalFileExtension hasSuffix:@"rar"] || [internalFileExtension hasSuffix:@"mp3"] || [internalFileExtension hasSuffix:@"pdf"] || [internalFileExtension hasSuffix:@"exe"] || [internalFileExtension hasSuffix:@"mp4"] || [internalFileExtension hasSuffix:@"flv"] || [internalFileExtension hasSuffix:@"torrent"] || [internalFileExtension hasSuffix:@"aiff"] || [internalFileExtension hasSuffix:@"css"] || [internalFileExtension hasSuffix:@"doc"] || [internalFileExtension hasSuffix:@"gz"] || [internalFileExtension hasSuffix:@"hqx"] || [internalFileExtension hasSuffix:@"htm"] || [internalFileExtension hasSuffix:@"html"] || [internalFileExtension hasSuffix:@"ico"] || [internalFileExtension hasSuffix:@"ipa"] || [internalFileExtension hasSuffix:@"mov"] || [internalFileExtension hasSuffix:@"mpg"] || [internalFileExtension hasSuffix:@"ppt"] || [internalFileExtension hasSuffix:@"ps"] || [internalFileExtension hasSuffix:@"ram"] || [internalFileExtension hasSuffix:@"rtf"] || [internalFileExtension hasSuffix:@"swf"] || [internalFileExtension hasSuffix:@"tar"] || [internalFileExtension hasSuffix:@"tiff"] || [internalFileExtension hasSuffix:@"txt"] || [internalFileExtension hasSuffix:@"viv"] || [internalFileExtension hasSuffix:@"wav"] || [internalFileExtension hasSuffix:@"wmv"] || [internalFileExtension hasSuffix:@"xml"] || [internalFileExtension hasSuffix:@"ipsw"]) {


                NSError *error = nil; //error setting
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
                NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];

                if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
                    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

                //Download manager
                HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
                [dlvc downloadURL:internalURl userInfo:nil];

                [self.navigationController setNavigationBarHidden:NO];
                self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
                [self.navigationController pushViewController:dlvc animated:YES];
                dlvc.delegate = self;

            }else{

///////////////
//IMAGE CODE///
///////////////
                //Image file links
                NSURL *imageURl = [request URL];
                NSString *imageFileExtension = [[imageURl absoluteString] pathExtension];


                //Image file extensions
                NSLog(@"fileExtension is: %@", imageFileExtension);
                if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"] || [imageFileExtension hasSuffix:@"gif"] || [imageFileExtension hasSuffix:@"bmp"] || [imageFileExtension hasSuffix:@"raw"] || [imageFileExtension hasSuffix:@"PNG"] || [imageFileExtension hasSuffix:@"JPG"] || [imageFileExtension hasSuffix:@"JPEG"] || [imageFileExtension hasSuffix:@"GIF"] || [imageFileExtension hasSuffix:@"BMP"] || [imageFileExtension hasSuffix:@"RAW"]) {

                    [webView stopLoading];
                    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

                    //Image manager
                    WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
                    [self.navigationController pushViewController:vc animated:YES];
                    [vc release];

                }else{

                    //Image file links
                    NSURL *imageURl = [request URL];
                    NSString *imageFileExtension = [imageURl pathExtension];

                    //Image file extensions
                    NSLog(@"fileExtension is: %@", imageFileExtension);
                if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"] || [imageFileExtension hasSuffix:@"gif"] || [imageFileExtension hasSuffix:@"bmp"] || [imageFileExtension hasSuffix:@"raw"] || [imageFileExtension hasSuffix:@"PNG"] || [imageFileExtension hasSuffix:@"JPG"] || [imageFileExtension hasSuffix:@"JPEG"] || [imageFileExtension hasSuffix:@"GIF"] || [imageFileExtension hasSuffix:@"BMP"] || [imageFileExtension hasSuffix:@"RAW"]) {                        

                    [webView stopLoading];
                    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

                    //Image manager
                    WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
                    [self.navigationController pushViewController:vc animated:YES];
                    [vc release];

                    }else{

                    //Image file links
                    NSURL *imageURl = [request URL];
                    NSString *imageFileExtension = [imageURl absoluteString];

                    //Image file extensions
                    NSLog(@"fileExtension is: %@", imageFileExtension);
                if ([imageFileExtension hasSuffix:@"png"] || [imageFileExtension hasSuffix:@"jpg"] || [imageFileExtension hasSuffix:@"jpeg"] || [imageFileExtension hasSuffix:@"gif"] || [imageFileExtension hasSuffix:@"bmp"] || [imageFileExtension hasSuffix:@"raw"] || [imageFileExtension hasSuffix:@"PNG"] || [imageFileExtension hasSuffix:@"JPG"] || [imageFileExtension hasSuffix:@"JPEG"] || [imageFileExtension hasSuffix:@"GIF"] || [imageFileExtension hasSuffix:@"BMP"] || [imageFileExtension hasSuffix:@"RAW"]) {                            

                    [webView stopLoading];
                    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

                    //Image manager
                    WebImageViewController * vc = [[WebImageViewController alloc] initWithURL:[request URL]];
                    [self.navigationController pushViewController:vc animated:YES];
                    [vc release];

                    return NO;

                    }
                }
            }
        }
    }
}

    return YES;
}

2 个答案:

答案 0 :(得分:0)

尝试检查您提到的方法

[[request URL] absoluteString]

您可以尝试使用hasPrefix:(NSString *)前缀来检查url是否以您网站的名称开头。

[[[request URL] absoluteString] hasPrefix:@"http://www.yoursite.com"];

答案 1 :(得分:0)

您应该检查host并返回YES(如果它是您自己的域),如果没有,则返回NO,然后手动打开新的视图控制器:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        NSString *host = [[request URL] host];
        if ([host isEqualToString:kHostName])
            return YES;
        else
        {
            ViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"webview"];
            controller.url = request.URL;
            [self.navigationController pushViewController:controller animated:YES];
            return NO;
        }
    }

    return YES;
}

显然,如果你想进行模态演示,请调整它以使用presentViewController。同样,如果使用NIB,请将instantiateViewControllerWithIdentifier替换为[[ViewController alloc] initWithNibName:nil bundle:nil];(或其他)。我的视图控制器有一个公共属性NSURL *url,但你可以使用你想要的任何属性。但希望这说明了这个想法。