How to write HTML iframe in tvos app for Apple TV - html

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

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.

Related

How to load localized html file from WKWebView?

1st StackOverflow question for me...
!! Objective-C !!
I'm in the process of updating an old app and am working on help screens.
I chose to display some useful How-tos through some html mainly for the ease of formatting the text (bullet points, colors, bold, underline etc...).
Since the app is a bit complex, The help screens are in a total of 3:
A main info webpage that links to 2 other detailed pages.
Tha app is localized in English (Base) and French.
Once the setup was complete, and with everything working fine, I went ahead and localized the base html files.
Now, they are neatly placed in the Base.lproj and fr.lproj files.
But when when I tap on a link to another page from the main WebView, nothing happens !
Any ideas ?
Thanks
Here are some bits of code I'm using:
In the .h file :
#interface howToViewController : UIViewController <WKUIDelegate>
{
WKWebView *infoText;
NSString *uRLString;
}
#property (nonatomic, retain) WKWebView *infoText;
In the .m file: (had to programmatically create the WKWebView because of the known Xcode 9 bug when creating it through Storyboards)
- (void)viewDidLoad
{
[super viewDidLoad];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *aWebView = [[WKWebView alloc] initWithFrame:self.view.frame
configuration:theConfiguration];
aWebView.UIDelegate = self;
self.infoText = aWebView;
NSURL *url = [[NSBundle mainBundle] URLForResource:#"infoHTML" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[infoText loadHTMLString:html baseURL:baseUrl];
[self.view addSubview:infoText];
}
In the html file, the links that no longer work once the files are localized:
<p>3/ More information is available for the respective tabs by pressing the following links :</p>
<ul><li>Limitations
</li></ul>
<ul><li>Fueling
</li></ul>
Working code from one of my apps. I'm using NSURLRequest and WKWebView's loadRequest instance method. I'm not sure what's going wrong with your use of loadHTMLString:baseURL: but I think it may have something to do with the value you're passing for the baseURL argument. Have you cleared the console, then single-stepped over that line of code to see if you're getting any message in the console?
NSURL *url = [[NSBundle mainBundle] URLForResource: pageString withExtension: #"html"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[webView loadRequest: request];

Load html content in UIWebView - iOS

I am loading html content in UIWebview (iPhone6) and the content has actionable buttons. Upon pressing one of the buttons a pop up shows up in the middle of the content (content is approximately 6-7 pages long). As a result, user is unable to see the popup as she is on the first screen. Earlier, I thought it was content's issue, but on android, the pop up comes on the middle of the first page.
-(void) loadScorm {
NSString *scormUrl = self.content.media.scormUrl;
scormUrl = [NSString stringWithFormat:#"%#%#", #"https:" , scormUrl];
NSURL *url = [NSURL URLWithString:scormUrl];
NSString *body;
body = //constructed body
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:request];
self.webView.frame = self.webViewContainer.frame;
}
I also read that javascript for UIWebView is enabled by default. What am I missing? Any pointers would be appreciated.
PS: Can't share the screenshot as the content is classified.
How you create your UIWebView via Code or IBOutlet.
If you are created your webView via IBOutlet then you have to check some webView property in Storyboard like
Then your webView will take Events.
And if you are created your UIWebView via code then write this code
[your_webView_Object setDataDetectorTypes: UIDataDetectorTypeAll];
[webview loadHTMLString: [NSString stringWithFormat:#"<div id ='foo' align='justify' style='font-size:22px; font-family:helvetica; color:#ffffff';>%#<div>",[jsonObject objectForKey:#"msg"]] baseURL:nil];
Hope you will find useful
Apparently, this was an issue from the side of content creator. The person didn't include the support of Safari and Chrome on iOS platform and hence, these issues.

I am trying to Load html string into a webview. But the webview shows a white blank colour on run time

I am trying to Load html string into a webview. But the webview shows a white blank colour on run time. added the UIWebViewDelegate and declare object like this.
#property (strong, nonatomic) IBOutlet UIWebView *aboutWebView;
but still having the same issue.
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
[_aboutWebView setDelegate:self];
[self loadWebView];
// Do any additional setup after loading the view.
}
-(void)loadWebView{
_aboutWebView= [[UIWebView alloc]init];
_aboutWebView.backgroundColor= [UIColor blueColor];
NSString *embedHTML= #"html Code";
[_aboutWebView loadHTMLString: embedHTML baseURL: nil];
}
Please remove the below line from your code.It works for you.
_aboutWebView= [[UIWebView alloc]init];
Set the web-view delegate after initialising it , and add this line of code [webView setScalesPageToFit:YES];
There is no need for allocating your webview again in loadWebView method of yours.
comment this line "_aboutWebView= [[UIWebView alloc]init]".
You are already creating an IBOutlet for your webview which automatically creates an object for view.
Have below in HTML String
<head><style type='text/css'>body {background-color: transparent;border: 0px;margin: 0px;padding: 0px;}</style></head>
And for webview have this
yourWebView.backgroundColor = [UIColor clearColor];
yourWebView.opaque = NO;
This will not show white color in run time...
First at all,your HTML format is not correct,writing correct HTML to load on webView.
Then,add self.view.backgroundColor=[UIColor whiteColor]; in function - (void)viewDidLoad.
Hope to help you!

How to show only section of site in UiWebView?

I want to show site in UiWebView but I don't need to show all site but only from <section id="xxx"> to </section> with optional <head> for CSS styles. I tried to download html code and paste it to UiWebView
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://example.com/page"] encoding:NSUTF8StringEncoding error:nil];
NSString* content2 = string;
NSString* beforeBody = #"<section id=";
NSString* afterBody = #"</section>";
NSString* finalContent = [[beforeBody stringByAppendingString:content2]
stringByAppendingString: afterBody];
[_webView loadHTMLString:finalContent baseURL:nil];
but it didn't work for me. Any ideas? Thank you for help!
You can do the following:
Create 2 UIWebViews in your viewController, webView and webView2, implement #interface ViewController : UIViewController <UIWebViewDelegate> in your viewController header.
In viewDidAppear you will load the fullHTMLString from the page you would like to work with into webView2 and afterwards set the delegate for webView2 like so:
-(void)viewDidAppear:(BOOL)animated
{
NSString *fullHTMLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"https://python.org"] encoding:NSASCIIStringEncoding error:nil];
[self.webView2 setHidden:YES];
[self.webView2 loadHTMLString:fullHTMLString baseURL:nil];
[self.webView2 setDelegate: self];
}
You will have to implement webViewDidFinishLoad delegate method, and in it you call for [stringByEvaluatingJavaScriptFromString], which will retrieve your desired single element based on parameters you give it.
In this example I chose to retrieve it by ElementID, you can use javascript to retrieve it in other ways.
Once you have your desired element to display, you create an HTML string and insert it into it, afterwards you load the full html with the single element into the first webView (webView and not webView2) this is done to prevent an endless loop while calling the delegate method.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *singleElementString = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"mainnav\").innerHTML;"];
NSString *html = [[NSString alloc] initWithFormat:#"<html>%#</html>", singleElementString];
[self.webView loadHTMLString:html baseURL:nil];
}

