How to create new view w/ webview from push of button - iphone

UPDATED QUESTION:
Hello , so I was able to create the modal view curl up overlay for this use. However I now have the issue that I can click on the button1 and pull up second view with the url1 on webView1, but when I click button2 I receive the display of url1 on webview also. below is the code can you help me out on what I'm doing wrong?
FourthViewController.h has the following code:
'
#import <UIKit/UIKit.h>
#interface FourthViewController : UIViewController {
UIButton *openBlogButton, *openFbButton, *openYelpButton;
}
#property (nonatomic, retain) IBOutlet UIButton *openBlogButton, *openFbButton, *openYelpButton;
-(IBAction)openBlog:(id)sender;
-(IBAction)openFacebook:(id)sender;
-(IBAction)openYelp:(id)sender;
#end
'
FourthViewController.m has the following code:
'
#import "FourthViewController.h"
#import "FourthBlogWebViewController.h"
#import "FourthFBWebViewController.h"
#implementation FourthViewController
#synthesize openBlogButton, openFbButton, openYelpButton;
-(IBAction)openBlog:(id)sender {
FourthBlogWebViewController *sampleView = [[[FourthBlogWebViewController alloc] init] autorelease];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
//THIS IS WHERE I THINK THE ISSUE IS, WHEN I CLICK openFacebook it should lead to the fb URL, it seems as if I should be able to list all possible urls on the first fourthwebviewcontroller, but i was not able to be successful so I tried making a second webview controller to feed it this url but this failed...lol. thnx for any input.
-(IBAction)openFacebook:(id)sender {
FourthFBWebViewController *sampleView = [[[FourthFBWebViewController alloc] init] autorelease];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
-(IBAction)openYelp {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.yelp.com"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[openBlogButton release];
[openFbButton release];
[openYelpButton release];
[super dealloc];
}
#end
'
FourthBlogWevViewController.h
'
#import <UIKit/UIKit.h>
#interface FourthBlogWebViewController : UIViewController {
UIButton *dismissViewButton;
IBOutlet UIWebView *webViewFB;
}
#property (nonatomic, retain) IBOutlet UIButton *dismissViewButton;
#property (nonatomic, retain) UIWebView *webViewFB;
- (IBAction)dismissView:(id)sender;
#end'
FourthBlogWebViewController.m code:
'#import "FourthBlogWebViewController.h"
#implementation FourthBlogWebViewController
#synthesize dismissViewButton, webViewFB;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
}
-(IBAction)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.facebook.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webViewFB loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[dismissViewButton release];
[webViewFB release];
[super dealloc];
}
#end
Where or how would I add the second url and have it distinguis on click of other buttons. Thank you in advance for the ongoing help.
Original Question:
Hello,
So first app and I'm trying to make some basic function use of buttons, Im doing this in a tab bar application. On my fourth tab view a view opens with 3 buttons, on the click of the button I am trying to get a webpage to load within the app (i was able to successfully make the webpages open via safari on button touchup inside click, but that is not how I want it to load. So I edited the way it loads and on push of button the website is now opening within the webView. However I want to create this webview on a new view rather then on the same view where the 3 buttons are, but I'm not clear on how to do this. Any input would be greatly appreciated (I apologize beforehand since this seems as an easy task, but haven't figured it out yet, I did some searching before asking, but was unable to find anything that showed how to do this on push of different button to load webview on a new view within the same XIB.
Thanks.
Below is the code that I have for the load of webview on push of button.
On fourthviewcontroller.h
#import <UIKit/UIKit.h>
#interface FourthViewController : UIViewController {
IBOutlet UIWebView *webView;
}
-(IBAction)openWebsite:(id)sender;
-(IBAction)openButton2;
-(IBAction)openButton3;
#property (nonatomic, retain) UIWebView *webView;
#end
On fourthviewcontroller.m
#import "FourthViewController.h"
#implementation FourthViewController
#synthesize webView;
-(IBAction)openWebsite:(id)sender{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.URL.com"]]];
}
-(IBAction)openButton2:(id)sender{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.URL2.com"]]];
}
-(IBAction)openButton3:(id)sender{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.URL3.com"]]];
}
Basically I'm trying to get different buttons to push to the same new view but the only difference is that i want the uiwebview to display a different URL depending on the button click that bring the new view.

