How do I respond to successful login with Dropbox API on iOS? - iphone

It checks whether the user is logged into dropbox when my app is launched. If they are, it continues with code relating to dropbox. If not, it shows them a login screen using [[DBSession sharedSession] link];.
From the login screen comes this delegate, if authorization fails:
-(void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId {
[[DBSession sharedSession] link];
}
But there doesn't seem to be something for when authorization succeeds. How do I deal with this scenario? I need to start running the necessary code once they're linked with dropbox.

You handle successful login from the Dropbox API in the
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url function
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if ([[DBSession sharedSession] handleOpenURL:url]) {
//Successfully Logged in to Dropbox
return YES;
}
return NO;
}

As part of applicationDidFinishLaunching you could initiate the dropbox api you could do the following.
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DBSession* dbSession = [[[DBSession alloc] initWithAppKey:#"appKey" appSecret:#"appSecret" root:kDBRootAppFolder] autorelease];
dbSession.delegate = self;
[DBSession setSharedSession:dbSession];
[[NSNotificationCenter defaultCenter] postNotificationName:kSharedSessionAvailability object:[NSNumber numberWithBool:dbSession != nil ? YES : NO]];
});
But normally you simply get your session and use it later via the restClient.

Related

Parse.com Facebook Login stays on "Loading" iOS

I'm using Parse, with the LogInViewController. I added Facebook on the logIn View.
When someone wants to connect with Facebook, it opens the browser, the user accepts the application and then he returns to the app. But when he returns on the app, he stays on "loading" and nothing happens.
However if the user restart the app, it works.
Only when it's the first connexion for the user I have this problem.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (![PFUser currentUser]) {
// Customize the Log In View Controller
PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
[logInViewController setDelegate:self];
[logInViewController setFacebookPermissions:[NSArray arrayWithObjects:#"friends_about_me", nil]];
[logInViewController setFields: PFLogInFieldsFacebook | PFLogInFieldsDismissButton];
// Present Log In View Controller
[self presentViewController:logInViewController animated:YES completion:NULL];
}
}
I think the problem isn't in my code because I use the default code of Parse but I'm not sure.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
withSession:[PFFacebookUtils session]];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
[FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]];
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
[[PFFacebookUtils session] close];
}

Facebook integration using parse framework

I done Facebook integration using parse framework with my application so how can i add logout button as well as permission to the user below mentioned image is my application screen-short as well how can i obtain okay button pressed event in my code..?
Login
In your app delegate, these lines should be present:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Parse setApplicationId:#"YOUR_APPLICATION_ID"
clientKey:#"YOUR_CLIENT_KEY"];
[PFFacebookUtils initializeWithApplicationId:#"YOUR_FB_APP_ID"];
// Override point for customization after application launch.
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [PFFacebookUtils handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [PFFacebookUtils handleOpenURL:url];
}
This will ensure that parse is initialized and connected with your facebook app on startup. The two methods at the bottom are what enables your app to launch the screen to sign in to facebook and request permissions. In order to actually present that screen to the user, all that's necessary is a button that calls a method similar to this one:
-(IBAction)facebookLoginButtonPressed:(id)sender {
[self loginWithFacebook];
}
-(void)loginWithFacebook {
NSArray *permissionsArray = #[#"publish_actions", #"email", #"user_location"];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
if (!user) {
if (!error) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
} else {
NSLog(#"Uh oh. An error occurred: %#", error);
}
} else {
[self performSegueWithIdentifier:#"loginToFeed" sender:self];
}
}];
}
You shouldn't need an event callback for the ok, when the [PFFacebookUtils logInWithPermissions:block:] comes back, it will perform the block that you provide, enabling you to segue to different ViewControllers or show different features.
Log Out
Add a button to whatever view you want to have control logging out. Then add an IBAction method for that button:
-(IBAction)logOutButtonPressed:(id)sender {
[PFUser logOut];
NSLog(#"User logged out!");
[self dismissViewControllerAnimated:YES completion:^{}];
}

How to redirect on app after successfull facebook login in iOS5

There is a facebook button in my app in login screen ,when user click on this button facebook login page open in browse and after successful login application redirect and display next page . Please hep mw with example .
IOS version : 5.0
ARC project
Thank You
by the way you guys can visit my reply here Native iOS Facebook SSO won't return to app
In my application i had to implement facebook and googleplus both and finally I made it successfully. I dont think facebook developer app portal needs any edit rather than getting appid. I made three apps with facebook login, one of them does not have iOS Native App setting in facebook developer app but that app's login part is also running well.
in your you AppDelegate modify the method
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSString *urlString = [url absoluteString];
if ([urlString hasPrefix:#"fb://xxxxxxxxxxxx"]) {
[FBSession.activeSession handleOpenURL:url];
returnValue = YES;
}
return returnValue;
}
xxx will be your facebookappid
But keep in mind that this is not triggered in IOS 6.In ios 6 the following method will be triggered.
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBSession.activeSession handleOpenURL:url];
}
If the state of your session changes due to login or disconnect FBsession calls the following method and you should handle your cases.
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState)state
error:(NSError *)error {
switch (state) {
case FBSessionStateOpen: {
//update permissionsArrat
[self retrieveUSerPermissions];
if (!needstoReopenOldSession) {
//First User information
[self getUserInformation:nil];
}
NSNotification *authorizationNotification = [NSNotification notificationWithName:facebookAuthorizationNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotification:authorizationNotification];
}
case FBSessionStateClosed: {
break;
}
case FBSessionStateClosedLoginFailed: {
[FBSession.activeSession closeAndClearTokenInformation];
break;
}
default:
break;
}
if (error) {
NSNotification *authorizationNotification = [NSNotification notificationWithName:faceBookErrorOccuredNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotification:authorizationNotification];
}
}
Use this official tutorial and you will be OK. See the 2-d point for your problem as well.

