WebView in Cocoa not rendering - objective-c

In my window controller, I have this:
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"webView = %#", webView);
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://www.apple.com/"]]];
}
My understanding is that should make the webView in my window now download and render Apple's home page. But all I see in a white panel. What am I missing?
(I've checked that webView is not null; NSLog shows something like
"webView = <WebView: 0x10be7c3b0>")
I've also tried the more modern approach, but still had no success:
[webView setMainFrameURL:#"http://www.apple.com/"];

As this project is sandboxed, the solution in this case was to add the relevant entitlement to the project.

Related

Custom BackButton in iOS webview

I want to create custom back button on each page in my iOS web-view (objective-C). Can anyone please suggest me how to implement.
Thanks in Advance
I am new to iOS Web-view that is why I posted directly what I want this is my code :
#import "ViewController.h"
#interface ViewController () <UIWebViewDelegate>
#end
#implementation ViewController{
__weak IBOutlet UIImageView *logoImage;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.delegate = self;
NSString *urlString = #"https://www.anything.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
self.webView.hidden = YES;
if ([_webView canGoBack]) {
[_webView goBack];
}}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.webView.hidden = NO;
logoImage.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"%#", error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am passing a url of my website and I want to show a back button on every page of my application so that user can go to previous page. I have one more issue that whenever I minimise my application and opens it back from recent apps it get restarted while I want it to be resumed from the same screen
For the UIWebView, check the method - (void)goBack; out.
For the WKWebView, check the method - (WKNavigation *)goBack; and - (WKNavigation *)goBack:(id)sender; out.
UPDATE
// init the back button
[btnBack addTarget:self action:#selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- (void)backButtonTapped:(id)sender
{
[self.webview goBack];
}
Assume you have set up your webview and back button (in the navigation bar or somewhere else) in the full code.
The browser could go back when the current (tab) page has a history list and the current page url is not the first and last one.
Make sure you really want to goBack in viewDidLoad, it needs some time to finishing loading the target page you specified (https://www.anything.com) just like you do it in a browser.
The method loadRequest of webview is asynchronous, it pushes the url request to webview and returns it immediately.
Bind the webview's goBack method to your custom back button like the above code snippets.
Try to click a sample link in the initial page (https://www.anything.com), once the new page finishes loading, it could be able to goBack with satisfying [self.webview canGoBack].
Make a breakpoint in the implementation of method backButtonTapped, if needed.
Drop the logoImage or other unrelated code issues in your next question, that makes your question more clear and helpful.

UIWebView shows black screen without data

iOS 8.1, XCode 6.1, Storyboards and 2 UIViewControllers that look like this:
The view on the left is the main view, the view on the right is a UIWebView that will show help information written in HTML. My problem is that when I tap on the blue circle with an 'i' in it, it is supposed to go to the 2nd view controller (which it does) and display the html for that language (which it doesn't do)... all I get is a black screen! Here is my code to display the html file contents:
-(void) viewWillAppear:(BOOL)animated {
NSURL *indexURL;
NSString *sysLangCode = [[NSLocale preferredLanguages] objectAtIndex:0];
// do we support this language?
if([sysLangCode isEqualToString:#"en"] || [sysLangCode isEqualToString:#"de"] || [sysLangCode isEqualToString:#"it"] ||
[sysLangCode isEqualToString:#"es"] || [sysLangCode isEqualToString:#"fr"] || [sysLangCode isEqualToString:#"ja"] ||
[sysLangCode isEqualToString:#"zh-Hant"] ) {
indexURL = [[NSBundle mainBundle] URLForResource: [NSString stringWithFormat:#"instRST-%#", sysLangCode]
withExtension:#"html"]; // contatenate the language code to the filename
}
else
indexURL = [[NSBundle mainBundle] URLForResource: #"instRST-en" withExtension:#"html"]; // make 'en' the default
NSLog(#"\n\nmainBundle: %#",[NSBundle mainBundle]);
NSURLRequest *request = [NSURLRequest requestWithURL: indexURL];
NSLog(#"\n\nhtmlURL: %#",indexURL);
[self.webView loadRequest:request];
}
- (void)viewDidLoad {
[super viewDidLoad];
[webView setDelegate:self];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Failed to load with error :%#",[error debugDescription]);
}
UPDATE This is the connection for the webView:
I have tried different scenarios for hours now, and have given up doing this on my own. Can someone please tell me what I'm doing wrong?
Since you are using a storyboard you must use a Segue to present your ViewController. Delete your button action implementation and Control+Drag from your button onto your Help view controller. Link it to the Show segue.
i also face the same issue, below webview property helps me to avoid black screen problem
[self.web setOpaque:NO];

Opening URL in browser by clicking button

Hi friends,
I am getting data from server when user post a link on his timeline am displaying like this but my requirement is when i click Title then it will open browser. How can i do this?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.stackoverflow.com"]];
Will open a URL by the Application defined for the scheme. http has Safari as default (I guess)
To open it in the App itself create a ViewController + xib,
add a UIWebView to your View
& Buttons to get back to the app.
Then you can create an instance of your new ViewController and present it with
[self presentViewController:WebVC animated:YES completion:^(void){
[[WebVC MyWebView] loadRequest: [NSMutableURLRequest requestWithURL:[NSURL URLWithString: #"http://stackoverflow.com"]];
}];
e.g.
To make it even easier you could add a function to your ViewController like this one
-(void) LoadUrlFromString: (NSString *)url{
[self.MyWebView loadRequest: [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]]];
}
and then just do as before but call
[WebVC LoadUrlFromString:#"http://stackoverflow.com"];
on completition
You can use TTTAttributedlabel
TTTAttributedlabel *title_lbl = [TTTAttributedlabel alloc]init] //Make it an instance of TTTAttributed label what you are using for displaying the Title.
NSRange range = [title_lbl.text rangeOfString:"YOUR_TILE_HERE (Latest bollywod and hollywood.....")"];
[title_lbl addLinkToURL:[NSURL URLWithString:#"YOUR_URL_HERE (www.bollywood.com)"] withRange:range];
title_lbl.delegate = self;
And for onlcick method
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
NSlog("%#",label.text);
[UIApplication sharedApplication] openURL:url];
}

UIWebView Content is not resizable by user

I'm struck over this little issue and just can't figure a way out. My apologies and gratitude.
Description:
an UIView with fullscreen UIWebView targeted for iPhone programmatically loads a web page using viewDidLoad method. When running the app into the simulator UIWebView is displayed, the web page loaded, but no pinching is allowed. Thus, I can move around the page, but I can't resize with multigesture.
Details:
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.someurl.it";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.userInteractionEnabled=YES;
webView.autoresizingMask=YES;
webView.multipleTouchEnabled=YES;
}
Just insert this line of code along with the other properties you set for the webview:
webView.scalesPageToFit = YES;
Because the proper property to set to allow UIWebView's to interpret pinch-to-zoom events is calledscalesPageToFit. Setting an autoresizingMask is a hint to the auto-rotational layout mechanism in UIView of where a view should be positioned in the new coordinate space of the given device.

How do I scroll a WebView of an HTML string to a specific anchor?

I'm wondering how to scroll programmatically to a given anchor in a WebView.
The content I am showing is rendered by
[[webView mainFrame] loadHTMLString:htmlString baseURL:someURL];
and thus I cannot simply navigate to #anchors by pointing them out in the URLs.
I'm looking for a method along the lines of
[[webView mainFrame] scrollToAnchor:#"anchor"]
but obviously it isn't there.
TIA
Using Javascript Bridge works, but you can also do the equivalent from Objective-C if you like:
DOMDocument *doc = [[webView mainFrame] DOMDocument];
DOMElement *element = [doc getElementById:#"anchor"];
[element scrollIntoView:YES];
- (void)viewDidLoad
{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"YOUR_STRING_WITHOUT_ANCHOR"]]];
}
- (void)webViewDidStartLoad:(UIWebView *)_webView {
[self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('YOUR_ANCHOR_HERE_WITHOUT_#').scrollIntoView(true);"];
}
Ok, found a workaround, but I don't know if this is the right way to do it. By getting the reference to the javascript context I can call javascript methods in the webFrame.
[[webView windowScriptObject] evaluateWebScript:#"document.getElementById('TheId').scrollIntoView(true);"];