As NWCoder mention, it depends how you like the new webview to be shown.
Display in UINavigationController-
Make your fourth tab a uinavigationcontroller and set the 3 button view controller as the top most view controller.
Create a new UIViewController that encapsulate the UIWebView.
When user click on the open webview button, create and push the uiviewcontroller that contains the uiwebview
Display in modal view:
Create a new UIViewController that encapsulate the UIWebView.
When user click on the open webview button, create and push the webview uiviewcontroller by calling the fourth's tab uiviewcontroller presentModalViewController:animated.

Related

How to allow UITapGestureRecognizer to detect taps on web view?

I have made web browser for iPhone in Xcode, and to exit the web browser and go back to the main page you tap on the screen three times, except that the gesture will only work on the toolbars at the top and bottom of the screen but not on the web view.
What can I do?
Here is my code
.h File
#import <UIKit/UIKit.h>
#interface Internet : UIViewController {
IBOutlet UIWebView *webview;
IBOutlet UIBarButtonItem *forwards;
IBOutlet UIBarButtonItem *backwards;
IBOutlet UITextField *URLTextField;
}
- (IBAction)share:(id)sender;
- (IBAction)returnKeyPressed:(id)sender;
- (IBAction)google:(id)sender;
- (IBAction)reload:(id)sender;
- (IBAction)forwardsButton:(id)sender;
- (IBAction)backwardsButton:(id)sender;
#end
.m File (only relevant code shown)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *homePageString = #"http://www.google.com";
NSURL *homePage = [NSURL URLWithString:homePageString];
NSURLRequest *requestHomePage = [NSURLRequest requestWithURL:homePage];
[webview loadRequest:requestHomePage];
forwards.enabled = NO;
backwards.enabled = NO;
UIAlertView *touchHelp = [[UIAlertView alloc] initWithTitle:#"Alert, Please Read" message:#"To go back to the App Selection Page please anywhere on the top or bottom toolbar three times" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[touchHelp show];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:tapRecognizer];
}
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer {
[self dismissViewControllerAnimated:YES completion:nil];
}
Implement shouldRecognizeSimultaneouslyWithGestureRecognizer delegate and return YES.
Implementation details:
Add the UIGestureRecognizerDelegate protocol to your .h file
#interface ViewController : UIViewController <UIGestureRecognizerDelegate>
Add this delegate in your .m file
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES
}

how to change WebView with buttons in separate View Controller