Can UILocalNotification fire a custom method when App is in background mode?

Well the title is self explained. I want to create an App that can manage programmed local notifications when App is in background mode. Notifications works smoothly but I want to fire a custom method when the alert is fired.
Is it possible?
Thank you.
Yes it can be done. You can do somethng like this:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:17.0 target:self selector:#selector(makeNotificationRequest:) userInfo:nil repeats:YES];
}
-(void)makeNotificationRequest:(NSTimer *)timer
{
CLLocation *location = [[AppHelper appDelegate] mLatestLocation];
NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
#ifdef _DEBUG
[paramDic setValue:[NSString stringWithFormat:#"77.586"] forKey:#"Lat"];
[paramDic setValue:[NSString stringWithFormat:#"12.994"] forKey:#"long"];
#else
[paramDic setValue:[NSString stringWithFormat:#"%f",location.coordinate.latitude] forKey:#"Lat"];
[paramDic setValue:[NSString stringWithFormat:#"%f",location.coordinate.longitude] forKey:#"long"];
#endif
WNetwork *mNetwork = [[WNetwork alloc] init];
[mNetwork makeRequsetWithURL:URL_Showbeeps type:JBJsonParser paramDictionary:paramDic delegate:self];
[mNetwork autorelease];
NSLog(#"URL HIT%#",paramDic);
[paramDic autorelease];
}
And to customize your action on alert you can use this:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
;
}
}
If the app were in the foreground then you would want the - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification method of UIApplicationDelegate, however the documentation suggests that this will not be called when the app is in the background.
You can get a list of local notifications by calling scheduledLocalNotifications on your UIApplication instance - you can then poll these for their times and schedule a function to call at that time in your background mode. This won't necessarily 100% match up with when the Local Notification fires though, but I think it's as close as the App Store guidelines will let you get.
You can also present your own Local Notifications when you are in background mode by calling the presentLocalNotificationNow: method:
https://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/presentLocalNotificationNow:
so you could work around this by just presenting your own notifications and not scheduling them for the OS to deliver.
If you are trying to access Local Notifications from other applications then I don't think this is allowed.
No ..no custom methods are defined while app is running in background..Once an notification is fired we can;t change the alert message also. But nice when we show the alert message then by clicking the yes button to the alert take you to the one method called app is Running in Background which is in AppDelegate.m file

iphone: Set badge on a tabbarItem when receiving a PUSH message, when the app is inactive

I have a application that uses PUSH. But I have one problem when the application is inactive/in the background.
When the PUSH messages come and the user clicks on Close, the badge is set on the application-icon.
But I also want to set a badge on a tabBarItem.
I have this code that saves the PUSH
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateInactive) {
//Save the PUSH until the app is active.
newPush = [userInfo copy];
}
}
And in:
- (void)applicationDidBecomeActive:(UIApplication *)application
I have the following code:
//Check if there is new PUSH messages.
if (newPush!=nil) {
//There is a new PUSH!
NSInteger badge = [[[newPush objectForKey:#"aps"] objectForKey:#"badge"] intValue];
if (badge > 0) {
//Set badge-numbers to 'badge'
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
[[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:[NSString stringWithFormat:#"%d",badge]];
}
else {
//Set badge-numbers to zero
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:nil];
}
}
My code for handling the PUSH when the application is active works fine and the badges are set both on the application-icon and on the tabBarItem.
Someone know what's wrong?
Thanks in advance!
If the application is inactive, didReceiveRemoteNotification is not executed. The only way the notification data can reach your app in this case is if the user taps on the notification to open the app. Then, when the app is launched, you can get the notification data in application:didFinishLaunchingWithOptions: by using this code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[self handleRemoteNotification:application userInfo:remoteNotif];
return YES;
}
return YES;
}