UIWebView do not display contents in ios - ios

I have been working around one small project which needs a similar GUI as that of SplitViewController. Now what i have done is, I have two UIViewControllers namely MasterViewController and DetailViewController which are added as subview of ViewController
The code is shown below for ViewController.m
- (void)viewDidLoad
{
UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds; // always implicitly in Portrait orientation.
width = fullScreenRect.size.width;
height = fullScreenRect.size.height;
detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
masterViewController = [[[MasterViewController alloc]initWithNibName:#"MasterViewController" bundle:nil] autorelease];
masterViewController.view.frame = CGRectMake(0, 0, 299, height);
detailViewController.view.frame = CGRectMake(300, 0, width-300, height);
[self.view addSubview:masterViewController.view];
[self.view addSubview:detailViewController.view];
}
Now in the MasterViewController didLoad method i have added as a subview MasterTableViewController...
MasterTableViewController class looks like this...
#interface MasterTableViewController : UITableViewController
{
DetailViewController *detailViewController;
}
All the functions related to UITableViewController are already made... and i have also added the functionality to add URL from user and store it in the MasterTableView.
Now in the following function I am trying to call loadWebView function of DetailViewController with the required string. The call is made perfectly to the function.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlString = [_objects objectAtIndex:indexPath.row];
detailViewController = [[DetailViewController alloc]init];
[detailViewController loadWebView:urlString];
}
In the loadWebView method of DetailViewController i have the following implementation..
-(void) loadWebView: (NSString *) urlToLoad
{
self.webView = [[[UIWebView alloc]
initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
[self.webView setDelegate:self];
NSLog(#"SELF->WEBVIEW: %#",self.webView);
NSURL *url = [NSURL URLWithString:urlToLoad];
NSLog(#"loading url %# %#", urlToLoad, self.webView);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
I have also implemented the following function just to make sure that my Webview is working..
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"webViewDidFinishLoad");
}
Now when i run my program the log prints all these things...
2012-10-15 20:04:21.621 SyneBlogApp[2320:c07] device width height 768.000000 1024.000000
2012-10-15 20:04:21.630 SyneBlogApp[2320:c07] calling viewDidLoad for detailView
2012-10-15 20:04:21.636 SyneBlogApp[2320:c07] /Users/Library/Application Support/iPhone Simulator/5.1/Applications/B846EDA4-49CF-4DD3-AAB3-FF8BC19C65DA/Documents/Data.plist:
2012-10-15 20:04:22.942 SyneBlogApp[2320:c07] calling viewDidLoad for detailView
2012-10-15 20:04:22.954 SyneBlogApp[2320:c07] SELF>WEBVIEW: <UIWebView: 0x6a94510; frame = (0 45; 768 1004); layer = <CALayer: 0x6a94930>>
2012-10-15 20:04:22.955 SyneBlogApp[2320:c07] loading url http://wordpress.com <UIWebView: 0x6a94510; frame = (0 45; 768 1004); layer = <CALayer: 0x6a94930>>
2012-10-15 20:04:22.994 SyneBlogApp[2320:c07] webViewDidStartLoad
2012-10-15 20:04:26.828 SyneBlogApp[2320:c07] webViewDidFinishLoad
According to log my webview loads the data perfectly, but when i have a look at the emulator i get a white screen. That is the content is not shown up...
Now just to make sure that nothing is wrong with webview i tried in DetailViewController viewDiDloadMethod the following which works and shows up the google page...
- (void)viewDidLoad
{
self.webView = [[[UIWebView alloc]
initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view addSubview:self.webView];
[super viewDidLoad];
NSLog(#"calling viewDidLoad for detailView");
}
Now I seriously have no idea of what is going wrong. Even after all the things are working up well in log still the webpage content is not shown up.. Guys if you have any clue of what is going wrong please help...

This...
detailViewController = [[DetailViewController alloc]init];
...creates a new DetailViewController instead of using the one you already created and made part of your display in the first code segment you showed. You are therefore loading up your web view for a controller/view that isn't on screen.

Related

xcode 8 created a second controller as a popup but just get black screen

I've tried using similar answers to my problem that i've encounter. I'm new to Xcode and I have one controller that contains a uiwebview that loads my website. i have coding to detect if a certain link is pressed. The issue i'm running into is when a certain link is pressed, I need my second controller to act as a pop up and display a different webpage. Everything works and that link is pressed my NSLog says that it in the second controller but the display is black. Here code that i have in the main view controller that loads my webpage:
- (void)viewDidLoad {
[super viewDidLoad];
self.webViewer.delegate=self;
NSString *fullURL = #"https://www.example.com?first_run=true";
NSURL *url = [NSURL URLWithString:fullURL];
//NSLog(#"first page loaded=%#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
[_webViewer loadRequest:requestObj];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
self.webViewer.delegate=self;
NSURL *strurl = request.URL;
NSString *urlString = strurl.relativeString;
NSLog(#"link clicked=%#",urlString);
if(//not the right link do this){
}
else{//is the right link do this
NSLog(#"found");
self.popup = [[PopUpController alloc] init];
[self.popup showInView:self.view animated:YES];
return NO;
}
}
#end
then for my second controller it's supposed to load a different webpage but just get a black screen:
- (void)viewDidLoad {
self.popupView.delegate=self;
NSString *popupURL = #"https://www.example2.com";
NSURL *url_popup = [NSURL URLWithString:popupURL];
NSLog(#"popup page loaded=%#",url_popup);
NSURLRequest *requestObj2 = [NSURLRequest requestWithURL: url_popup];
[_popupView loadRequest:requestObj2];
}
- (void)showAnimate{
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.view.alpha = 0;
[UIView animateWithDuration:.25 animations:^{
self.view.alpha = 1;
self.view.transform = CGAffineTransformMakeScale(1, 1);
}];
}
- (void)showInView:(UIView *)aView animated:(BOOL)animated{
CGFloat x = self.view.center.x - (aView.frame.size.width);
CGFloat y = self.view.center.y - (aView.frame.size.height);
[aView setFrame:CGRectMake(x,y,aView.bounds.size.width,aView.bounds.size.height)];
[aView addSubview:self.view];
if (animated) {
[self showAnimate];
}
}
Any help would be extremely appreciated. I've tried for 3 weeks searching and trying different solutions found on this forum but they all didn't work for mine. Not sure if I'm just not instantiating my controllers right or some piece of code that I'm missing. Thank you for your time in advanced.
This line:
self.popup = [[PopUpController alloc] init];
simply creates an instance of PopUpController but doesn't load any of you UI elements with it.
Assuming you have designed PopUpController in storyboards, give it an Identifier such as "MyPopUp"
and then use:
// storyboard reference
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: #"Main" bundle:[NSBundle mainBundle]];
// instantiate PopUpController from the storyboard
self.popup = (PopUpController *)[storyboard instantiateViewControllerWithIdentifier:#"MyPopUp"];
// show it
[self presentViewController:self.popup animated:YES completion:nil];

Adding header view to WKWebView ScrollView

Has anyone managed to successfully add a header or footer view to a WKWebView ScrollView?
I'm currently trying to do this using the method described here for a UIWebView Adding a header view to a UIWebView similar to Safari and Articles.
When this method is used in a WKWebView the content view origin.y is correctly changed but content is cut off at the bottom.
Using the scroll view content offset is also not possible as it breaks fixed positioned CSS elements in the web view.
In webView Delegate method
- (void)webViewDidFinishLoad:(UIWebView *)webView
add the following codebase,
mainWebViewObj.scrollView.contentInset = UIEdgeInsetsMake(headerView.frame.size.height,0.0,headerView.frame.size.height,0.0);
mainWebViewObj.scrollView.backgroundColor = [UIColor whiteColor];
if(![headerView superview])
{
[webView.scrollView addSubview:headerView];
[webView.scrollView bringSubviewToFront:headerView];
}
[mainWebViewObj.scrollView setContentOffset:
CGPointMake(0, -mainWebViewObj.scrollView.contentInset.top) animated:NO];
this worked perfect for me. Hope it solves your problem.
Here's an example that I think does as you describe. It offsets the web content by setting contentInset on the scrollView, and by offsetting the header view frame by a negative amount:
#implementation ViewController
{
WKWebView* _webView;
UIView* _headerView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_webView = [[WKWebView alloc] initWithFrame: self.view.bounds];
[self.view addSubview: _webView];
[_webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: #"http://www.stackoverflow.com"]]];
[_webView.scrollView setContentInset: UIEdgeInsetsMake(100, 0, 0, 0)];
_headerView = [[UIView alloc] initWithFrame: CGRectMake(0, -100, 375, 100)];
_headerView.backgroundColor = [UIColor redColor];
[_webView.scrollView addSubview: _headerView];
}
- (void) viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_webView.frame = self.view.bounds;
CGRect f = _headerView.frame;
f.size.width = _webView.bounds.size.width;
_headerView.frame = f;
}

Load different URL's in Web View

I have an app where I would like to push a button and have the user taken to a web view. However, I would like a particular page to load depending on the button that's pushed. Originally I thought about creating a separate web view for each button, but that only caused more issues, which I won't go into. So my question is: How can I load different URL's into a web view, based on whether a particular button is pushed?
I thought about using the prepareForSegue method, but thought that might be over complicating things. Any direction would be great.
so you must create new viewController with UIWebView inside it.
On newViewController create a NSString property named url or somethings like that
example:
#property (retain, nonatomic) NSString *url;
#property (weak, nonatomic) IBOutlet UIWebView *webView;
On buttonTouchUpInside event in oldViewController
UIStoryboard *mainStorybroad = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
newViewController *webViewController = [mainStorybroad instantiateViewControllerWithIdentifier:#"webviewIdentifier"];
webViewController.url = #"url string";
and final, in newViewController you just do:
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
Why don't you link the button in IB to just do a modal transition to the view controller page? Then, in the viewdidload on the new view controller, the code should look something like this:
NSString *fullURL = #"google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
How many buttons do you have? If it's just a couple, then I would just create an extra couple view controllers. If it's a bunch, you could always create a global variable (I can already hear the cringing...) and set the variable equal to the desired URL depending on which button was pushed.
How can I load different URL's into a web view, based on whether a particular button is pushed?
There are two basic approaches to recognizing different buttons:
separate actions: You can create a separate action method for each button, and then just hook up each button to the corresponding action.
single action: You can create a single action for all the buttons, and use some property of the buttons to differentiate between them.
The first one looks like this:
- (IBAction)button1Action:(UIButton*)sender
{
[self.webView loadRequest:[NSURLRequest requestWithURL:url1]];
}
- (IBAction)button2Action:(UIButton*)sender
{
[self.webView loadRequest:[NSURLRequest requestWithURL:url2]];
}
The second looks like:
- (IBAction)buttonsAction:(UIButton*)sender
{
switch (sender.tag) {
case button1Tag:
[self.webView loadRequest:[NSURLRequest requestWithURL:url1]];
break;
case button2Tag:
[self.webView loadRequest:[NSURLRequest requestWithURL:url2]];
break;
}
}
Here is the code for .h and .m File
Create a XIB and Put UIWebView on it and bind it to the class.
For Header File
#import <UIKit/UIKit.h>
#interface NewWebViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
NSString *currentURL;
UIActivityIndicatorView *activityView;
}
#property (nonatomic, copy) NSString *currentURL;
#property (nonatomic, retain) UIActivityIndicatorView *activityView;
#end
For Implementation File (.m)
#import "NewWebViewController.h"
#interface NewWebViewController ()
#end
#implementation NewWebViewController
#synthesize currentURL;
#synthesize activityView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
webView.frame = self.view.frame;
[webView setScalesPageToFit:YES];
NSURL *url = [NSURL URLWithString:self.currentURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
self.activityView = [[[UIActivityIndicatorView alloc] init] autorelease];
self.activityView.tag = 1;
self.activityView.hidesWhenStopped = YES;
UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
self.activityView.center = customView.center;
[customView addSubview:self.activityView];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:customView] autorelease];
[self.activityView startAnimating];
}
#pragma mark UIWebView Delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self.activityView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.activityView stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Error %#",[error description]);
[self.activityView stopAnimating];
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[webView stopLoading];
webView.delegate = nil;
webView = nil;
self.activityView = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
webView.frame = self.view.frame;
return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) dealloc {
[currentURL release];
[webView release];
[super dealloc];
}
#end
Here how to use this
- (void) openWebViewControllerWithURL:(NSString*) url {
if (!url) {
return;
}
NewWebViewController *vController = [[NewWebViewController alloc] initWithNibName:#"NewWebViewController" bundle:nil];
vController.currentURL = url;
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}
On click of button just pass the URL you want to load
like this
[self openWebViewControllerWithURL:#"http://www.google.com"];

Subview common to multiple view controllers is unresponsive

I am using a UIView to show busy/processing clue to User and components in loading view are not responding to user touches. Let me elaborate my problem.
I have around 3 UIViewControllers and based on situation one View from VieControllers would be displayed. I use following code to change current view
UIView *currentView = self.view;
UIView *theWindow = [currentView superview];
//Some other code
[currentView removeFromSuperview];
[self.view setFrame: [tileCountViewController.view bounds]];
[theWindow addSubview:tileCountViewController.view];
[theWindow setFrame:[tileCountViewController.view bounds]];
[theWindow setFrame: [[UIScreen mainScreen] bounds]];
I then have a Loading view which generally used to show busy/processing kinda of UI to User. I created Loading view in following way from nib file
- (id)init {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
NSString *nibName = #"LoadingViewController";
if(UIInterfaceOrientationIsLandscape(orientation)) {
nibName = #"LoadingView-landscape";
}
else {
nibName = #"LoadingView";
}
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self = [super initWithFrame:portraitView.frame];
[self addSubview:portraitView];
[self bringSubviewToFront:portraitView];
return self;
}
In app delegate, I initialize loading view and add it to Main window
if(!loadingViewIndicator){
loadingViewIndicator = [[LoadingView alloc]init];
}
[[[UIApplication sharedApplication]keyWindow]addSubview:loadingViewIndicator];
loadingViewIndicator.hidden = YES;
Whenever I need to show loading/busy view, I simply load it like this
[loadingViewIndicator performSelectorOnMainThread:#selector(startLoadingViewIndicator:) withObject:startMsg waitUntilDone:NO];
- (void)startLoadingViewIndicator:(NSString *)startMsg{
self.hidden = NO;
[self.superview bringSubviewToFront:self];
[self.activityIndicator startAnimating];
[self.loadingMessage setText:startMsg];
self.loadingProgressView.progress = 0.0f;
[self refreshPosition];
}
Everything works fine but components in Loading view are not responding to touches. Any clue what is going wrong?
I just realized that problem is due to threading issue. I show this subview when User starts downloading any file. My earlier code was to directly start download
urlconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately: YES];
I am now calling this method from another method using background thread as below
[self performSelectorInBackground:#selector(startDownload) withObject:nil];
And then I am adding runloop in download like
urlconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately: YES];
CFRunLoopRun();
This works fine and my subview is responsive. It really took lots of time. The problem is there is clear code example to help to run download in background thread and at the same time make UI responsive.

How to detect a click event in a UIWebView

I have a UITableView that loads content into a UIWebView like this:
//MainViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:bounds];
UIWebView *htmlView = [ [ UIWebView alloc ] initWithFrame:[scrollView bounds]];
htmlView.frame = CGRectMake(10,10,280,360);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[htmlView loadRequest:requestObj];
scrollView.contentSize = [htmlView bounds].size;
[scrollView addSubview:htmlView];
[detailsViewController.view addSubview:htmlView];
[htmlView release];
htmlView = nil;
[scrollView release];
scrollView = nil;
[[self navigationController] pushViewController:detailsViewController animated:YES];
[detailsViewController release];
}
All of the remote content that gets fed to the UIWebView comes from a web service that I wrote. The first screenshot is what is presented when a word is selected from the list view titled "Glossary"
This all is great until you follow a link, in this case clicking on the "frame" link in the UIWebView content. The new content gets loaded but I need to tell the nav bar that the UIWebView has changed so I can at least change the white title text to match the new content. What UIWebView methods should I implement and where should they go?
Set your view controller to be the UIWebView's delegate, then implement
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

Resources