I need to change the URL of a WebView (located in WebViewViewController) using buttons in View Controller 1 (called PracticeViewController). I totally understand how to change the web View when it is in the same View Controller as the buttons, but I don't understand how to get the buttons to affect the WebView when the WebView is in a different ViewController than the buttons. Below is the code I currently have:
PracticeViewController.h
#import <UIKit/UIKit.h>
#interface PracticeViewController : UIViewController
- (IBAction)passGoogleButton:(id)sender;
- (IBAction)passYahooButton:(id)sender;
- (IBAction)passBingButton:(id)sender;
/* ok I tried to make this as clear as possible, and you can look at the other files if you don't understand what I mean, but at its core I want to change the web site loaded by the Web View by clicking on the buttons in the other View Controller */
#end
space (BTW is there an easier way to post on StackOverFlow Besides pressing space 4 times for every line of code? am I missing something?)
PracticeViewController.m
#import "PracticeViewController.h"
#import "WebViewViewController.h"
#interface PracticeViewController ()
#end
#implementation PracticeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)passGoogleButton:(id)sender {
WebViewViewController.webSiteURL = #"http://www.google.com";
/* This supposedly works on a tutorial I saw, but Xcode flags it with the error 'Property webSiteURL not found on object of type 'WebViewViewController'' */
}
- (IBAction)passYahooButton:(id)sender {
WebViewViewController.webSiteURL = #"http://www.yahoo.com";
//same error as above
}
- (IBAction)passBingButton:(id)sender {
WebViewViewController.webSiteURL = #"http://www.bing.com";
//same error as above
}
#end
space
WebViewViewController.h
#import <UIKit/UIKit.h>
#interface WebViewViewController : UIViewController
{
UIWebView *myWebView;
NSString *webSiteURL;
}
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#property (strong, nonatomic) NSString *webSiteURL;
/* I don't entirely understand what 'strong' means but I know it has to do with memory retention and that it (supposedly) should be used when I wan to transfer the value of the string 'webSiteURL' to the other ViewController */
#end
space
WebViewViewController.m
#import "WebViewViewController.h"
#interface WebViewViewController ()
#end
#implementation WebViewViewController
- (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.
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webSiteURL]]];
/* As you can see above, the WebView loads from the string 'webSiteURL' but I can't figure out how to assign different values to it based on what button is clicked in the other View Controller so that it will run in ' - (void)viewDidLoad.' I already know how to transfer the value of the string to other things within another view controller (like into a label or a text field) but what I really need to know is how to get the string 'webSiteURL' to change before the view is loaded */
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You have the right idea but you are missing a step here. You are never creating an instance of webViewController.
What you need somewhere is...
webViewcontroller *MyWebController = [[webViewController alloc] init];
It depends on how you are presenting the WebViewController. If you simply want the button to pop up the WebViewController modally you could use the following:
- (IBAction)passGoogleButton:(id)sender {
webViewController *myWebVC = [[webViewController alloc] init];
[myWebVC setWebSiteURL:#"http://www.google.com"];
[self presentViewController:myWebVC animated:YES completion:nil];
}
So, you can see here. You create the instance of the webViewController called myWebVC. Then you pass the string to it. So when myWebVC loads and it hits viewDidLoad, it will use the string that you passed in already and load the web view with that content. Hope that helps.
Also, Make sure you #synthesize your properties in your .m files.
You can use nsnotificationcenter to accomplish this task.
First register to notificationcenter in WebViewController, then send notification from another controller.
Here's a sample for registering to notificationcenter in WebViewController
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(nMessageOpenURL:) name:#"nMessageOpenURL" object:nil];
This is the code to run the logic when you receive the notification. (in WebViewController)
- (void)nMessageOpenURL:(NSNotification *)note {
//run the logic in here.
}
And finally this is the code where you trigger the notification.
[[NSNotificationCenter defaultCenter] postNotificationName:#"nMessageOpenURL" object:nil];
Here's the Apple documentation.
I got to know that you are trying to pass the value of the string of url between two view controllers but you are not getting it right!!
So if you are using storyboard you can try out passing this value using Segue. For Eg: you can use prepareForSegue to set the different values using if-else construct.
And if you are not using segue the best practice to pass those values will be using delegate.
Just create a delegate method and pass the url string as an argument.
Hope this works :)

MWFeedParser in-app web browser INSTEAD of safari

So i'm using the MWFeedParser, which I think is a beautiful RSS feed app, but when clicking on a link it goes to Safari. I fear that a good portion of iPhone users are unaware of the fact that they can double-click on the iPhone's one and only button to view all open apps and go back to the RSS feed, and will thus get stuck. Therefore I'd rather use an in-app browser.
In other words, currently when I click on a link from MWFeedParser it goes to Safari; I prefer it goes to an in-app browser.
I have the browser class set up called WebViewController.
basically with [detailWebView loadRequest:[NSURLRequest requestWithURL:detailURL]];
So "detailURL" is what it will look for
What do I put in the DetailTableViewController instead of the sharedApplication code???
bummer nobody was able to answer...in case others are looking for how to do it, change the contents of didSelectRowAtIndexPath to
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
MWFeedItem *entry = [parsedItems objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
// Deselect
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
Add a WebViewController;
the .m has
#import "WebViewController.h"
#import "MWFeedItem.h"
#implementation WebViewController
#synthesize webView = _webView;
#synthesize entry = _entry;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:_entry.link];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)viewWillDisappear:(BOOL)animated {
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[_entry release];
_entry = nil;
[_webView release];
_webView = nil;
[super dealloc];
}
And the .h has
#class MWFeedItem;
#interface WebViewController : UIViewController {
UIWebView *_webView;
MWFeedItem *_entry;
}
#property (retain) IBOutlet UIWebView *webView;
#property (retain) MWFeedItem *entry;
(Basically I just took the webview part of Ray Wenderlich's RSS feeder and threw it in to this one.)

RootViewController - iPhone

I am new to iPhone programming and trying to grasp the concept of RootViewController.
Scenario:
I have 3 views
RootViewController - Parent View
SportsViewController - Sub View 1
CricketViewController - Sub View 2
Both the subview have to be in FullScreenMode, so tab-bar or navigation bar cannot be used.
At first, Sub View 1 is loaded which is having some content and a DONE button on it.
Once user press DONE button then Sub View 1 has to be unloaded and RootViewController should load the Sub View 2.
Query
I have successfully displayed SubView 1 and when user taps on DONE then I can unload it. But I did n't get how should I notify the RootViewController from Sub View 1 that Sub View 1 has unloaded and now it should load the Sub View 2?
Thanks in Advance
Paras Mendiratta
I think the easiest solution here is to use UINavigationController and just hide the navigation bar. You can use -setNavigationBarHidden:animated: to hide (or show) the nav bar.
one way is to implement a method like - (void)loadSecondView which does all u want to do when first view is unloaded. and then int doneButtonClicked-method u call this method like this: [super loadSecondView]; and also remove first view from superview.
I assume that the screen with the "Done" button is some kind of login screen. You actually do not need all the view controllers you have defined to do what you want.
Instead you could do like this:
At application launch set CricketViewController as the root view controller.
Immediately let CricketViewController present `SportsViewController" as a modal view controller, no animation.
As far as the user is concerned the sports view controller is the starting point.
Dismiss the modal view controller on the Done button tap, giving the user the illusion of going to the next view.
view1 = [[View1 alloc] initWithNibName:#"View1" bundle:nil]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
view1 = navigationController1;
[window addSubview:view1.view];
[window makeKeyAndVisible];
This is general idea so please change according to your problem.
Here is my code where I tried to use delegate pattern.
The problem is the sub view 1 (videoPlayer) is not able to call the delegate methods. :(
ViewSwitcher.h - Root Controller
#class VideoPlayer; //Sub View 1
#class LandingPage; //Sub View 2
#protocol viewSwitcherDelegate
-(void)notifyViewSwitcher;
#end
#interface ViewSwitcher : UIViewController
{
id <viewSwitcherDelegate> delegate;
}
#property (nonatomic, retain) VideoPlayer *videoPlayer;
#property (nonatomic, retain) LandingPage *landingPage;
#property(assign) id <viewSwitcherDelegate> delegate;
-(void)loadSecondView;
-(void)delegateSetInSubView;
#end
ViewSwitcher.m - Implementation
#synthesize videoPlayer;
#synthesize landingPage;
//#synthesize delegate;
// 1.4 -> Declare the delegate constructor
- (id <viewSwitcherDelegate>)delegate
{
return delegate;
}
// 1.5 -> Declare the setDelegate method
- (void)setDelegate:(id <viewSwitcherDelegate>)v
{
delegate = v;
}
- (void)viewDidLoad
{
VideoPlayer *videoController = [[VideoPlayer alloc] initWithNibName:#"VideoPlayer" bundle:nil];
self.videoPlayer = videoController;
[self.view insertSubview:videoController.view atIndex:0];
[videoController release];
[super viewDidLoad];
}
-(void)loadSecondView
{
NSLog(#"Call for loading 2nd View");
}
VideoPlayer.h - SubView 1 (Movie Player)
#interface VideoPlayer : UIViewController <viewSwitcherDelegate>
{
ViewSwitcher *viewSwitcher;
MPMoviePlayerController *videoController;
}
#end
VideoPlayer.m - implementation
-(void)notifyViewSwitcher
{
NSLog(#"notifyViewSwitcher called.");
//Attempted to call the loadSecondView of ViewSwitcher
I tried calling delegate's method but in log nothing is printed.
[viewSwitcher loadSecondView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
// Setting the delegate
viewSwitcher.delegate = self;
return self;
}
- (void)viewDidLoad
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"bumper.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoController = [[MPMoviePlayerController alloc] initWithContentURL:url];
videoController.controlStyle = MPMovieControlStyleNone;
[self.view addSubview:videoController.view];
videoController.view.frame = CGRectMake(0, 0, 480, 320);
videoController.fullscreen = YES;
// Remove the status bar from this view.
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:UIStatusBarAnimationFade];
// TODO: This observer needs to be removed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateChange:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoController];
// Play the video.
[videoController play];
[super viewDidLoad];
}
// Receives notification once movie is finished.
-(void)playbackStateChange:(NSNotification*)notification
{
// TODO: Switch the view.
NSLog(#"Notification = %#", notification);
[self notifyViewSwitcher];
}
Here is the LOG:-
2011-08-03 02:44:47.333 My Video Player[24016:207] Notification = NSConcreteNotification 0x5768280 {name = MPMoviePlayerPlaybackDidFinishNotification; object = <MPMoviePlayerController: 0x5740940>; userInfo = {
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 0;
}}
2011-08-03 02:44:47.337 My Video Player[24016:207] notifyViewSwitcher called.

How to load a UIWebView with a close button on top?

I currently have the following code that loads a UIWebView from another View. Now is there anyway I can have a close button?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:webView];
NSURLRequest *urlRequest;
NSURL *urlforWebView;
urlforWebView=[NSURL URLWithString:#"http://www.google.com"];
urlRequest=[NSURLRequest requestWithURL:urlforWebView];
[webView loadRequest:urlRequest];
}
I am going to load a page built using jquery mobile, so a close button inside the page would also work fine. But on a navigation bar would be ideal. Btw, my application does not have a UINavigationBar
I would create a new sub class of UIViewController, say WebViewController with a nib. Then I would add an UINavigationBar with a close button and an UIWebView. Then to show your web view controller you can do something like:
WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.loadURL = [NSURL URLWithString:#"http://www.google.com"];
[self presentModalViewController:webViewController animated:YES];
[webViewController release];
In your WebViewController you can define:
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#property (nonatomic, retain) NSURL *loadURL;
- (IBAction)close:(id)sender;
and implement something like this:
- (void)viewDidLoad {
[super viewDidLoad]
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.loadURL];
[self.webView loadRequest:urlRequest];
}
- (IBAction)close:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
Whenever you load the UIWebView, you could run a javascript snippet on the content first.
[webView stringByEvaluatingJavaScriptFromString:#"window.close = function() { window.location = 'app://close-webview'; };"];
That just hijacks the normal behavior of window.close() and gives you chance to catch the call in your Objective-C.
Then in your UIWebViewDelegate you could listen for whatever you chose as the close url.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if([request.URL.absoluteString isEqualToString:#"app://close-webview"]) {
webView.hidden = YES;
// or whatever you want to do to remove the UIWebView...
return NO;
}
return YES;
}
A bit hackish, but it would allow the web developer to control the look and feel of the close button from within the HTML content.