iOS button to webpage with uiwebview - iphone

For a current project I am embedding a mobile site into an iOS app. I've taken use of the goback() and goforward() functions, with UIWebView. But how would I make a custom function that onClick (or onTouch which ever is the correct term) Sends the UIWebView back to the page initially loaded? Any Help would be much appreciated as I have bene unable to find anything on the web thus far.

Add something like to the implementation file:
- (IBAction)loadPage:(id)sender;
{
NSURL *url = [NSURL URLWithString:#"http://www.stackoverflow.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
And add this to the header file (each line at the appropriate position):
- (IBAction)loadPage:(id)sender;
IBOutlet UIWebView *webView;
Then bind the button to loadPage within interface builder,
and bind webView to the UIWebView.
Response to comment:
I created this sample project, hopefully it helps:
- http://dd5.org/static/iPhone_Browser_Sample.zip
Binding the button:
Right-click and drag to the class where you added the code above.
Release the button. Now a little dark-gray panel should appear.
Then click on the loadPage: entry within that panel.
If the code was added correctly, the entry should show up there.

Related

UIWebView loading and general performance

I am using UIWebView to display textual content in my app (I store the content in local HTML files that I pack with the app). All together, I have three web views whose content I change dynamically based on user feedback.
Although some might argue that this is not the most accepted way, I find UIWebView very convenient to display formatted text, and modify that text using HTML if necessary. While this works 99% of the time, on occasion, I experience problems that generally fall into one of these categories:
Sometimes, the web view content loads slow and is late a second or so.
The content loads but is not showing. However, as long as, I touch the view (try to scroll or something) the content pops in.
A few times I received memory warnings (usually not long after the app's initial loading) that in no way affected the performance of my app. It logged the memory warning but the app worked like nothing happened.
This is the method I use to load content to my web views:
- (void)loadSimpleDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
Aside from this, the shouldStartLoadWithRequest delegate method is also implemented, always returning a YES.
My question is: Is there a way to improve the reliability/performance of my web views (in particular loading)? Is there something I have overlooked, did wrong, or do not know about?
Some additional info:
I am on iOS6 SDK, use ARC, and do everything programmatically (do not use IB or storyboard).
You have 2 options to check what's going on:
Implement webViewDidStartLoad & webViewDidFinishLoad delegate methods to check why the content isn't showing (may be the content isn't loaded yet).
read the html content to an NSString then use loadHTMLString:baseURL instead of using loadRequest and check if it loads faster.
Hope this could help.

How to Load a WebView pre-zoomed in? (In Xcode / Objective C)

I am loading a UIWebview of a hosted .jpg. It's actually a schedule, so it is a rather large image. Instead of having users have to zoom in right away, I would like to load the web view already zoomed in. Although I still need the user to be able to zoom in and out, and scroll. Basically I am just looking for an "initial" zoom. How would I accomplish this? Just FYI I put the method below I am using to load the image...Thanks!
// Webview code
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.
[webview loadRequest:requestObj];
In iOS 5 you can access the UIWebView's scrollView property and set the zoom level on it after the page has loaded. That would likely work as you wanted. To get the same thing on pre-iOS 5 you'd probably want to go through the UIWebView's subviews until you find a UIScrollView and do the same thing on that.
I don't think there's any other way to programatically zoom it. Unless you can do it with Javascript and execute some using UIWebView's stringByEvaluatingJavaScriptFromString: method.

How to program UItoolbar buttons to load a new website in UIWebView?

I am working on an IPhone app and i have a UIWebview that by default loads a url.
I need to create a UIToolbar with three buttons under UIWebview which will allow users to hit the buttons and load a new url in UIWebview?
you have to add three buttons to your toolbar( This can be easily done by using the Interface builder(IB) and connect them to there respective IBAction in the IB.
now when tap on a a button you can do like this.
you have a UIWebView say myWebView //declared in .h file and properly linked with the your View using IB.
when you are done with the basic initialization
for button1 press
-(IBAction)button1:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
}
so by clicking on the button1 google webpage is loaded in your webView.
do the same for other button also.
:)
These methods and properties of UIWebview maybe help:
- (void)reload;
- (void)stopLoading;
- (void)goBack;
- (void)goForward;
#property(nonatomic,readonly,getter=canGoBack) BOOL canGoBack;
#property(nonatomic,readonly,getter=canGoForward) BOOL canGoForward;
#property(nonatomic,readonly,getter=isLoading) BOOL loading;

How to create a button that links to Safari?

For legal reasons, I'm obligated to show Terms of Service in my application, a PDF on an external server. What I believe would be easiest to do would be to create a UIBarButton item and then create an IBAction that launches the link in Safari.
So I create a button:
IBOutlet UIBarButtonItem *legal;
Then I make it into a nonatomic property and synthesize it in my implementation file, right? I go on to create an IBAction:
-(IBAction)legalButtonPressed:(id)sender;
I go into my implementation file, and here's where the issue comes. When it comes to defining those actions, I become confused. As I am new to iOS development, I could use some guidance. I don't know how to force the link into safari in the action. Any help would be greatly appreciated!
Thanks!
IBOutlet and IBActions are used for Interface Builder connections. If you have your UIBarButtonItem on a xib file, you can connect its action to the controller by right clicking in the files owner object. The same with the outlet.
Once you have the action in the controller connected to the button in the xib file, (I see no need to get an outlet here), you just implement the method as follows:
-(IBAction)legalButtonPressed:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://yourdomain.com/legal.pdf"]];
}
this will automatically open safari with the given url
Take a look at UIApplication's documentation, it has a openURL method you might find useful.
Another option is to include your legal text in a UITextView.
Example:
By clicking Join you agree to company's
Terms of Service, found here: http://www.site.com/tos.html and
Privacy Policy, found here: http://www.site.com/privacy.html
Make sure Link Detection is turned on in the UITextView and it will automatically recognize URLs. Any clicks on those URLs will automatically launch Safari.
Although you can do this approach (i.e. launch the link in Safari). I would suggest that you try to keep the user as much as possible in your app. I am guessing this LEGAL TERMS is a HTML doc?
You can do that using UIWebView. Init a webView and do this -
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
This will open the url in your app only! You can make the UIWebView open up as a modal window or in many other ways...

