I have a view controller that has a web view. What I'm wanting to know is if it is possible to keep the current page info available so that if the user goes elsewhere in my app and comes back to the web view, it will show the last page the user was on and not always load the url defined in viewDidLoad.
Save your current url in view will disappear to lets say NSUserDefaults like:
-(void)viewWillDisappear:(BOOL)animated{
NSString *currentURL = currentWebView.request.URL.absoluteString;
//save to NSUserDefaults
}
then in viewWillAppear get this url and load webview
Related
I'm working on an iOS app that uses a tab bar. An item is scanned using a barcode reader, and a callback method sets the tab bar item to the result view, then sends the request to the server. The callback from the server populates the display and the result view is shown.
However, under certain conditions which depend on the response from the server, I'd like show an alternate view which allows the user to manually enter the data, and then process the data in the same way as the callback. The manual entry display can't be shown on the tab bar.
So I create a modal view and exit the callback:
EnterTextController* enterTextController = [[EnterTextController alloc]init];
presentModalViewController:enterTextController animated:YES];
return;
In the view, I take the input and call the same process called by the callback:
NSLog(#"Button Clicked!");
NSString *myText = myInput.text;
[self dismissViewControllerAnimated:NO completion:nil];
[self.mainViewController processMyText: myText];
The problem is the processMyText doesn't get executed. I have a breakpoint set and it never hits it.
I'm sure I'm not going about this correctly. Any suggestions would be welcome.
Make sure the receiver isn't nil.
I have 2 views as shown in the following diagram. First View is a ViewController and the 2nd is a TableViewController.
After the user installs and runs the app for the first time he/she will see View number (1) (as shown in the image). Thereafter when the user opens the application for the 2nd time he/she is suppose to see the second (2) viewcontroller.
How am i to program this ?
NB: For example in Viber the user will first see the Enter the phone number view controller, and once the user successfully logins he'll directly see the all contacts view. I am looking to implement the same functionality. Can someone tell me how this is done ?
Solution i came across is when the user successffully logins, i set a NSUserDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:loginSuccess forKey:#"yes"];
[defaults synchronize];
Thereafter i check if this is set in the didFinishLaunchingWithOptions method. Is this the correct way to do this ?
Using NSUserDefaults is a great way to keep track of which view to show. To actually show the correct view you make the UITableViewController the root view controller for your app. Then in its viewDidLoad you check the value you stored in NSUserDefaults. If you need to display the one-time-only controller then you can simply use presentViewController:vc animated: NO. This will make it appear to the user that they started at the one-time-only controller, but for the rest of time you will have the "normal" flow work as expected.
Check for the second call by using NSUserDefaults. Upon start check for a value for any key. When that is null then it is the first time after installation, that the app is executed. After that store something with the very key.
You can do the check in your application delegate (in application didLodWithOptions) and then show either this or that.
Or you perform the check in viewDidLoad in your tableview controller. You can simply push to the other view (or display it modally). This is quite convenient when you want the user to return to the tableview as soon as his introduction is over. Then simply discard the other view ontop of the tableview.
Actually I have two related questions here, about different use cases of loading requests in a UIWebView.
Is it safe to call - [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy and its hidden property or the one of its superview is set to YES?
Is it safe to call - [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy?
In particular I'm interested whether it is considered to be a good practice to load request in a UIWebView that is not visible, and whether the delegate assigned to the instance of UIWebView will be notified once the request succeeds/fails. The reason I'm asking is that UIWebView class reference says "create a UIWebView object, attach it to a window, and send it a request to load web content", where the part telling that a UIWebView should be attached to a window makes me doubt if the above approaches are reliable.
I have successfully used [UIWebView loadRequest:] with objects that are not in the view hierarchy. I expect that the class reference just assumes that the view will be displayed as it's probably the most common use case.
Yes it is safe to call [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy.Because you are going to use web in the view.Also if you just give the URL in the program,it is enough to get.
The following code for [UIWebView loadRequest] is
NSString *strurl =#"http:www.google.com";
NSURL *url=[NSURL urlWithString:strurl];
NSURLRequest *urlrequest =[NSURLRequest requestWithUrl:url];
[webView loadRequest:urlrequest];
Even it is safe to call [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy.Because you can dynamically create the view and write the code for web view through the program.
It works. The approach is completely reliable as long as you are not using any private API and following HIG. It is not a bad practice as long it suits to your requirement. If there is a hidden property available to you for UIWebView then of-course you can hide the webView as per your requirements.
About your below query, it is written in the documentation as per the sentence context.
The reason I'm asking is that UIWebView class reference says "create a
UIWebView object, attach it to a window, and send it a request to load
web content", where the part telling that a UIWebView should be
attached to a window makes me doubt if the above approaches are
reliable.
The full context is below, which clearly means that to display a webpage in your application using UIWebView, you have to do it in the mentioned way.
You use the UIWebView class to embed web content in your application.
To do so, you simply create a UIWebView object, attach it to a window,
and send it a request to load web content.
I'm in need of some detail. I've looked through multiple books/forums, and I've tried as best as I can on my own. However, at this point I'm in need of some outside help with simple directions, if at all possible. I'm trying to make this as easy as possible to get the goal across, and if you need anything, just ask.
So, here is what I'm trying to do:
What I have:
I currently have the WebView view, and the bookmarks view with a table in it. I have NSUserDefaults passing the current URL to the bookmarks view and displaying it (but not saving it)
What the goal is:
I need to add an "Add Bookmarks" button underneath the rest of the table that gets the current string of the WebView in the other view and saves it as an entry to the table.
What I need:
So, at this point (as a start) I need a method of getting the URL of the WebView from the other view and transferring it to the bookmarks view to be saved as a row to the table.
I was thinking of doing this by calling absoluteString and saving to user defaults, but I need assistance for the code portion. I don't know the full usage of NSUserDefaults yet, but I do have some experience with it.
Thanks for the help! It's greatly appreciated!
Jake
You should pass the current URL and Title to the Bookmarks view before you open it:
[bookmarks setUrl:webView.request.URL.absoluteString];
[bookmarks setTitle:[webView stringByEvaluatingJavaScriptFromString:#"document.title"]];
... open bookmark view now ...
A full example would be:
- (IBAction)bookmarksPressed:(id)sender {
// Open the book marks controller passing it the current URL and Title
BookmarksViewController * bookmarksViewController = [[[BookmarksViewController alloc] initWithNibName:#"BookmarksViewController" bundle:nil] autorelease];
[bookmarksViewController setUrl:webView.request.URL.absoluteString];
[bookmarksViewController setTitle:[webView stringByEvaluatingJavaScriptFromString:#"document.title"]];
[self presentModalViewController: bookmarksViewController animated:YES];
}
I have table view navigation controller and the data is loaded from url. I created a thread in order to keep the user in table view in Second Level view controller. The problem is that the second level view controller loads url each time the view is opened, even if it has already been opened before. How can I make it load the data only once?
When you load the data within viewWillAppear: (or viewDidAppear:) then move the code to viewDidLoad.
You can also set a timestamp within your loading procedure.
self.lastUpdateDate = [NSDate date];
When the program enters the loading code, you can check after the last update date:
if([[NSDate date] timeIntervalSinceDate:self.lastUpdateDate] <= 300) {
//if the lastupdate was no longer then 5 minutes ago, don't update
}
else {
// do some web loading stuff
}