In my iOS app, I want the user to log in with facebook on page1, then get redirected to page2. On page2, there is a "Yes" and "No" button. Now, there are two scenarios:
1-If user pushes "No" he stays on page2. If he logs out from app and logs in back in future again, he again goes to page2 until he pushes "Yes" eventually.
2-User pushes "Yes" on page2. Now, he gets redirected to page3. From now on, every time user logs out and logs back in, he gets redirected to page3 directly without seeing page2. This is the part I am having trouble understanding how to implement in my code. Is there a way to initialize a global variable at start of my "ViewController.swift" code, but update its' value with a new value at the part of the code that takes care of actions if user pushes "Yes"? I mean is it possible to update the value of a variable from one thing to another at initialization?
Actually, maybe the shorter way to explain my question is: Could I initialize a variable in swift with some value at first, then, after user performs a specific action such a pushing a button, I change the initialization value permanently afterwards that action?
Also, please do let me know if you know any other ways to accomplish this.
Yes of course you can. I would say it's a user preference. Lucky, there is something called NSUserDefaults for that. It's a persistent storage so the value will stay here forever (until the app is uninstalled or rebuilt).
For example if user say yes on page 2 you write:
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "usedSaidYes")
otherwise:
if NSUserDefaults.standardUserDefaults().boolForKey("usedSaidYes") == true {
// Redirect page 3
} else {
// Redirect page 2
}
You should check if this value exist before, if not you go on page 1
Related
I have a JSON Object with an array of "employee" objects that indicate a login and logout time for each individual employee. I'm trying to write a function in Swift to show me the times when there are no employees logged in (i.e. the idle or gap time). Additionally this store has a set open/close time so I have the upper and lower bounds of when people could possibly be logged in.
I'm stuck on where to start so any help would be greatly appreciated. I've looked through a number of well known algorithms, but can't seem to find anything. Thank you so much for any help!
The general idea is pretty simple. I'll assume that you can identify a starting place where nobody is logged in, and that your input data is sorted by ascending time.
Build an empty dictionary, keyed by login name. Then start reading the log. When you get a login, add that login name to the dictionary. When you get a logout, remove that login name to the dictionary. Whenever you add a name to an empty dictionary, you've ended an idle period. Whenever removing a name causes the dictionary to be empty, you start an idle period.
Pseudo code is:
names = empty dictionary
while not end of log
if login-type is login
if names contains login-name
// error: already logged in
if names.count == 0
// end of an idle period
idle-end = login-time
names.add(login-name)
else if login-type is logout
if names does not contain login-name
// error: logout without login
names.remove(login-name)
if names.count == 0
// beginning of an idle period
idle-start = login-time
end while
You'll need some additional logic to empty the dictionary at the end of each open period, and allow for early logins (before the official open). But the basic idea above should give you a good start on the project.
I have an issue. I have a list of recipes in my recipes index page. The idea is that when you click on a particular recipe, it carries you to the show page and stores the recipe id in session. The presence/absence of the id in session is integral to the workflow of the program. The session gets cleared when you create a sub-recipe or revisit the index page. However, when you click back on the browser (to recipes/index) without creating a resource, the recipe id remains in session and you no longer desire the value in the session. This causes the program to run into errors. How do I do it that the clicking of the back button also deletes the session
Two possible solutions with the information you've provided:
Set a before_action on the index to clear the recipe id from the session.
Pass the recipe id to the show page via params then set it to an instance variable for use in the view.
I would choose the latter of these if possible. More info would allow a better answer.
So I solved it by checking if the request.referer is the index page's url. If it is, then I clear the session.
Moving forward, you can handle cases like this by checking the header key (request.headers["HTTP_REFERER"]) (also gotten by calling request.referer). This way regardless of the method you used to get to a page, you can always get its fidelity.
Sorry it abit long,hope you guys understand
I have a list of items in my tableView which it is JASidePanelController
Eg:
#menu = { 'menu' => ['Item1','Sign In'] }
This list will be in every of my cell.
When I perform Sign In action successfully, how do I automatically update the Sign In into Sign out?
There is
main_controller
menu_controller(JASidePanel)
when I clicked sign in, it will go to
sign_in_controller
When successfully, i just able to do
self.dismissViewControllerAnimated(true, completion: nil)
but the menu_controller is not updated. If I close the app and reopen,the update only will reflect.
I'm using App::Persistence['session'] to determine whether a user is logged in or not.
I research alot and maybe KVO is the solution?
Anyone had done this before and point me to the right direction?
Example app: Scribd
Call setNeedsLayout on the menu's view if you've managed to set it but it's just not updating, though I'm unsure of why your setup would require hackery like this. I'll update my answer if some source code is provided and I was wrong.
I have set up Facebook Connect on my site, and the login is working fine. I have set up a listener to detect a login state change, as follows:
FB.Event.subscribe('auth.sessionChange', function() {
window.location.reload();
});
All good. Except that leaving a browser window open causes the page to reload after perhaps 20mins. So the auth.sessionChange is firing at some interval. Which can caused havoc, particularly if the last page was a POST form submission.
In the FB docs it says "Sessions are refreshed over time as long as the user is active with your app." How often is this session refreshed?
And how can I protect the page from reloading if it doesn't need to? A conditional maybe within that function??
EDIT - adding information for clarification:
I thought I would add some info to give those offering the advice some more to go on (thanks so far BTW):
the reason I have the listener triggering a reload is because I wanted users to be logged in to the site every time they visit - if they already had a session in FB. I was detecting an active session with the JS SDK, which I know could do log in on its own, but I needed to trigger a page reload after the JS had done the detection, in order to set a PHP session for the site - required step. I couldn't do the PHP login without first letting the JS detect the FB session in the browser.
Usually you can see the length of the validity of the session just by looking at the session itself. However in many cases, I suggest requesting the offline_access permission to ensure the access_token remains valid as long as the user doesn't change their password and doesn't remove your application.
Andy, if you are only listening for login state then subscribe to auth.login and auth.logout instead of auth.sessionChange.
If you need an active session for a user to take an action on your site, use FB.getLoginStatus() or FB.getSession() to check the session object.
http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
http://developers.facebook.com/docs/reference/javascript/FB.getSession/
It really depends on your App work-flow and the below is just a general approach.
You can have a flag that presents in all your pages (maybe in your main layout if you are using a template system) with a setter function to be executed once you don't need a page reload to happen. Something like:
var do_reload = true; // By default, we always allow a reload
FB.Event.subscribe('auth.sessionChange', function() {
if( do_reload ) {
window.location.reload();
}
});
// Call this whenever you don't want 'auth.sessionChange' to reload the page
function turnoff_reload() {
do_reload = false;
}
// Call this to return to default behavior
function turnon_reload() {
do_reload = true;
}
Now if you are "redirecting" the user (e.g.: after a form post) to a page that doesn't require a reload then you need to set the flag in the first line to false.
Another scenario if you have an ajax loaded content based on the user interaction, if the page reloads by itself that content will be lost and the user needs to interact again to view that content, what can be done here is setting the flag to false after the Ajax call.
I'm developing a firefox extension. Inside my extension I use:
content.location.assign(url)
Depending on the users input content.location.assign(url) gets called again after a short time. It seems like my 2nd assign gets ignored. I'm looking for a way to abort the in process request to push trough the current one.
If I'm not wrong, try using either of this methods:-
reload(forceget)
Reload the document from the current URL. forceget is a boolean, which, when it is true, causes the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.
replace(url)
Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
You shouldn't use content.location to load a new page into the browser; instead use loadURI(url[, referrer[, postData[, allowThirdPartyFixup]]]) to load a page and BrowserStop() to cancel a load.