Put a link on home screen of my app and connect it to WebView

In my app i want to put my website's url on home screen and on clicking on it i want it to be open as a WebView.
How should i go for this.
Thanks,
Previous commenter is incorrect. You can open any hyperlink either externally with Safari or internally with a UIWebView.
Add a UIWebViewController to your project. Then, instantiate an instance of a the UIWebViewController that will be shown inside your app--you'll do this by declaring a property & synthesizing it within your main view controller (which will need to be declared as a UIWebViewDelegate), such as:
#interface MyMainViewController: UIViewController <UIWebViewDelegate> {
// Your implementation code here
}
When a user taps the button (assuming you make it a button, rather than just a text hyperlink), you instruct your app to add the UIWebView to the view stack, loading the correct link. You'll want to either do this within a modal view or within a navigation stack so your users can get back out of the web view, of course.
In your MyMainViewController implementation file, something like this:
-(void) showWebView {
// NOTE: I have not tested this, just prototyping
// off the top of my head
UIWebView *myWebView = [[UIWebView alloc] init];
myWebView.delegate = self;
NSURL *homeUrl = [[NSURL alloc] initWithString:#"http://example.com"];
NSURLRequest *homeRequest = [NSURLRequest requestWithURL:homeURL];
[myWebView loadRequest:homeRequest];
[self.presentModalViewController: myWebView animated:YES];
// Don't forget to release objects when you're done
[myWebView release]; // etc.
}
Now, this is off the top of my head from what I know and have done. But I hope you get the general idea. I offer no warranty of any kind here, but do guarantee this is entirely possible with minimal headache. If you get stuck, check out the developer references for UIWebView. Apple's docs are top-notch & show great examples to get you up and running quickly.
Best.