UIWebView not loading URL - iphone

I'm having trouble getting a UIWebView to load a URL that I'm sending to it. Before I get into my code I was wondering if URL has to start with "http://" or can it start with "www."?
I'm using an IBAction to push a UIView on to the stack:
(IBAction)goToWebView {
WebViewController *webController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]];
//set the strings
webController.webTitle = [self Title];
webController.webURL = [self website];
//Push the new view on the stack
[[self navigationController] pushViewController:webController animated:YES];
[webController release];
webController = nil;
}
Then here is my WebViewController.h file:
#interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
NSString *webTitle;
NSString *webURL;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#property (nonatomic, retain) NSString *webTitle;
#property (nonatomic, retain) NSString *webURL; here
And my WebViewController.m file:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = webURL;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}

Your first step should be to include error-checking in the viewDidLoad code in your WebViewController class. There are at least three pointers that could potientially be nil that, were you to catch them at their point of failure, would give you big insights as to what might be going wrong with your page loading code.
From there, the contents of webURL should be examined (AFAIK it should start with "http://" or similar) as well as the proper instantiation of the webView at the end of viewDidLoad.

Related

Push TableView Url to webView

i have a problem with my table, i use LazyTableImages example from Apple before embed into my project.
Every work fine when load my xml like this
<im:title>1</im:title>
<content type="html">some desc</content>
<im:releaseDate label="October 23, 2013">2013-10-23T00:00:00-07:00</im:releaseDate>
<im:artist href="http:/facebook.com">Facebook</im:artist>
<im:name>1</im:name>
<im:image height="53">http://chingfong.com/Icon.png</im:image>
<url>http://facebook.com/url>
<im:date>Ene, 27 2013</im:date>
and here is my AppRecord.h
#property (nonatomic, strong) NSString *appName;
#property (nonatomic, strong) UIImage *appIcon;
#property (nonatomic, strong) NSString *artist;
#property (nonatomic, strong) NSString *imageURLString;
#property (nonatomic, strong) NSString *appURLString;
#property (nonatomic, strong) NSString *appURL;
ParseOperation.h - i also added
static NSString *kUrlStr = #"url";
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if (self.workingEntry)
{
if (self.storingCharacterData)
{
NSString *trimmedString = [self.workingPropertyString stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.workingPropertyString setString:#""]; // clear the string for next time
if ([elementName isEqualToString:kIDStr])
{
self.workingEntry.appURLString = trimmedString;
}
else if ([elementName isEqualToString:kNameStr])
{
self.workingEntry.appName = trimmedString;
}
else if ([elementName isEqualToString:kImageStr])
{
self.workingEntry.imageURLString = trimmedString;
}
else if ([elementName isEqualToString:kUrlStr])
{
self.workingEntry.appURL = trimmedString;
}
else if ([elementName isEqualToString:kArtistStr])
{
self.workingEntry.artist = trimmedString;
}
}
else if ([elementName isEqualToString:kEntryStr])
{
[self.workingArray addObject:self.workingEntry];
self.workingEntry = nil;
}
}
}
RootViewController.m in tableView didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
NSString *urlAddress = appRecord.appURL;
NSURL *url = [NSURL URLWithString:urlAddress];
WebViewController *detailViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
// WebViewController.sites= [_entries objectAtIndex:indexPath.row];
[detailViewController.webView loadRequest:[NSURLRequest requestWithURL:url]];
[self.navigationController pushViewController:detailViewController animated:YES];
}
WebViewController.h
#interface WebViewController : UIViewController <UIWebViewDelegate>
#property (nonatomic, strong) IBOutlet UIWebView *webView;
The table load fine, and it push to WebViewController when i selected cell but the UIWebView didn't load the web page, it's only blank (white).
can anyone help me to fix it?
thanks so much!
I think the best way for you is to pass the url from RootViewController to WebViewController and refresh your webview in WebViewController.
So you need to modify your code like this :
RootViewController.m in tableView didSelectRowAtIndexPath
// Add a #property of your detail view for lazing
#property (nonatomic, strong) WebViewController *detailViewController;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
if (!_detailViewController)
self.detailViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
self.detailViewController.websiteURL = appRecord.appURL;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
WebViewController.h
#interface WebViewController : UIViewController <UIWebViewDelegate>
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#property (nonatomic, strong) NSString *websiteURL;
WebViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:_websiteURL]];
// Load the request in the UIWebView
[_webView loadRequest:requestObj];
}

