I need to get the url that a UIWebView is displaying after the user clicks on it once.
I've tried putting a button which calls a method which determines the url, under the UIWebView, but this way the button doesn't work. I've tried putting a button over a UIWebView but this way it gives not the url after the click, but the starting url.
Can you help me, please?
Thanks in advance!
The best way to know if somebody has clicked over your view is implementing touchesEnded method:
First declare a rect with the size of your webView in your viewDidLoad for example:
touchRect=CGRectMake(YourWebView.startPositionX, YourWebView.StartPositionY,YourWebView.width, YourWebView.height);
and then implement the touchesEnded:
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if(touch){
CGPoint location = [touch locationInView: [touch view]];
if (CGRectContainsPoint(touchRect, location)){
//do whatever
}
}
}
This way you will know if you have make a touch inside your webView.
To display the current location of UIWebView (location of main page):
NSURLRequest* webViewRequest = [myWebView request];
if (webViewRequest) {
NSURL* webViewURL = [webViewRequest URL];
NSString* webViewLocation = [webViewURL absoluteString];
}
Related
I've a little issue in my app. my app is based on core data using magical record.
In my first view ( a tableview) I have all the data, when one of the cell is tapped, it open the second view (detail UIview).
But i don't have enough space to show all the detail so i create a second detail view from the first one ( I don't want a scroll view).
the segue between the tableView and the firstDetailView work perfectly
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController*dvc = segue.destinationViewController;
dvc.indice = indexPath.row;
}
}
But when I go to the second detail UIview I always get the detail of the first record in the tableview. I think the problem is in the prepareForSegue method, but I can't figure out how to solve it,
Somebody could help me??
I created another property in the second detail view and set it equal to the property "indice" of the first detail view
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showMaking"]) {
DettaglioPreferito2*dvc = segue.destinationViewController;
dvc.indiceDue = self.indice;
}
}
I am working on an IOS app and have been stumped by this problem for about a week now and cannot find a solution. Any help you can offer would be greatly appreciated. Here's my set-up:
I have a tab bar controller.
I have a TableViewController which has a navigation bar with a navigation item "Add".
After you press the "Add" selector I am modally presenting another viewController that has a picker on it.
I am using Core Data.
When the second view controller is modally presented it comes up with a black screen with a navigation bar. If I access the second view controller from an unrelated screen modally it comes up fine without the navigation bar.
No error message is logged, not even that the object wasn't saved when you press the "save" on the navigation bar. However, pressing "save" will bring you back to the TableViewController and it looks like the entity was added.
Here is the code in my TableViewController:
- (void)add:(id)sender {
SecondViewController *addController = [[SecondViewController alloc] init];
addController.delegate = self;
Entity *newEntity = [NSEntityDescription insertNewObjectForEntityForName:#"Entity" inManagedObjectContext:self.managedObjectContext];
addController.entity = newEntity;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addController];
[self.navigationController presentModalViewController:navController animated:YES];
}
- (void)secondViewController:(SecondViewController *)secondViewController didAddEntity:(Entity *)entity {
if (entity) {
[self showEntity:entity animated:NO];
}
[self dismissModalViewControllerAnimated:YES];
}
- (void)showEntity:(Entity *)entity animated:(BOOL)animated {
EntityDetailTableViewController *detailViewController = [[EntityDetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.entity = entity;
[self.navigationController pushViewController:detailViewController animated:animated];
}
Here is the code in my second View Controller:
- (void) save {
entity.attribute = attributeTextField.text;
NSError *error = nil;
if (![entity.managedObjectContext save:&error])
{
NSLog(#"Problem saving attribute: %#", [error localizedDescription]);
}
NSLog(#"saveAttribute");
[self.delegate secondViewController:self didAddEntity:entity];
}
Any suggestions on where to go from here would be really helpful.
After much frustration I found the answer. If you are using storyboard you cannot navigate to the next view controller by the standard code. I put the prepareForSegue statement in my TableViewController.m file and then hooked up the connection on storyboard and identified the segue.
Now, when you press the Add button it segues to the new view controller screen and it's not black.
Here's a link to a useful tutorial
My guess is the way you are initializing is not right:
SecondViewController *addController = [[SecondViewController alloc] init];
should be initWithNIB:
I am currently using the new iOS 5 Storyboard approach to creating my Tabbed Application with Monotouch. I have developed two of my tab views in Xcode with Storyboard and linked them appropriately to the Tab Bar Controller. I also want to develop (in Xcode) a third tab view that would be shared among two additional tabs. I want to reuse the same layout, but display different data depending on which tab is selected (think something like a "Popular" and a "Recent" that would have the same layout but different data).
To do this, I figured I could add the tab manually twice after the Storyboard-driven tabs are added. How do I do this with the Storyboard approach? I'm not sure where in the code to do this since the loading of the Storyboard seems pretty transparent (i.e. no code in AppDelegate that I see). Or, is there another (easier/better) way to share a view between two tabs using the Storyboard approach?
I don't know Monotouch, but here's how I did it in Objective-c. I didn't find anything about this topic, so if something is wrong, people please comment :) By the way, I'm using ARC, so I don't manually manage memory! What I needed to achieve was like you, having a tab bar, loading the same viewController, but loading different data for each tab.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *root = (UITabBarController*)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
TeamViewController *home = [[mainStoryboard instantiateViewControllerWithIdentifier:#"Team"] initHome];
TeamViewController *visitor = [[mainStoryboard instantiateViewControllerWithIdentifier:#"Team"] initVisitor];
[root setViewControllers:[NSArray arrayWithObjects:home, visitor, nil] animated:NO];
UITabBar *tabs = root.tabBar;
UITabBarItem *homeTab = [tabs.items objectAtIndex:0];
UITabBarItem *visitorTab = [tabs.items objectAtIndex:1];
homeTab.title = #"Home team";
visitorTab.title = #"Visitor team";
return YES;
}
You can see I call initHome and initVisitor when I load my two TeamViewController, here is the code about it.
TeamViewController.h
#interface TeamViewController : UIViewController
{
enum
{
HOME,
VISITOR
};
int team;
}
TeamViewController.m
- (id)initHome
{
team = HOME;
return self;
}
- (id)initVisitor
{
team = VISITOR;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(team == HOME)
{
label.text = #"home data";
}
else if(team == VISITOR)
{
label.text = #"visitor data";
}
}
I don't know how well you can translate that to your project, but I hope you get the big picture of it :)
If you need to read a bit about how to access the first view controller using the storyboard: http://developer.apple.com/library/ios/#releasenotes/Miscellaneous/RN-AdoptingStoryboards/_index.html#//apple_ref/doc/uid/TP40011297
There is a section called "Accessing the First View Controller"
I have an app which adds a webview as a subview of the main UIViewController. I want to intercept the links clicked in this webview and open them in a new UIViewController (as modal view or navigationController).
For this new UIViewController, i am using https://github.com/samvermette/SVWebViewController which is an inline browser class and works pretty well.
I am able to intercept clicks from that webview just fine in webView:shouldStartLoadWithRequest:navigationType. But my problem is that it is not able to open the new UIViewController (SVWebViewController). Following is the code to intercept clicks in the webView and open it :
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if(inType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = [inRequest URL];
NSString *fname = [url absoluteString];
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithAddress:fname];
[self presentModalViewController:webViewController animated:YES];
[webViewController release];
return NO;
}
return YES;
}
There is no issue in SVWebViewController, it works fine when i call it from the main UIView but not when i use it to open clicks from the webView.
Thanks for your help!!
I followed this tutorial http://bytedissident.theconspiracy5.com/2010/11/24/uiwebview-tutorial/ and when I run the simulation in Xcode, all that is displayed is a blank white screen. I'm wondering what could be wrong. Is there some sort of code connection to the internet that i'm missing? I really don't know what could be wrong.
WebPageViewController.m
#import "WebPageViewController.h"
#implementation WebPageViewController
#synthesize wView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//a URL string
NSString *urlAddress = #"http://www.nd.edu";
//create URL object from string
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object created from your URL object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView
[wView loadRequest:requestObj];
//scale page to the device - can also be done in IB if preferred.
//wView.scalesPageToFit = YES;
}
Did you connect the .xib to the WebPageViewController class you created? You can check by selecting the .xib in the editor, and use identity inspector on 'File's Owner' in the Class field under Custom Class