After
_facebook = [[Facebook alloc] init];
and
[_facebook authorize:kAppId permissions:_permissions delegate:self];
How can I tell whether the _facebook is (still) logged-in/valid to do further [_facebook requestWithMethodName: ...]?
Or should I just simply [_facebook authorize:...] again and again? Thanks!
I was having the same issue which i solved with the solution of another Member from here, dont know link exactly. but here's the solution
When you are logged into the facebook
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:#"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:#"ExpirationDate"];
In next View Controller, where you want to use the facebook with same session
_facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:#"AccessToken"];
_facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:#"ExpirationDate"];
if ([_facebook isSessionValid] == NO) {
//[_facebook authorize:kAppId permissions:self.permissions delegate:self]; //Create new facebook instance
}
Let me know if i put something which does not work for you.
Thanks
Related
I am trying to toggle user agent for a uiwebview between iPhone and iPad. So to have a button that will change from iPad user agent to iPhone user agent and backward.
I found this link: http://www.mphweb.com/en/blog/easily-set-user-agent-uiwebview
Unfortunately, the user agent can be changed only once, even if I recreate the uiwebview.
Does anyone has an idea about how to do it?
Btw, I also tried set the user agent in urlrequest header, without success.
Thanks
Me too, I have been able to set the UserAgent once only. Then I can't change it anymore. I tried on the simulator and on the device too. Same trouble.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *dictionary = #{#"UserAgent" : #"MyOWnUserAgent_2"};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
I don't use web views. I just make calls like:
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlRequest] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
I cleaned, deleted the app from the device, I rebuilt, I quit, reboot, rebuilt... The problem is still there. I run XCode 7.3 (7D175) on OS X 10.11.5 (15F34) and build against Deploymen Target iOS 9.1, Base SDK iOS 9.3.
Any clue?
try this
static void appendUA(NSString *uaStr) {
if (!uaStr)
return;
UIWebView *tempWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *secretAgent = [tempWebView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
secretAgent = [secretAgent stringByAppendingString:uaStr];
NSDictionary<NSString *, id> *domain = [[NSUserDefaults standardUserDefaults] volatileDomainForName:NSRegistrationDomain];
if (domain) {
NSMutableDictionary<NSString *, id> *newDomain = [domain mutableCopy];
[newDomain setObject:secretAgent forKey:#"UserAgent"];
domain = newDomain;
} else {
domain = [[NSDictionary alloc]
initWithObjectsAndKeys:secretAgent, #"UserAgent", nil];
}
[[NSUserDefaults standardUserDefaults] removeVolatileDomainForName:NSRegistrationDomain];
[[NSUserDefaults standardUserDefaults] setVolatileDomain:domain forName:NSRegistrationDomain];
[[NSUserDefaults standardUserDefaults] synchronize];
}
You should call this function BEFORE create WKWebview/UIWebView, and space is NOT automatically added.
appendUA(#" ====TEST_UA====");
WKWebView *wkWebView = [WKWebView new];
//...
I want to keep password which is given by user in a variable. Where to get password value. I look in code but it is containing only username. I am using MGTwitterengine api in my application. Here is the function which is printing data on console.
-(void)setUsername:(NSString *)newUsername password:(NSString *)newPassword
{
NSLog(#"uset sername %# and password %#",newUsername,newPassword);
...
}
I debug in all function but I saw this function is running after loggedin. But this is printing null in password value although username is printing well. Actually I I have to trace all tweet value of user. I went through MGTwitterEngine. There is a block of code but it is necessary to write username and password.
Here is the code
MGTwitterEngine *twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
[twitterEngine setUsername:#"username" password:#"password"];
// Get updates from people the authenticated user follows.
NSString *connectionID = [twitterEngine getFollowedTimelineFor:nil since:nil startingAtPage:0];
Please help me how to keep password ?
Read the developer documentation, it provides all the info you need for something as easy as this. Back to the question, there're a couple of ways how you can save this data.
NSUserDefaults: You can save the username and password using this class like this:
[[NSUserDefaults sharedUserDefaults] setObject:username forKey:#"username"];
[[NSUserDefaults sharedUserDefaults] setObject:username forKey:#"password"];
[[NSUserDefaults sharedUserDefaults] synchronize];
Once you need this data again, call it:
NSString*username = [[NSUserDefaults standardUserDefaults] objectForKey:#"username"];
NSString*password = [[NSUserDefaults standardUserDefaults] objectForKey:#"password"];
OR:
NSDictionary: If you don't want to rely on NSUserDefaults, you can save the login data in a NSDictionary and save it to your sandbox. This works like this:
NSMutableDictionary*loginDictionary = [[NSMutableDictionary alloc] init];
[loginDictionary setObject:username forKey:#"username"];
[loginDictionary setObject:password forKey:#"password"];
[loginDictionary writeToFile://Your Path Here atomically:NO];
[loginDictionary release];
Then, once you need the data again, read from file:
NSMutableDictionary*loginDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile://Your Path];
NSString*username = [loginDictionary objectForKey:#"username"];
NSString*password = [loginDictionary objectForKey:#"password"];
[loginDictionary release];
Hope it helps!
I have a program that authorize on facebook with cookies. So the modal dialog with facebook authorizing every time displaying. I want to hide it from user. Any help please..
When your login was successful you could do something like:
[[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:#"FBAccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:#"FBExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
then when your application starts up, do something similar to this:
if ( [[NSUserDefaults standardUserDefaults] objectForKey:#"FBAccessToken"] != nil ) {
_facebook.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:#"FBAccessToken"];
_facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"FBExpirationDate"];
}
What this is doing is, that it saves the "cookie" of the facebook authentication and reuses it when the application is restarted.
Hope this helps.
I have the following function to post to Facebook using the latest iOS Facebook SDK.
-(void)fbPost:(NSMutableDictionary *) params{
NSLog(#"fbPost called");
if (![facebook isSessionValid]) {
NSLog(#"session invalid, calling fblogin");
[self fblogin];
}
if ([facebook isSessionValid]) {
NSLog(#"session valid, calling publishToFB");
[self.facebook dialog:#"stream.publish" andParams:params andDelegate:self];
}
}
It works fine when there is no existing session: it logs in to facebook, gets permissions, returns to the app, shows the dialog and publishes the status. However, when trying a second time, isSessionValid returns true the first time and nothing happens, although the log shows publishToFB is called.
The session is persisted in fbDidLogin:
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:#"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:#"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
and loaded in application didFinishLaunchingWithOptions:
facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:#"AccessToken"];
facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:#"ExpirationDate"];
I made sure to ask for offline_access permission when logging in:
_permissions = [[NSArray arrayWithObjects:
#"publish_stream",#"offline_access",nil] retain];
It appears the problem was in drawing the dialog in this code in FBDialog.m
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
If I comment out the If clause, it works OK. I guess that in my app setup, "keyWindow" is not front-most, so the dialog was not showing up.
I have this in my app delegate applicationDidFinishLaunching method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"AcceptTC"] == nil){
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"NO" forKey:#"AcceptTC"];
[defaults registerDefaults:appDefaults];
}
and I have this in my RootViewController viewDidLoad method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:#"AcceptTC"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notice" message:#"By using this application you agree to be bound by the Terms and Conditions as stated within this application." delegate:self cancelButtonTitle:#"No Deal" otherButtonTitles:#"I Understand",nil];
[alert show];
[alert release];
}
and my alert view delegate does this:
if(buttonIndex == 0){
exit(0);
}
else{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"YES" forKey:#"AcceptTC"];
}
However when I click "I understand" (button index 1) and then restart the application I still see the alert view! Even though I've definiely set the value to YES.
I have no idea how to change this. :( I only want it to show the first time a user starts the application - i don't want to keep showing it every time they want to use it.
Thanks
In my application I'm using NSUserDefaults with a bool, works fine.
When the first ViewController loads, it will do:
BOOL terms = [[NSUserDefaults standardUserDefaults] boolForKey:#"termsaccepted"];
if (!terms) {
[self presentModalViewController:disclaimerViewController animated:YES];
}
Within the disclaimer view, after the button has been tapped:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"termsaccepted"];
[[NSUserDefaults standardUserDefaults] synchronize];
I think you're missing the "synchronize" part. However I find using a bool more streamlined, too.
Maybe you need to call synchronize on the defaults to save the changes to disk?
Concering registerDefaults:
The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.
// Load default defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary \
dictionaryWithContentsOfFile:[[NSBundle mainBundle] \
pathForResource:#"Defaults" ofType:#"plist"]]];
Code taken from this SO answer.
Another blog article about NSDefaults:
http://retrodreamer.com/blog/2010/07/slight-change-of-plan/