After viewing web in app, cannot return to app without closing it and relaunching - xcode

I have an app which has several different buttons leading to various views. One of the views will open a website in Safari on iPhone which is what I want to happen. THe problem is that if I then want to go back to the app, all I get is a white screen, and I have to double click on home button, close the app and relaunch it.
Would this relate to releasing memory or view perhaps? I'm not sure how to go about fixing it so after viewing the web page in safari I can then go back to the app and continue with other views.
Here is what I am using in webViewController.m
#import "webViewController.h"
#interface webViewController ()
#end
#implementation webViewController
- (void)viewDidLoad
{
NSURL *url = [ [ NSURL alloc ] initWithString: #"http://www.mydomain.com" ];
[[UIApplication sharedApplication] openURL:url];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end

you can use webView to load this url, then you can make a back button on this webview to come back to your app without leaving the app.
UIWebView * webView =[[UIWebView alloc]init];
webView.backgroundColor = [UIColor clearColor];
[webView setFrame: CGRectMake(0, 51, 320, 398)];
NSURL *url = [NSURL URLWithString:#"http://www.mydomain.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
webView loadRequest:requestObj];
[self.view addSubview:webView];
You can also use some delegate methods of webView to know whether your page has been loaded or not
-(void)webViewDidStartLoad:(UIWebView *)webView
{
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}

Ah I've figured it out, I've added
web view.scalesPagesToFit = Yes
statement and opening the webpage in the webview works exactly how I wanted it to.

Related

How to display a image when there is no internet connection (web view)

I have a web view based program I was just testing. How do you display a image when there is no internet connection?
I have a basic webview
//
// XYZViewController.m
// Phantomore
//
// Created by Kevin Jin on 5/15/14.
// Copyright (c) 2014 Kevin Jin. All rights reserved.
//
#import "XYZViewController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#end
Thats it,I just want to know what I have to do to have it displaying a image when tehre is no internet connection
I just simplified mamnun answer(Get Reachability here).
You can use this method after you have implemented the reachability class
- (BOOL)isConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
In any of your class
- (void)viewDidLoad
{
[super viewDidLoad];
if([self isConnectedToInternet]){
//Hide your imageview
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
else{
// Hide your webview
// Show your imageview
// Do not forget to import reachability class on top
}
}
There is two way of doing it.
Before even performing the url request, check if internet connection is available. Use Apple provided Reachability classes or better yet use the open-source version. If internet is available load the request. If not add an UIImageView with your preferred UIImage as a subview of the webview. You can do something like this:
//inside #interface
#property(nonatomic, strong) UIImageView *imageView;
//inside -(void)viewDidLoad
self.imageView = [UIImageView alloc] initWithImage:[UIImage imageNamed:#"as.as"];
Reachability* reach = [Reachability reachabilityWithHostname:#"www.google.com"];
reach.reachableBlock = ^(Reachability*reach) {
// load request
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
};
reach.unreachableBlock = ^(Reachability*reach) {
//show imageview
[_viewWeb addSubView:self.imageView];
};
[reach startNotifier];
Implement UIWebView delegate. In the – webView:didFailLoadWithError: method, check the error. If the error is caused by no internet connection, add the imageview(same as before).

Chromium Tabs are reseting flash content in WebView

I'm working with https://github.com/rsms/chromium-tabs in my project and I have created my app so the tabs are registered to a WebView. When I'm debugging I noticed that when I was viewing a Youtube video or any flash content and I switched tabs then switch back the content reloaded or the video restarted. Is there any reason this should hapen.
When a tab is selected it is set: [self becomeFirstResponder];
Other than that I have found no other connected way the flash would reset and the rest of the page would remain the same. I'm believing its a setting in WebView whenever it is not visible.
For the comment bellow:
I followed the examples on Github and used MyTabContents.mm to create the contents. Also used a custom webview.
MyTabContents.mm:
-(id)initWithBaseTabContents:(CTTabContents*)baseContents {
if (!(self = [super initWithBaseTabContents:baseContents])) return nil;
ProgressWebView *wv = [[ProgressWebView alloc] initWithFrame:NSZeroRect];
[wv setProgressDelegate:self];
[[wv mainFrame] loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:#"https://google.com"]]];
[wv setShouldUpdateWhileOffscreen:YES];
webView = wv;
// Create a NSScrollView to which we add the NSTextView
NSScrollView *sv = [[NSScrollView alloc] initWithFrame:NSZeroRect];
[sv setDocumentView:wv];
[sv setHasVerticalScroller:NO];
// Set the NSScrollView as our view
self.view = sv;
return self;
}
AppDelegate.mm:
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
CTBrowserWindowController* windowController =
[[CTBrowserWindowController alloc] initWithBrowser:[MyBrowser browser]];
[windowController.browser addBlankTabInForeground:YES];
[windowController showWindow:self];
}
MyBrowser.mm:
-(CTTabContents*)createBlankTabBasedOn:(CTTabContents*)baseContents {
// Create a new instance of our tab type
MyTabContents *contents = [[MyTabContents alloc]
initWithBaseTabContents:baseContents];
[self changeLayout];
return [contents autorelease];
}

XCode Prevent UIWebView flashing white screen after Launch Image

I am fairly new to XCode - I'm designing an app with a single view that just displays a UIWebView that loads my jQuery mobile web site.
I have the Default.png and Default#2x.png files for the Launch images in place. When I run the app on my test device or in the emulator, it shows my Launch Image, and then flashes a white screen while the UIWebView loads the first page.
I know that I can change the background color and opacity so it will flash a different color than white, but I would like to prevent the flash altogether. I would like the app to launch, show the Launch image, and not show the UIWebView until the first page has completely loaded. Help?
# Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")
First, add the name-of-loading-image.png and name-of-loading-image#2x.png files to your Xcode project. The regular image should be 320 x 460, and the #2x image should be 640 x 920.
Then, in my ViewController.h file I added these properties:
#property (nonatomic, retain) UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
And then in my ViewController.m file I added this:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
#synthesize webView;
#synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"name-of-loading-image.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"name-of-loading-image.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
To know when your webView has finished loading, you must implement the UIWebViewDelegate protocol.
In your viewController's .h file:
#interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
#end
Then in your viewController's .m file, add this method:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
You can use the -viewDidLoad method in your viewController's .m to continue displaying your launch image. You can also begin the loading of content for your webView:
- (void)viewDidLoad {
[super viewDidLoad];
// continue to display launch image
UIImage *launchImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"launchImage.png" ofType:nil ]];
UIImageView *launchImageView = [[[UIImageView alloc] initWithImage:launchImage] autorelease];
[self.view addSubview:launchImageView];
// now setup the base view for the webView controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
// for rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//create a frame to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//determine the path the to the index.html file in the Resources directory
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
}

Load a PDF from internet in xcode

I have a view controller that has a UIWebView object within it.
I would like to load and display a PDF from the internet within the UIWebView.
I have the code working to display the a PDF loaded from the resource bundle, but i am lost on having the PDF loaded from the internet.
I was thinking it would be as simple as changing the fileURLWithPath to point to the location on the internet... although the form displays, it does not show the contents. So I am assuming that I have the wrong URL in the string???
Does anyone have a code snippet to load and display a PDF from the internet within a UIWebView that they would be willing to share???
thanks
tony
I take it you are looking at iOS stuff, not Mac. On iOS, it should really be just as simple as
NSURL* url = [NSURL URLWithString:stringURL];
[aWebView loadRequest:[NSURLRequest requestWithURL:url]];
Use the following for xCode 5.
Interface section - myWebView is the outlet to the UIWebView in my storyboard.
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#property NSURL *url;
The rest goes into viewDidLoad section of the ViewController class.
- (void)viewDidLoad
{
[super viewDidLoad];
_url = [[NSURL alloc] init];
_url = [NSURL URLWithString:#"http://deimos3.apple.com/WebObjects/Core.woa/FeedEnclosure/utah.edu.1668842900.01668842919.1681195338/enclosure.pdf"];
[_myWebView loadRequest:[NSURLRequest requestWithURL: _url]];
}
This worked for me but there maybe a more efficient way of doing this.
Here is how i implemented it....
visitNoteViewController *controller = [[visitNoteViewController alloc] initWithNibName:#"visitNoteView" bundle:nil];
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
controller.pdfUrl = [NSURL URLWithString:#"http://deimos3.apple.com/WebObjects/Core.woa/FeedEnclosure/utah.edu.1668842900.01668842919.1681195338/enclosure.pdf"];
[self presentModalViewController:controller animated:YES];
[controller release];
got it working.

Custom NSScroller issues

I'm trying to subclass NSScroller in order to draw my own scroller knob. To do this, I've subclassex NSScrollView and usex the following code to instantiate my custom NSScrollers:
- (void)awakeFromNib;
{
NSRect horizontalScrollerFrame = [[self horizontalScroller] frame];
NSRect verticalScrollerFrame = [[self verticalScroller] frame];
NSString *scrollBarVariant = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain] valueForKey:#"AppleScrollBarVariant"];
if (![scrollBarVariant isEqualToString:#"DoubleBoth"]) {
[self setVerticalScroller:[[[TRScroller alloc] initWithFrame:verticalScrollerFrame] autorelease]];
[self setHorizontalScroller:[[[TRScroller alloc] initWithFrame:horizontalScrollerFrame] autorelease]];
}
}
This works and my NSScrollers display correctly. But I'm occasionally seeing rendering issues upon first loading my application. Within Interface Builder I have laid out a number of NSScrollViews with their scrollbars set to hide automatically. The issue I'm seeing is that when the application first loads, the scrollbar backgrounds are rendered across the NSScrollViews contents.
alt text http://www.freeimagehosting.net/uploads/1d3fc75db8.png
I believe this is because I instantiate my NSScroll subclass (TRSubclass) via awakeFromNib, which means that the scrollbars are given the frame of the NSScrollView before it is automatically resized to meet the windows saved location and size (in other words, it's using the frame that's assigned by default within Interface Builder). What's the best way around this?
I've tried forcing the NSScrollView to redisplay (using setNeedsDisplay: and display:) but with no luck. Has anyone else come across a similar issue?
I'm using the same schema in my applications and I fighted this issues a lot. I use the same trick: scrollers are substituted in [scrollView awakeFromNib] methods, but I don't face such rendering issues at the moment. You can try to play with "draws background" property of the NSScrollView - it really helps sometimes
- (void)changeSubs
{
// change clip view
// ...
// change scrollers
NSRect horizontalScrollerFrame = [[self horizontalScroller] frame];
NSRect verticalScrollerFrame = [[self verticalScroller] frame];
if (![[self verticalScroller] isKindOfClass:[CRScroller class]])
[self setVerticalScroller:[[[CRScroller alloc] initWithFrame:verticalScrollerFrame] autorelease]];
if (![[self horizontalScroller] isKindOfClass:[CRScroller class]])
[self setHorizontalScroller:[[[CRScroller alloc] initWithFrame:horizontalScrollerFrame] autorelease]];
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self changeSubs];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
NSKeyedUnarchiver* unpacker = (id)aDecoder;
[unpacker setClass:[CRClipView class] forClassName:[NSClipView className]];
[unpacker setClass:[CRScroller class] forClassName:[NSScroller className]];
self = [super initWithCoder:aDecoder];
if (self)
{
}
return self;
}
- (void)awakeFromNib
{
[self changeSubs];
}
There are few tricks here, they work depending on a way NSScrollView is created. 'isKindOfClass' check helps to avoid double-swap.

Resources