Translucent , setStatusBarStyle, UIWebView - ios

I'm working on the UIWebView and what I want is for the webView not to over rap the view.
so here is the question. I set the UIWebView ,and the status bar is overlaid over the content.
How can I handle this? I coded as when I made the UIImage view, but at that time there is the navigation bar, and it worked well.
Also I did setFrame:CGRectMake(0, 20).... but also doesn't work. why the [ setFrame]; method doesn't work? Any ideas please.
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault];
[self.webView setFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *myURL = [NSURL URLWithString:#"http://www.amazon.com"];
NSURLRequest *myURLReq = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:myURLReq];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
#end
UPDATE: I switched off the Auto-layout and change setFrame:CGRectMake(0, 20) and now they are not overlaid. But I wonder why we need to set this? If I set the status bar translucent , it makes sense. But I set it as the default, which is supposed to start to draw the screen right below the status bar at (0,20).

Try this one
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault];
[self.webView setFrame:CGRectMake(0.0, 20.0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *myURL = [NSURL URLWithString:#"http://www.amazon.com"];
NSURLRequest *myURLReq = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:myURLReq];
}
Your autosize masks should look like this.
Final Output:

Add following code in your viewDidLoadMethod :
if( [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
self.edgesForExtendedLayout = UIRectEdgeNone;
}

Related

How to get a activityindicator stop when the web load already?

I have a webview and an activityIndicator, I want that the activityIndicator stop when the URL load already. I have this:
XYZViewController.h
#interface XYZViewController : UIViewController <UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *browser;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
#end
XYZViewController.m
-(void)viewDidLoad {
[super viewDidLoad];
self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com"]];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.browser loadRequest:request];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
}
AppDelegate.m
XYZViewController* viewVC = [XYZViewController alloc]init];
viewVC.browser.delegate = viewVC;
I dont know that is wrong?
Anybody help me please?
Do what #puzzl suggested and then add it to webViewDidStartLoad: as opposed to viewDidLoad
- (void)viewDidLoad {
...
self.browser.delegate = self
...
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];
}

Open web page in UIWebView after click on the button

I need help. I have two button, and I need each button to open a different web page in my UIWebView (for example: button1 open website apple.com, button2 open google.com). I can not find any tutorial that would help me. Thx for help (I use Storyboard).
There is my code, but something is wrong, because I see only a black screen (after click on the button)
TabulkaViewController.m
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 240, 300, 35);
[myButton setTitle:#"Buttonone" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: myButton];
}
-(void) myButtonClick:(NSString *)myString
{
NSURL *url = [NSURL URLWithString:#"http://www.apple.com/"];
WebViewController *viewWeb = [[WebViewController alloc] initWithURL:url andTitle:#"Apple"];
[self presentViewController:viewWeb animated:YES completion:nil];
}
WebViewController.h
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
#property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
#end
WebViewController.m
#import "WebViewController.h"
#implementation WebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[_viewWeb loadRequest:requestObject];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (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)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
_viewWeb.delegate = nil;
[_viewWeb stopLoading];
}
#end
When working with storyboard, it would help if you upload your project file somewhere.
But from experience you need to set your webView delegate to your class WebViewController
you can do it by adding this in your viewDidLoad:
self.webView.delegate = self;
then you must implement the shouldStartLoadWithRequest delegate call, this is a good start.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
let me know if this fixes it.
EDIT:
after checking your project, change WebViewController->viewDidLoad to this:
- (void)viewDidLoad
{
[super viewDidLoad];
// init webview
_viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];
_viewWeb.delegate = self;
// add it to this controller's view
[self.view addSubview:_viewWeb];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[self.viewWeb loadRequest:requestObject];
}
your _viewWeb wasn't initialized as I suspected, See my comments on how to initialize it and how to add it to your WebViewController
EDIT2:
Added constraints in for the web view to take full screen, I recommend you read-up about auto layout in apple docs
- (void)viewDidLoad
{
[super viewDidLoad];
// init webview
_viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];
_viewWeb.delegate = self;
// add it to this controller's view
[self.view addSubview:_viewWeb];
// add constraints
NSDictionary* dict = #{#"viewWeb":_viewWeb};
_viewWeb.translatesAutoresizingMaskIntoConstraints = NO;
NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
[self.view addConstraints:constraints];
webTitle.title = theTitle;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
[self.viewWeb loadRequest:requestObject];
}

UIWebView opening external links