UIWebView Not loading

I am having a problem where UrlDisplay isn't displaying any data. UrlDisplay is located at the bottom of my .M. What I am doing is requesting a website collecting a few urls specific to the users request. Then setting up UiWebViews for them to navigate though is there any way to split a single UiWebView, or is this the best way to do it? (My sample is only collecting one website and trying to set one website to a UIWebView)I am not sure of why this is happening Everything in the .h is connected properly to the storyboard. Thanks for the help in advanced for any feed back!
.H
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *UrlDisplay;
#end
.M
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize UrlDisplay;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *website = [NSURL URLWithString:#"www.Google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:website];
NSURLResponse* response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//storing data in a string
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray *newString = [myString componentsSeparatedByString:#"\n"];
// Do any additional setup after loading the view, typically from a nib.
NSMutableString *curLineNum;
NSString *curLine;
NSMutableArray *URL =[[NSMutableArray alloc]init];
int i;
for (i = 0; i <[newString count]; i++) {
curLine = [newString objectAtIndex:i];
if ([curLine rangeOfString:#"Start Here"].location != NSNotFound) {
NSScanner *scanner = [NSScanner scannerWithString:curLine];
[scanner scanUpToString:#"Start Here" intoString:NULL];
[scanner setScanLocation:([scanner scanLocation]) + 16];
[scanner scanUpToString:#"End Here" intoString:&curLineNum];
[Url addObject:curLineNum];
}
}
NSURL *preWebsite = [NSURL URLWithString:[Url objectAtIndex:0]];
NSURLRequest *preRequest = [NSURLRequest requestWithURL:preWebsite];
[UrlDisplay loadRequest:preRequest];
//NSLog(#"%#",myString);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
the method URLWithString always expecting full URL path including URL scheme. In your case URL scheme is missing. Try this.
NSURL *website = [NSURL URLWithString:#"http://www.Google.com"];

Opening a pdf inside the app breaks the design

I'm having some kind of problems with one thing, i'm making an app and i have a webview that shows a pdf file, and i have a button if clicked will call an UIDocumentInteractionController (open the pdf in a new window).
I will leave screenshots to make it easier:
When i open the app:
http://imgur.com/dpIEd
After i open the UIDocumentInteractionController:
http://imgur.com/VYV2N
Here's the code too
.h file
#interface pdfView : UIViewController <UIDocumentInteractionControllerDelegate>
{
IBOutlet UIActivityIndicatorView *loading;
IBOutlet UIWebView *Wview;
UIDocumentInteractionController *controller;
}
#property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loading;
#property (nonatomic, retain) IBOutlet UIWebView *Wview;
#property (nonatomic, retain) UIDocumentInteractionController *controller;
-(void)buttonPressed;
-(IBAction)open_in:(id)sender;
-(IBAction)back:(id)sender;
#end
.m file
#import "pdfView.h"
#implementation pdfView
#synthesize loading,Wview;
#synthesize controller;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)viewDidLoad
{
[loading startAnimating];
[super viewDidLoad];
Wview.scalesPageToFit = TRUE;
Wview.multipleTouchEnabled = TRUE;
[Wview setOpaque:NO];
NSString *path = [[NSBundle mainBundle] pathForResource:#"guia_seguranca_2" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[Wview loadRequest:request];
[loading stopAnimating];
}
-(IBAction)open_in:(id)sender
{
NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:#"guia_seguranca_2" ofType:#"pdf"];
UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]];
preview.delegate = self;
[preview presentPreviewAnimated:YES];
[preview retain];
}
-(IBAction)back:(id)sender
{
[self.view removeFromSuperview];
}
-(void)buttonPressed
{
//[self.view removeFromSuperview];
NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:#"guia_seguranca_2" ofType:#"pdf"];
UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]];
preview.delegate = self;
[preview presentPreviewAnimated:YES];
[preview retain];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
[self.controller autorelease];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
[Wview release];
[loading release];
}
#end
Thanks in advance.....
I would suggest not useing the webview and using a pdf app (ibook, cloudreader) like this.
//name of pdf
NSString * pathString = #"userguide";
//get pdf path
NSString * filePath = [[NSBundle mainBundle] pathForResource:pathString ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:url];
BOOL isValid = [self.docController presentOpenInMenuFromRect:self.handBookLaunch.frame inView:self.view animated:YES];
if (!isValid) {
NSString * messageString = [NSString stringWithFormat:#"No PDF reader was found on your device. Please download a PDF reader (eg. iBooks)."];
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:messageString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
Solved, added this code for layout verification:
-(void) viewWillAppear:(BOOL)animated
{
CGRect arect=[[UIScreen mainScreen]applicationFrame];
CGRect anotherrect=[[UIApplication sharedApplication]statusBarFrame];
if(self.view.center.y==arect.size.height/2)
self.view.center=CGPointMake(self.view.center.x, self.view.center.y+anotherrect.size.height); // fix silliness in IB that makes view start 20 pixels higher than it should on iPhone
}
source: https://discussions.apple.com/thread/2658315?start=0&tstart=0

UIWebView not loading URL when URL is passed from UITableView

So i'm building a webView into my application to show the contents of a URL that I am passing from a selection in a UITableView. I know the UIWebView is loading content properly because if you hard code say http://www.google.ca into the NSURL then it loads fine, however when I'm passing the URL that I parsed from an RSS feed back from the UITableView it won't load the URL properly. I tried the debugger and the URL is coming out as nil right before I try and parse it, however I can use NSLog to print the value of it out to the console.
here's the code in my UIViewController that has my UIWebView
#import <UIKit/UIKit.h>
#interface ReadFeedWebViewController : UIViewController
{
NSString *urlToGet;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain) IBOutlet UIWebView *webView;
#property(nonatomic, retain) NSString *urlToGet;
#end
Here's the code for my implementation's viewDidLoad method...
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Url inside Web View Controller - %#", urlToGet);
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Once again, I can print the URL to NSLog fine and if I hard code the URL into the NSURL object then it will load fine in the UIWebView. Here is where I'm setting the value in my UITableViewController...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReadFeedWebViewController *extendedView = [[ReadFeedWebViewController alloc] init];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
extendedView.urlToGet = [[stories objectAtIndex: storyIndex] objectForKey:#"link"];
//NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"summary"]);
NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"link"]);
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
However, since I can print the value using NSLog in the extendedView view controller I know it's being passed properly. Cheers
The urlToGet is null into your ReadFeedWebViewController in the viewDidLoad method because when this method is call this variable is not yet affected. The viewDidLoad is call when the initialisation of the ViewController is finished.
I should you to call it in the viewWillAppear method like that:
- (void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Also, there is a memory leak in your code. You should release extendedView after pushing it on the navigation controller.
ReadFeedWebViewController *extendedView = [ReadFeedWebViewController new];
if (extendedView != nil) {
... setup extendedView here ...
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
Try this
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];

presentModalViewController issues: Newbie needs help

I have a UIViewController (which sits inside a tabUIController). Within this UIViewController, I want to present a modelviewcontroller when a user clicks on a button. I cannot get this to work. With the code I have (see below) i get to the stage where i press the button but i get the strange error of "unable to read unknown load command 0x80000022".
Any ideas? (for the BrowserUIViewController i havent added anything in there yet, but it shouldnt matter, with any controller i just want to get the syntax right).
#interface AboutDoronUIViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource> {
NSDictionary *values;
NSArray *keys;
BrowserUIViewController *browser;
AboutDoronUIViewController *about;
}
#property (retain, nonatomic) NSDictionary *values;
#property (retain, nonatomic) NSArray *keys;
#property (retain, nonatomic) BrowserUIViewController *browser;
#property (retain, nonatomic) AboutDoronUIViewController *about;
#end
...
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *getSection = [keys objectAtIndex:section];
NSArray *valuesForSection = [values objectForKey: getSection];
NSString *selectedLink = [valuesForSection objectAtIndex:row];
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; //unused for now
BrowserUIViewController *temp = [[BrowserUIViewController alloc] initWithNibName:#"BrowserUIViewController" bundle:nil];
self.browser = temp;
[self presentModalViewController:browser animated:YES];
[urlAddress release];
[valuesForSection release];
[selectedLink release];
[getSection release];
[temp release];
}
The code you have here looks correct, so your issue is likely elsewhere.
Try to put breakpoints around the presentModalViewController:animated: line to see what lines get executed before the error occurs.
Try putting breakpoints in the viewDidLoad and viewWillAppear: methods of the BrowserUIViewController class as well, to see which of those, if any, are being called before the error occurs.