I'm trying to make my app display a different webpage for every row of a tableview, but running it, when I tap a row it display a white page!
This is the code of the table view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowPDF"]) {
Tab2_ItemViewController *vc = [segue destinationViewController];
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
NSString *stringaProva = [dataArrayLink objectAtIndex:selectedIndex];
vc.selectedItem = stringaProva;
}
}
#end
And this is the code for the subview:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:selectedItem];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:httpRequest];
}
If I write a url manually instead of NSURL *url = [NSURL URLWithString:selectedItem]; it works with no problem, so I'm guessing the problem is that the segue doesn't deliver the right variable... Can someone help?
Check stringaProva has value before passing to selectedItem
Use like this:
if(self.selectedItem)
NSURL *url = [NSURL URLWithString:self.selectedItem];
else
NSLog(#"selectedItem has no value");
Related
I am new to Objective c. I am learning about segue's and PrepareForSegue. So I have a table view with random links that the user has saved. I want to allow the user to click on the link and be taken to a web view to view the webpage. However, every time I click on a link the webpage is empty.
Table View. m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showArticle"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *urlString = [NSString stringWithFormat:#"http://%#", _toDoItems[indexPath.row]];
[[segue destinationViewController] setUrl:urlString];
WebView.m
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *URL = [NSURL URLWithString:[self.url
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://%#", request]]]];
// Do any additional setup after loading the view.
}
Add these keys to your info.plist. In ios 9 you must use https:// instead of http://
Here is the link for configuring App Transport Security
App Transport Security
Make sure your urlString return by this line must not be nil. It must be a valid url working in your browser:
NSString *urlString = [NSString stringWithFormat:#"http://%#", _toDoItems[indexPath.row]];
I have a UITableView within a UINavigationController,when I select a cell in the table view,it should slide to a UIWebView
//code in table view delegate(tableviewcontroller)
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSURL *url = [NSURL URLWithString:#"https://www.baidu.com/"];
self.webViewController.title = #"UIWebViewTest";
self.webViewController.URL = url;
[self.navigationController
pushViewController:self.webViewController animated:YES];
}
self.webViewController was initialized by
- (void)viewDidLoad {
[super viewDidLoad];
//..
//other code
//..
YGGWebViewController *wvc = [self.storyboard
instantiateViewControllerWithIdentifier:#"wvc"];
self.webViewController = wvc;
}
in the YGGWebViewController, I have a NSURL *URL property and override the setter
- (void)setURL:(NSURL *)URL{
_URL = URL;
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
}
besides,YGGWebViewController also have a UIWebView *webView property linked to a UIWebView in storyboard.
what odd is the web view does not display anything when it slides to the screen in the first time,but it will show the website in the second time(go back to the table view by navigation bar,and select a cell again).
Environment
xcode-7.0
deployment target-7.0
OS X Yosemite-10.10.5
P.S
if I move the code
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
to viewDidLoad in YGGWebViewController,it works right.but why?
When a cell is selected in my UITableView, I set self.itemURL to that cell's respective URL, then segue to WebViewController and load up that URL.
Here's the logic that happens when a cell is tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_matchCenterDone == YES) {
self.itemURL = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Item URL"];
NSLog(#"The URL IS:'%#", self.itemURL);
[self performSegueWithIdentifier:#"WebViewSegue" sender:self];
}
}
The NSLog prints out The URL IS:'http://www.ebay.com/itm/NEW-Sony-XPERIA-Z3-Compact-D5803-FACTORY-UNLOCKED-/131554310432?pt=LH_DefaultDomain_0, which tells me that self.itemURL does in fact exist, and is a properly formatted URL.
Before I segue to WebViewController, I do this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"WebViewS egue"]){
// Opens item in browser
WebViewController *controller = (WebViewController *) segue.destinationViewController;
controller.itemURL = self.itemURL;
}
}
Here's what WebViewControllers viewDidLoad function looks like:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"The url in webview is: '%#'", self.itemURL);
// Initialize UIWebView
self.myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,
self.view.frame.size.height)];;
self.myWebView.delegate = self;
[self.view addSubview:self.myWebView];
// set the url
NSURL *url = [NSURL URLWithString:self.itemURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// make url request
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil) {
[self.myWebView loadRequest:request];
[activityIndicator stopAnimating];
}
else if (error != nil) NSLog(#"Error: %#", error);
}];
[self.myWebView setScalesPageToFit:YES];
}
The NSLog above logs out The url in webview is: '(null)' and the console logs out an error stating Error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x7fcbfa5e7f10 {NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x7fcbfa64bec0 "unsupported URL"}
What's happening between my initial view controller and WebViewController that's causing self.itemURL become null, and therefore unable to load the url?
There's a space in your segue identifier string in your prepareForSegue: method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"WebViewS egue"]){
// ^ space here
// Opens item in browser
WebViewController *controller = (WebViewController *) segue.destinationViewController;
controller.itemURL = self.itemURL;
}
}
You are segueing to an instance of WebViewController, but its itemURL property is not being set because the code in that if statement is not being executed.
Take out the space and it should work.
I have a UITableViewController that needs to open a web view.
In my function I have:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:#"LinkViewController_iPhone" bundle:nil];
[linkViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
[self.navigationController pushViewController:linkViewController animated:YES];
}
I have connected all outlets and am not getting any warnings however I do not see the page pull up.
However if I go in the actual LinkViewController file and do something like:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
everything seems fine. I do not understand why ?
You should add a URL property to your LinkViewController. Then in your table view controller you do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:#"LinkViewController_iPhone" bundle:nil];
linkViewController.URL = [NSURL URLWithString:#"http://www.google.com"];
[self.navigationController pushViewController:linkViewController animated:YES];
}
Now in your LinkViewController you do:
- (void)viewDidLoad {
[super viewDidLoad];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.URL];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
There are two problems with what you had originally:
In the table view controller, you were accessing the webView property before it was initialized. This is why nothing happened.
You were exposing far too much implementation detail of the LinkViewController to the outside world. If you use LinkViewController in many places in your app, you are requiring every user of the class to know to create a URL, create a request, and then tell the internal web view to start loading. All of that logic belongs inside the LinkViewController. Now each user of the class only needs to know to set a simple URL property.
I'd like to load the PDF in my child view before it gets pushed onto the navigation stack. Right now there is a brief delay while the UIWebview is loading up the PDF file. What I would like is the PDF to be loaded so it slides on to the screen with the view.
I tried customizing the initWithNib method like below but then nothing loads. My thought was "If I start the PDF load during initialization it'll be ready when I go to push it." Guess not.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:pdfURL ofType:#"pdf"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
}
return self;
}
Here's my current implementation:
In the parent view I initialize the pdfViewer using this:
PDFViewerVC *vc = [[PDFViewerVC alloc]initWithNibName:#"PDFViewerVC" bundle:nil];
vc.pdfURL = #"Service Manual RS20";
vc.title = [self.list objectAtIndex:row];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
In the pdfViewer I load the PDF using this:
-(void)viewDidLoad{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:pdfURL ofType:#"pdf"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
[super viewDidLoad];
}
Thanks.
Try waiting until the UIWebView's webViewDidFinishLoad: delegate method is called before pushing its view controller onto the navigation stack.
You don't get very far with UIWebView, you need to go deeper, use Quartz.
Of course that means you need to replicate much that UIWebView has.
Check Apple's ZoomingPDFViewer for a simple example. http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html