// // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { // window = new UIWindow (UIScreen.MainScreen.Bounds); Forms.Init(); FBSettings.DefaultAppID = FacebookAppId; FBSettings.DefaultDisplayName = DisplayName; window = new UIWindow(UIScreen.MainScreen.Bounds); UIViewController initialController; // Try to open up the cached fb access token var accountStore = AccountStore.Create(); var savedBuddyAccount = accountStore.FindAccountsForService(PopPicConstants.BuddyAccountKey).LastOrDefault(); if (savedBuddyAccount != null && FBSession.OpenActiveSession(false) && FBSession.ActiveSession.IsOpen) { var buddyToken = savedBuddyAccount.Properties [PopPicConstants.BuddyAccessTokenKey]; BuddyClient buddyClient = new BuddyClient(PopPicConstants.BuddyAppName, PopPicConstants.BuddyAppKey); FacebookClient fb = new FacebookClient(FBSession.ActiveSession.AccessTokenData.AccessToken); fb.AppId = PopPicConstants.AppId; fb.AppSecret = PopPicConstants.AppSecret; initialController = SplashScreenStoryboard.InstantiateViewController("SplashScreenViewController") as UIViewController; buddyClient.LoginAsync(buddyToken).ContinueWith(t => { if (!t.IsFaulted) { var authenticatedUser = t.Result; var repository = new GameRepository(authenticatedUser, buddyClient, fb); AppDelegate.Repository = repository; InvokeOnMainThread(() => { var newController = Storyboard.InstantiateViewController("MyGamesTabBarController") as UIViewController; newController.ModalTransitionStyle = UIModalTransitionStyle.FlipHorizontal; initialController.PresentViewController(newController, true, null); }); } else { // TODO: error } }); } else { initialController = Storyboard.InstantiateViewController("MyGamesTabBarController") as UIViewController; // initialController = GameplayStoryboard.InstantiateViewController("GameplayViewController") as GameplayViewController; } window.RootViewController = initialController; window.MakeKeyAndVisible(); return(true); }
private Task <GameRepository> GetRepositoryHelper(Activity context) { // First try to get the game repository from saved settings var fbSession = Session.OpenActiveSessionFromCache(this); var accountStore = AccountStore.Create(this); var savedBuddyAccount = accountStore.FindAccountsForService(PopPicConstants.BuddyAccountKey).LastOrDefault(); if (fbSession != null && savedBuddyAccount != null && fbSession.IsOpened) { // This is the case where we have a cached FacebookClient fb = new FacebookClient(fbSession.AccessToken); fb.AppId = PopPicConstants.AppId; fb.AppSecret = PopPicConstants.AppSecret; var buddyToken = savedBuddyAccount.Properties [PopPicConstants.BuddyAccessTokenKey]; BuddyClient buddyClient = new BuddyClient(PopPicConstants.BuddyAppName, PopPicConstants.BuddyAppKey); return(buddyClient.LoginAsync(buddyToken).ContinueWith(result => { if (!result.IsFaulted) { var authenticatedUser = result.Result; var repository = new GameRepository(authenticatedUser, buddyClient, fb); return repository; } else { throw result.Exception; } })); } else { this.newRepositoryEvent.Reset(); Intent startIntent = new Intent(context, typeof(HelloFacebookSampleActivity)); startIntent.AddFlags(ActivityFlags.NewTask); // startIntent.AddFlags (ActivityFlags.NoHistory); context.StartActivityForResult(startIntent, 1234); return(Task.Factory.StartNew(() => { this.newRepositoryEvent.WaitOne(); return this.repository; })); } }
private void AuthThroughBuddy(string fbUserId, string fbUserToken) { //Check to ensure everything's setup right GcmClient.CheckDevice(this); GcmClient.CheckManifest(this); GcmClient.Register(this, GcmBroadcastReceiver.SENDER_IDS); var registrationId = GcmClient.GetRegistrationId(this); FacebookClient fb = new FacebookClient(fbUserToken); fb.AppId = AppId; fb.AppSecret = AppSecret; BuddyClient client = new BuddyClient(BuddyAppName, BuddyAppKey); const string BuddyAccountKey = "BuddyAccount"; const string BuddyAccessTokenKey = "AccessToken"; const string FacebookIDTokenKey = "FacebookIdTokenKey"; var accountStore = AccountStore.Create(this); var savedAccount = accountStore.FindAccountsForService(BuddyAccountKey).LastOrDefault(); Task <AuthenticatedUser> getUserTask; bool saveAccount = false; if (savedAccount != null && savedAccount.Properties.ContainsKey(BuddyAccessTokenKey)) { saveAccount = false; var token = savedAccount.Properties [BuddyAccessTokenKey]; getUserTask = client.LoginAsync(token); } else { saveAccount = true; getUserTask = client.SocialLoginAsync("Facebook", fbUserId, fbUserToken).ContinueWith((u) => { if (u.IsFaulted) { // try again for kicks } return((AuthenticatedUser)u.Result); }); } getUserTask.ContinueWith(r => { Console.WriteLine("Get User Task has happened result is faulted = " + r.IsFaulted.ToString()); if (!r.IsFaulted) { AuthenticatedUser user = r.Result; var successActivity = new Action(() => { var repository = new POPpicLibrary.GameRepository(user, client, fb); Console.WriteLine("Success task is running!"); if (saveAccount) { var properties = new Dictionary <string, string>(); properties[BuddyAccessTokenKey] = user.Token; properties[FacebookIDTokenKey] = fbUserId; Account buddyAccount = new Account(user.ID.ToString(), properties); accountStore.Save(buddyAccount, BuddyAccountKey); } // Finish(); if (progressDialog != null) { progressDialog.Dismiss(); } ((POPpicApplication)Application).SetGameRepository(repository); }); if (string.IsNullOrEmpty(user.ApplicationTag)) { user.PhotoAlbums.CreateAsync(user.ID.ToString(), true, null).ContinueWith(pa => { if (!pa.IsFaulted) { var album = pa.Result; user.VirtualAlbums.CreateAsync(user.ID.ToString(), null).ContinueWith(va => { if (!va.IsFaulted) { var virtualAlbum = va.Result; var extraData = new UserExtraData(); extraData.UploadAlbumId = album.AlbumId; extraData.WinnerAblumVirtualId = virtualAlbum.ID; user.UpdateAsync(null, null, UserGender.Any, 0, null, UserStatus.Any, false, false, JsonConvert.SerializeObject(extraData)).ContinueWith(updateResult => { if (!updateResult.IsFaulted && updateResult.Result) { RunOnUiThread(successActivity); } }); } }); } }); } else { RunOnUiThread(successActivity); } } else { if (progressDialog != null) { progressDialog.Dismiss(); } RunOnUiThread(() => { AndroidUtilities.ShowAlert(this, "Error Getting User Account", r.Exception.Message, "Try Again", (object sender, DialogClickEventArgs e) => { AuthThroughBuddy(); }); }); Console.WriteLine(r.Exception.Message); } }); }