PDF in UIWebView not working - ios

I have two UIPopoverController. each one opens up a pdf in a UIWebViewDelegate when I click the respective buttons.
However,only one works.
Both buttons, A and B, have this format for the click function (except Button B would say ButtonB not ButtonA)
- (IBAction)openPDF_Pictures_A:(id)sender
{
ButtonAPDFViewController *DWV =[[ButtonAPDFViewController alloc] initWithNibName:nil bundle:nil];
self.masterPopOverController = [[UIPopoverController alloc] initWithContentViewController:DWV];
[self.masterPopOverController presentPopoverFromRect:[sender frame] inView:[sender superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[self.masterPopOverController setPopoverContentSize:CGSizeMake(600, 800) animated:NO];
}
And in the UIViewController for each UIWebView, is this:
#import "ButtonAViewController.h"
#interface ButtonAViewController ()
#end
#implementation ButtonAViewController
#synthesize buttonAPDFView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"partA" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[buttonAPDFView loadRequest:request];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Both buttons A and B are on the same view. I did make one of the IBActions use masterPopOverController2 and one of the masterPopOverController to see if that would resolve anything but it doesn't. The one that doesn't work opens up a popover but it doesn't show the PDF in the popover. Any suggestions?

just realized I didn't add the UIWebView inside the UIView in the xib...

Related

launch a webview programmatically from another view controller error

Here is my main view controller:
#import "ViewController.h"
#import "WebViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
WebViewController *wvc = [[WebViewController alloc]init];
[self presentViewController:wvc animated:NO completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is webviewcontroller:
#import "WebViewController.h"
#interface WebViewController ()
#end
#implementation WebViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
NSString *url=#"https://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
The google page does not get loaded and the warning I get is
Warning: Attempt to present on
whose view is not in the window
hierarchy!
What the hell is going on?
I'd suggest double-checking. Following your code, I just confirmed this:
#import "LoadPresentViewController.h"
#import "WebViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)doLoadPresent:(id)sender {
WebViewController *wvc = [[WebViewController alloc]init];
[self presentViewController:wvc animated:NO completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// this fails in viewDidLoad
// WebViewController *wvc = [[WebViewController alloc]init];
// [self presentViewController:wvc animated:NO completion:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// this succeeds in viewDidAppear
WebViewController *wvc = [[WebViewController alloc]init];
[self presentViewController:wvc animated:NO completion:nil];
}
#end

Only black screen in my UIWebView

I have two View Controllers. In the first one i have a button and when you click it you need to display a Web Page in a second View Controller. When I click the button, I see only a black screen.
Here is TabulkaViewController.m code:
#import "TabulkaViewController.h"
#import "WebViewController.h"
#interface TabulkaViewController ()
#end
#implementation TabulkaViewController
- (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.
_hbutton.layer.borderWidth = 1;
_hbutton.layer.borderColor = [[UIColor grayColor] CGColor];
}
- (IBAction)hbutton:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://apple.com"];
WebViewController *webViewController = [[WebViewController alloc] initWithURL:url andTitle:#"Apple"];
[self presentViewController:webViewController animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Here is code of WebViewController.h:
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UIWebView *webView;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
- (IBAction) done:(id)sender;
#end
And here is code of WebViewController.m:
#import "WebViewController.h"
#interface WebViewController ()
#end
#implementation WebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[webView loadRequest:requestObject];}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
if( self = [super init] ) {
theURL = url;
theTitle = string;
}
return self;
}
-(id)initWithURL:(NSURL *)url {
return [self initWithURL:url andTitle:nil];
}
- (IBAction) done:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
webView.delegate = nil;
[webView stopLoading];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
You need to instantiate the webView at some point. I suggest you change your viewDidLoad to this:
- (void)viewDidLoad
{
[super viewDidLoad];
webView = [[UIWebView alloc] init];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[webView loadRequest:requestObject];
}
Since your webView is nullat the start of the method, it needs to be created before you can load any Requests. Just declaring it in the header file is not enough, thats why you need webView = [[UIWebView alloc] init];.
EDIT:
Of course, you could also put that line into your ìnitWithURL:andTitle: method, where the other instantiations take place.

how to present the same view controller after dismissing its once in iOS

In my app I have a UITabBarController which has four view controllers. In every view controller I have a collection view. In each collection view I have some images. When I select an image from the third view controller it opens a web view controller which is not in UITabBarController.
In web view controller I have a back button on top with navigation bar. After pressing that back button it's coming back to third view controller. Again when I select another image in third view controller, it should open the web view controller, but the web view controller didn't appear on simulator instead saying error 1thread,1breakpoint.
Here is my code:
This code is in thirdviewcontroller after selecting the image
webViewController = [[AppsWebViewController alloc]init];
[self presentViewController:webViewController animated:YES completion:nil];
This code is in webviewcontroller after pressing the back button
[self dismissViewControllerAnimated:YES completion:nil];
I need the webviewcontroller to open every time after selecting any image in any of the four view controllers.
my thirdviewcontroller code:
#import "FreqAppsThirdViewController.h"
#interface FreqAppsThirdViewController ()
#end
#implementation FreqAppsThirdViewController
- (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.
icons = [NSArray arrayWithObjects:#"amazon-1.png",#"best_buy.png",#"Carl-Icahn-Lectures-Apple-Gambles-Netflix-and-Threatens-eBay-2.jpg",#"index.jpg",#"Office-Max.jpg", nil];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:_collectionView];
[super viewDidLoad];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return icons.count;
}
// The cell that is returned must be retrieved from a call to - dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[icons objectAtIndex:indexPath.row]]];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionViewdidSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com/"];
webViewController = [[FreqAppsWebViewController alloc]initWithURL:url andTitle:#"Amazon"];
[self presentViewController:webViewController animated:YES completion:nil];
} else if (indexPath.row == 1){
NSURL *url = [NSURL URLWithString:#"http://www.bestbuy.com/"];
webViewController = [[FreqAppsWebViewController alloc]initWithURL:url andTitle:#"Best Buy"];
[self presentViewController:webViewController animated:YES completion:nil];
} else if (indexPath.row == 2){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.ebay.com"]];
} else if (indexPath.row == 3){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.walmart.com"]];
} else if (indexPath.row == 4){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.officemax.com"]];
}
// datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
my webviewcontroller code:
#import "FreqAppsWebViewController.h"
#import "FreqAppsThirdViewController.h"
#import "FreqAppsAppDelegate.h"
#interface FreqAppsWebViewController ()
#end
#implementation FreqAppsWebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
if( self = [super init] ) {
theURL = url;
theTitle = string;
}
return self;
}
-(id)initWithURL:(NSURL *)url {
return [self initWithURL:url andTitle:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[webView loadRequest:requestObject];
}
- (IBAction) back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
webView.delegate = nil;
[webView stopLoading];
}
#end
my webviewcontroller.h code
#import <UIKit/UIKit.h>
#interface FreqAppsWebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UIWebView *webView;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
- (IBAction)back:(id)sender;
#end
my thirdviewcontroller.h code:
#import <UIKit/UIKit.h>
#import "FreqAppsWebViewController.h"
#interface FreqAppsThirdViewController:UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
UICollectionView *_collectionView;
NSArray *icons;
FreqAppsWebViewController *webViewController;
}
#end
Copy and paste the full error message you're getting.
If it is a breakpoint, not an error, as you seem to be indicating, it might just be that you clicked on a the margin of your source and set a breakpoint without realizing it.
To check for breakpoints:
Press Command 7 to display the breakpoint navigator and see if there are any breakpoints set in your code. If there are, you'll an outline starting with the target (the current app) then the source file .m, and then an entry listing a method name and line number, with a symbol that looks like a cross between a right-pointing arrow and a blue sticky note.
If there are breakpoints, select them one at a time and note where they are in your code. Are then in your IBAction method, or the one of the methods in your AppsWebViewController?
Delete each breakpoint by selecting it and pressing the delete key. Then run your program again.

Inconsistent behavior with Activity Indicator in iOS

In my app, I have viewcontrollers that include webviews with activity indicators and they work fine. Now,in a new viewcontroller with a webview and activity indicator I am getting some weird behavior. In the simulator the view loads but the activity indicator remains spinning even when the view is finished loading. I copied everything exactly from another viewcontroller with webview and activity indicator that is working, but the weird behavior is still their. The URLs are just regular small html files so it's not like either one will take a long time to load. I don't know if this is a glitch in xcode (ver 5.0), or most likely something I am overlooking in the code. The code for the working viewcontroller is below:
#import "ProgramPDFViewController.h"
#interface ProgramPDFViewController ()
#end
#implementation ProgramPDFViewController
#synthesize webView;
#synthesize activity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[TestFlight passCheckpoint:#"PDFProgram-info-viewed"];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButtonItem;
// Do any additional setup after loading the view.
NSString *httpSource = #"<myURL>";
NSURL *fullUrl = [NSURL URLWithString:httpSource];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
[webView loadRequest:httpRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)webViewDidStartLoad:(UIWebView *)WebView
{
[activity startAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)WebView
{
[activity stopAnimating];
activity.hidden = TRUE;
}
#end
The code from the one with the weird behavior is below:
#import "ComitteeMeetingsViewController.h"
#interface ComitteeMeetingsViewController ()
#end
#implementation ComitteeMeetingsViewController
#synthesize webView, activity;
- (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.
[TestFlight passCheckpoint:#"CommitteMeetings-info-viewed"];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButtonItem;
NSString *httpSource = #"<myOtherURL>";
NSURL *fullUrl = [NSURL URLWithString:httpSource];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
[webView loadRequest:httpRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)webViewDidStartLoad:(UIWebView *)WebView
{
[activity startAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)WebView
{
[activity stopAnimating];
activity.hidden = TRUE;
}
#end
Are you sure the methods are actually called?
Did you set the webView's delegate and included UIWebViewDelegate in the header file?
For troubleshooting: try adding
NSLog(#"webViewDidStartLoad");
and
NSLog(#"webViewDidFinishLoad");
to the corresponding methods and see if they show up in the log

iOS PopOver with Webview

I am trying to build a pop over that displays a webView. So far, I have a button that when pressed, generates a pop over but the web view does not display. Using NSlog, I can see that the viewDidLoad method is firing. Here is the code I have so far:
In the popover.h file:
#interface PopUpViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *liveTimingPopUp;
#end
In the popover.m file:
#import "PopUpViewController.h"
#interface PopUpViewController ()
#end
#implementation PopUpViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadWebView];
}
- (void) loadWebView
{
// Do any additional setup after loading the view.
NSString *fullURL = #"www.google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_liveTimingPopUp loadRequest:requestObj];
[self loadView];
NSLog(#"PopUpView Fired");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
In the control view containing the button that generates the popover .h file:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
{
UIInterfaceOrientation intOr;
// APICallsViewController *pendingApiCallsController;
}
#property (strong, nonatomic) IBOutlet UIWebView *liveVideoPage;
- (IBAction)liveTimingButton:(id)sender;
#property (strong, retain) UIPopoverController *popOver; //declare UIPopOver so that you have an object to work with
-(void) loadVideo;
#end
In the control view containing the button that generates the popover .m file:
#import "SecondViewController.h"
#import "PopUpViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadVideo];
}
- (void) loadVideo
{
NSString *fullURL = #"http://192.168.130.230:1935/live/camera.stream/playlist.m3u8";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_liveVideoPage loadRequest:requestObj];
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
intOr = self.interfaceOrientation;
if (intOr == UIInterfaceOrientationPortrait)
{
NSLog(#"portrait");
_liveVideoPage.frame = CGRectMake(10, 100, 600, 550);
}
else
{
NSLog(#"landscape");
_liveVideoPage.frame = CGRectMake(200, 100, 600, 550);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)liveTimingButton:(id)sender
{
if ([_popOver isPopoverVisible]) {
[_popOver dismissPopoverAnimated:YES];
}
else
{
PopUpViewController *PUVC = [[PopUpViewController alloc]init];//instantiate ViewController for popOver's content
_popOver = [[UIPopoverController alloc] /* create Popover*/initWithContentViewController:PUVC]; //fills popover with PopUpViewController
_popOver.popoverContentSize = CGSizeMake(800, 250);
[_popOver presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //specifies where the popover is coming from...the liveTiming Bar button
}
}
#end
Any help would be greatly appreciated!
where is the webview supposed to come from? The VC gets not passed a xib name.. so the outlet will never bet set

Resources