0

我有一个 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;
}
4

2 回答 2

0

尝试检查您提到的方法

[[request URL] absoluteString]

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

[[[request URL] absoluteString] hasPrefix:@"http://www.yoursite.com"];
于 2013-07-22T14:21:16.823 回答
0

如果是您自己的域,您应该检查host并返回,如果不是,则返回,然后手动打开新的视图控制器:YESNO

- (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,但是你可以使用你想要的任何属性。但希望这能说明这个想法。

于 2013-07-22T14:41:40.317 回答