Injecting local files into UIWebView without breaking links

I am working on an iOS app that needs to display webpages from a server inside a UIWebView while injecting relevant local png and css files as needed in order to speed up load time. Here is the code I am using to try to do this:
NSData *myFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/index.html"]]];
NSString* myFileHtml = [[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding];
[myWebView loadHTMLString:myFileHtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
My problem is that some of the webpages have buttons in them that link to other webpages on the server, and because the UIWebView is only loading a string, the buttons when tapped don't cause the UIWebView to load the new webpage URL like it would if I had used the loadRequest method.
My question is how can I get the the UIWebView to behave like it is loading a request while still injecting local files from the baseurl?
Thanks
The relative links in the button cannot work, because the linked pages are on a remote server and not on the device's file system. However you can use a UIWebViewDelegate method to make it work:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *localRootPath = [NSString stringWithFormat:#"file://%#", [[NSBundle mainBundle] bundlePath]];
NSString *remoteRootPath = #"http://yourdomain.com";
NSString *remotePath = [[request.URL absoluteString] stringByReplacingOccurrencesOfString:localRootPath withString:remoteRootPath];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:remotePath]]];
// or you can use your own loading mechanism here
return NO;
}
return YES;
}
This method intercepts all requests from your WebView. If the request was triggered by a user tap / click the URL gets modified from a relative URL to a absolute URL so it can be loaded from the server. Don't forget to set the delegate on the WebView or this method will not be called.
NSURLPRotocol is a handler for NSURLConnection and will give you the opportunity to intercept the calls to the server and substitute your own content.
1) Derive a class from NSURlProtocol
2) Call NSURLProtocol registerClass: in your application:didFinishLaunchingWithOption
3) Read the documentation on implement these methods as necessary:
initWithRequest:cachedResponse:client:,
startLoading,
URLProtocol:didReceiveResponse:cacheStoragePolicy:
URLProtocolDidFinishLoading: