0

I am using InAppSettingsKit for my settings and want to add a company hyperlink in the table view footer. I have successfully add a uiwebview in a section header (which is in the bottom of the table view and appears as the footer). The webview shows the hyperlink but does not respond to touch.

The web view is created in settingsViewController:tableView:viewForHeaderForSection:

    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor clearColor];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 0, self.view.frame.size.width-60, 35)];
    webView.delegate = self;
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]];
    NSUInteger fontSize = 13;

    NSString *link = @"<a href=\"http://www.google.com\">My Hyperlink</a>";
    [webView loadHTMLString:[NSString stringWithFormat:@"<html><head></head><body style=\"font-family: sans-serif;font-size:%dpx;\"> <center>Copyright © %d %@</center></body></html>", fontSize, dateComponents.year,link]  baseURL:nil];
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    [containerView addSubview:webView];
    return containerView;

and the uiwebview delegate method:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
DDLogVerbose(@"Web view should start load with request %@. InType: %d", inRequest.URL.absoluteString, inType);
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
    [[UIApplication sharedApplication] openURL:[inRequest URL]];
    return NO;
}

return YES;

}

Can anyone tell me why the touch does not work?? Btw... webView:shoudStartLoadWithRequest: gets called right away when the table view gets loaded/displayed

Thx!

Chuckels5584
  • 551
  • 2
  • 6
  • 17

3 Answers3

1

in your code i found two issues for container view there is no frame and you are not setting proper frame for your webview once try this example you'l get where you went wrong.

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 0, 100, 35)];
  • Thank you for your response, but that didn't help. The webview with formattet hyperlink already displays just fine. It just does not respond to touch like it should. – Chuckels5584 Jun 19 '13 at 16:07
0

The reason was that IASKSettingsDelegate method settingsViewController:tableView:heightForHeaderForSection: returned 0 for that particular section. Returning the same height as the height of the uiwebview solved the issue.

Chuckels5584
  • 551
  • 2
  • 6
  • 17
0

As an alternative: Starting with iOS 7, UIKit/NSAttributedString supports NSLinkAttributeName and even does basic importing of HTML. You can therefore use UITextView instead of UIWebView as your table view header/footer.

To import HTML, use -[NSAttributedString data:options:documentAttributes:] with the NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType option.

NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:NULL error:NULL];

UITextView *textView = [[UILabel alloc] initWithFrame: ...];
textView.editable = NO;
textView.scrollEnabled = NO;
textView.textContainerInset = UIEdgeInsetsZero; // optional
textView.attributedText = attributedText;

[UILabel displays hyperlinks but they're not clickable, see UILabel and NSLinkAttributeName: Link is not clickable

Community
  • 1
  • 1
jrc
  • 20,354
  • 10
  • 69
  • 64