Search code examples
htmltvosapple-tv

How to write HTML iframe in tvos app for Apple TV


I am wondering how I can essentially wrap a webpage in some sort of iframe into a tvos app for the Apple TV. I was able to use a UITextView to write out basic HTML to display in a tvos app - basic div's with basic styling - however, I cannot figure out how to write out an iframe.

The code below, which is written in Objective C using XCode will display "hello!!!!" inside the Apple TV simulator, but not the iframe.

Here is my code below. Any help? Thanks!

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)print:(id)sender{

}

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 124.0f);
    UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
    textView.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:textView];

    NSString* staticHTMLContent = @"<div>hello!!!!</div> <iframe     frameborder=""1"" width=""420"" height=""345"" src=""//www.youtube.com/embed/C8kSrkz8Hz8""></iframe>";

    NSAttributedString* myHTMLString = [[NSAttributedString alloc] initWithData:[staticHTMLContent dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType} documentAttributes:nil error:nil];

    [textView setAttributedText:myHTMLString];
}


@end

Solution

  • A UITextView won't load an iframe. You need UIWebView to do that but that API isn't available in TvOS.

    The reason the HTML shows up in you example is because you can render HTML/CSS in a UITextView as you're doing, but it isn't actually processed as if it was in a UIWebView, it's simply used for styling the content as an alternative to attributed text.