1

I am currently using ChildBrowser in phoneapp to open a website. However, I would like to open all external links of this website to open in Safari.

I do not have control over the source of this website.

My understanding is that I have to modify ChildBrowser to open all links starting with "http" in Safari.

I can't exactly read Objective-C, but I believe the code below is relevant.

- (void)loadURL:(NSString*)url
{
    NSLog(@"Opening Url : %@",url);

    if( [url hasSuffix:@".png" ]  || 
        [url hasSuffix:@".jpg" ]  || 
        [url hasSuffix:@".jpeg" ] || 
        [url hasSuffix:@".bmp" ]  || 
        [url hasSuffix:@".gif" ]  )
    {
        [ imageURL release ];
        imageURL = [url copy];
        isImage = YES;
        NSString* htmlText = @"<html><body style='background-color:#333;margin:0px;padding:0px;'><img style='min-height:200px;margin:0px;padding:0px;width:100%;height:auto;' alt='' src='IMGSRC'/></body></html>";
        htmlText = [ htmlText stringByReplacingOccurrencesOfString:@"IMGSRC" withString:url ];

        [webView loadHTMLString:htmlText baseURL:[NSURL URLWithString:@""]];

    }
    else if ( [url hasPrefix:@"http" ])
    {
        //I have added in this else if.
    }
    else
    {
        imageURL = @"";
        isImage = NO;
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        [webView loadRequest:request];
    }
    webView.hidden = NO;
}

Any advice?

4

1 回答 1

0

我发现的最简单的方法是,如果您知道您最初将使用 childBrowser 请求的域主机名并且您不打算请求任何其他域,那么您可以将此事件直接写入 ChildBrowserViewController.m文件。

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    NSString* domain = [url host];

    // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"] || [[url scheme] isEqualToString:@"www"]) {
        if ( [domain isEqualToString:@"YOURDOMAIN.NAME] ) {
            return YES;
        } else {
            [[UIApplication sharedApplication] openURL:url];
            return NO;
         }
    } else {
         return YES;// [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}

这将捕获 childBrowser 发出的所有请求,如果它们符合标准 URL 约定(在 HTTP、HTTPS 或 WWW 之前)并且它们不是您预设的域主机名,那么它将强制它们在 Safari 中打开。然后,当用户返回您的应用程序时,他们仍然会在他们单击链接时所在的上一页上。

我搞砸了几个小时,直到我想出了这个。

如果您需要能够使用 childBrowser 打开多个域主机,但仍希望每个主机都在 Safari 中打开远离该当前域的所有外部链接,我建议如下。

将以下内容添加到 ChildBrowserViewController.h

BOOL scaleEnabled;
BOOL isImage;
NSString* originalURL; <- THIS RIGHT HERE
NSString* imageURL;

和这个

@property(retain) NSString* imageURL;
@property(retain) NSString* originalURL; <- THIS RIGHT HERE
@property(assign) BOOL isImage;

然后将以下内容添加到 ChildBrowserViewController.m

@synthesize imageURL;
@originalURL; <- THIS RIGHT HERE
@synthesize supportedOrientations;
@synthesize isImage;

并添加以下内容

- (void)loadURL:(NSString*)url
{
    originalURL = url; <- THIS RIGHT HERE
    NSLog(@"Opening Url : %@",url);

然后在我上面发布的方法中更改以下内容

    NSURL *url = [request URL];
    NSString* domain = [url host];

    NSURL *oURL = [NSURL URLWithString: originalURL]; <- THIS RIGHT HERE
    NSString* oDomain = [oURL host]; <- THIS RIGHT HERE

    // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"] || [[url scheme] isEqualToString:@"www"]) {
        if ( [domain isEqualToString:oDomain] ) { <- THIS RIGHT HERE
            return YES;
        } else {

我还没有测试第二部分,但它应该可以正常工作。

祝你好运。

于 2012-08-02T01:46:02.870 回答