I have this uiwebview code which on the main works except for the part that forces any links to not open with the uiwebview (basically I would like links to open in the browser)
Can anyone point me in the right direction or see what is wrong with my code
JGViewController.h
#import
#interface JGViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *customWebView;
}
#property (strong, nonatomic) IBOutlet UIWebView *customWebView;
#end
JGViewController.m
#import "JGViewController.h"
#interface JGViewController ()
#end
#implementation JGViewController
#synthesize customWebView;
//force any links to open in browser rather than in the uiwebview
-(BOOL) webView:(UIWebView *)customWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL* url = [request URL];
if (UIWebViewNavigationTypeLinkClicked == navigationType) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *httpSource = #"http://www.google.co.uk";
NSURL *fullUrl = [NSURL URLWithString:httpSource];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
customWebView.scalesPageToFit = YES;
customWebView.frame=self.view.bounds;
[customWebView.scrollView setShowsHorizontalScrollIndicator:NO];
customWebView.scrollView.scrollEnabled = NO;
customWebView.scrollView.bounces = NO;
//actually call the page into the uiwebview
[customWebView loadRequest:httpRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
fixed by adding
self.webview.delegate = self;
to viewDidLoad function

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"];

Is there an easy way to create thumbnails of various document formats like Word, Excel on iOS?

Is there a built-in mechanism in the iOS SDK that I could use to render thumbnails (UIImage) of arbitrary document types, like Word, Excel, PDF, image formats, etc - whatever iOS is capable of previewing?
It only ever needs to create a thumbnail of the first page.
I have code to scale down PDFs and images but I thought, as iOS can preview Word and others, maybe there is something built in?
use the trick to open office document file (docx, xls, pptx), display all pages in thumbnail bar, let the user to open any page in the same view while taping on any thumbnail.
Here are the steps
Open file in UIWebView
Take screenshot of every page and add in scroll-able thumbnail view bar(recursive)
Add a custom button over each thumbnail image to enable user interaction
Import QuartzCore.framework
Confugure.h file like
#import <UIKit/UIKit.h>
#include <QuartzCore/QuartzCore.h>
#interface ViewController : UIViewController<UIWebViewDelegate>
{
BOOL finished;
float currentOffset;
float pointToAddNextThumbnail;
NSURLRequest *request;
int totalFileHeight;
int currentExecutingSnapshot;
}
#property(nonatomic,retain) UIWebView* webView;
#property(nonatomic,retain) UIScrollView* thumbnailScrollView;
#end
.m file like
#import "ViewController.h"
#interface ViewController ()
#end
#define pageHeight 360
#define pageWidth 320
#define thumbnailHeight 88
#define thumbnailWidht 70
#implementation ViewController
#synthesize webView,thumbnailScrollView;
#pragma mark
#pragma mark VC Delegates
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Reader" ofType:#"doc"];
NSURL *url = [NSURL fileURLWithPath:path];
request = [NSURLRequest requestWithURL:url];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];
[self.webView setScalesPageToFit:YES];
self.webView.delegate = self;
[self.webView loadRequest:request];
[self.view addSubview:self.webView ];
self.thumbnailScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, pageHeight+5, pageWidth, thumbnailHeight)];
[self.thumbnailScrollView setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:self.thumbnailScrollView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
#pragma mark webView Delegate Methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// NSLog(#"webViewDidFinishLoad");
CGSize z = self.webView.scrollView.contentSize;
totalFileHeight= z.height;
NSLog(#"totalFileHeight in pxls %d",totalFileHeight);
totalFileHeight=totalFileHeight/pageHeight;
NSLog(#"totalFileHeight in pages %d",totalFileHeight);
[self takeScreenShotAndReloadWebViewWithPage];
}
#pragma mark
#pragma mark utitlityMehtods
-(void)takeScreenShotAndReloadWebViewWithPage
{
float widthOfThumbnailForScrollView = (thumbnailWidht+5);
float widhtOfScrollViewContant = (currentExecutingSnapshot*widthOfThumbnailForScrollView)+widthOfThumbnailForScrollView;
self.thumbnailScrollView.contentSize = CGSizeMake(widhtOfScrollViewContant,50);
self.webView.scrollView.contentOffset = CGPointMake(0, currentOffset);
// [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0,%f)",currentOffset]];
//take Snapshot
UIGraphicsBeginImageContext(CGSizeMake(320, pageHeight));
[self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//set frames of thumbailButton and thumnailImage
CGRect thumbnailFrame = CGRectMake(pointToAddNextThumbnail, 0, thumbnailWidht, thumbnailHeight);
UIButton *thumbnailButton = [[UIButton alloc] initWithFrame:thumbnailFrame];
UIImageView *thumbnailImage =[[UIImageView alloc] initWithFrame:thumbnailFrame];
[thumbnailImage setImage:img];
thumbnailButton.tag = currentOffset;
[thumbnailButton setBackgroundColor:[UIColor clearColor]];
[thumbnailButton addTarget:self action:#selector(thumnailButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.thumbnailScrollView addSubview:thumbnailImage];
[self.thumbnailScrollView addSubview:thumbnailButton];
[thumbnailImage release];
[thumbnailButton release];
pointToAddNextThumbnail = pointToAddNextThumbnail+thumbnailWidht+5;
currentOffset = currentOffset + pageHeight;
if (currentExecutingSnapshot < totalFileHeight)
{
NSLog(#"currentExecutingSnapshot %d",currentExecutingSnapshot);
currentExecutingSnapshot ++;
//[self.webView loadRequest:request];
//make a recursive call and take snapshot of all pages
[self takeScreenShotAndReloadWebViewWithPage];
}
else
{
[self finishedFethingThumbnails];
}
}
-(void)finishedFethingThumbnails
{
self.webView.scrollView.contentOffset = CGPointMake(0, 0);
}
#pragma mark
#pragma mark IBaction
-(IBAction)thumnailButtonClicked:(UIButton*)sender
{
[[self.webView scrollView] setZoomScale:0 animated:YES];
[[self.webView scrollView] setContentOffset:CGPointMake(0, sender.tag) animated:YES];
}
#pragma mark
#pragma mark dealloc
-(void)dealloc
{
[webView release];
[thumbnailScrollView release];
[super dealloc];
}
#end
Download sample project from http://abdus.me/downloads/testThumbnail.zip

Resources