public void ViewWillAppear(bool animated) { if(string.IsNullOrEmpty(Settings.FbAuth) || Settings.FbAuthExpire <= DateTime.Now) { fvc = new FacebookAuthorizationViewController("158978690838499", new string[] {"publish_stream"}, FbDisplayType.Touch); fvc.AccessToken += delegate(string accessToken, DateTime expires) { Settings.FbAuth = accessToken; Settings.FbAuthExpire = expires; DataAccess.GetFriends(); //BackgroundUpdater.AddToFacebook(); //this.DismissModalViewControllerAnimated(true); }; fvc.AuthorizationFailed += delegate(string message) { //this.DismissModalViewControllerAnimated(true); }; fvc.Canceled += delegate { Console.WriteLine("Canceled clicked"); //this.DismissModalViewControllerAnimated(true); //this.NavigationController.PopViewControllerAnimated(false); }; //this.NavigationController.PresentModalViewController(fvc, false); } else { DataAccess.GetFriends(); loadingView.Show(true); //Util.PushNetworkActive(); //BackgroundUpdater.GetFacebookFriends(foundFriends => {foundFacebookFriends(foundFriends);}); } }
public static void DownloadFaces() { if ((string.IsNullOrEmpty (Settings.FbAuth) || Settings.FbAuthExpire <= DateTime.Now)) { var fvc = new FacebookAuthorizationViewController ("158978690838499", new string[] { "publish_stream" }, FbDisplayType.Touch); fvc.AccessToken += delegate(string accessToken, DateTime expires) { Settings.FbAuth = accessToken; Settings.FbAuthExpire = expires; fvc.View.RemoveFromSuperview (); TouchPanel.Reset (); DataAccess.GetFriends (false); //BackgroundUpdater.AddToFacebook(); //this.DismissModalViewControllerAnimated(true); }; //this.DismissModalViewControllerAnimated(true); fvc.AuthorizationFailed += delegate(string message) { fvc.View.RemoveFromSuperview (); }; fvc.Canceled += delegate { Console.WriteLine ("Canceled clicked"); fvc.View.RemoveFromSuperview (); TouchPanel.Reset (); //this.DismissModalViewControllerAnimated(true); //this.NavigationController.PopViewControllerAnimated(false); }; fvc.View.Frame = new System.Drawing.RectangleF (System.Drawing.PointF.Empty, new System.Drawing.SizeF (fvc.View.Frame.Height, fvc.View.Frame.Width)); Util.MainGame.Window.AddSubview(fvc.View); } else { DataAccess.GetFriends (false); } }
/// <summary> /// Starts the login process. /// </summary> public void Login(UIViewController parent, bool animate) { switch (Config.Network) { case SocialNetwork.Facebook: { var config = (FacebookConfig)Config; var authorizor = new FacebookAuthorizationViewController(config.AppID, #if true // fel config.Permissions, FbDisplayType.Popup); #else config.Permissions, FbDisplayType.Touch); #endif authorizor.AccessToken += delegate(string accessToken, DateTime expires) { try { var wc = new System.Net.WebClient(); var result = wc.DownloadData("https://graph.facebook.com/me?access_token="+accessToken); var stream = new MemoryStream(result); var jsonArray = (System.Json.JsonObject)System.Json.JsonObject.Load(stream); string username = jsonArray["name"].ToString().Replace("\"", ""); this.UserId = jsonArray["id"].ToString().Replace("\"", ""); this.AccessToken = accessToken; this.Username = username; #if true // fel this.ExpirationDate = expires; #endif parent.BeginInvokeOnMainThread(delegate{ parent.DismissModalViewControllerAnimated(animate); OnSuccess(); }); } catch (Exception ex) { OnFailure(); } }; authorizor.Canceled += delegate { OnFailure(); }; authorizor.AuthorizationFailed += delegate { OnFailure(); }; parent.BeginInvokeOnMainThread(delegate{ parent.PresentModalViewController(authorizor,animate); }); } break; case SocialNetwork.Twitter: { var config = (TwitterConfig)Config; var authorizor = new TweetStation.OAuthAuthorizer(new TweetStation.OAuthConfig(){ ConsumerKey = config.ConsumerKey, ConsumerSecret = config.ConsumerSecret, Callback = config.Callback, RequestTokenUrl = "https://api.twitter.com/oauth/request_token", AccessTokenUrl = "https://api.twitter.com/oauth/access_token", AuthorizeUrl = "https://api.twitter.com/oauth/authorize" }); parent.BeginInvokeOnMainThread(delegate { authorizor.AcquireRequestToken(); authorizor.AuthorizeUser(parent,delegate(){ if (authorizor.AccessScreenname != "") { this.Username = authorizor.AccessScreenname; this.AccessToken = authorizor.AccessToken; this.AccessTokenSecret = authorizor.AccessTokenSecret; OnSuccess(); } else { OnFailure(); } }); }); } break; } }
// This method is invoked when the application has loaded its UI and its ready to run public override bool FinishedLaunching(UIApplication app, NSDictionary options) { window = new UIWindow (UIScreen.MainScreen.Bounds); window.BackgroundColor = UIColor.White; table = new UITableViewController(); NavController = new UINavigationController(); // Add the [+] button statusButton = new UIBarButtonItem (UIBarButtonSystemItem.Add); statusButton.Clicked += delegate { usvc = new UpdateStatusViewController(); NavController.PushViewController(usvc, true); }; table.NavigationItem.SetRightBarButtonItem (statusButton, false); FacebookAuthorizationViewController fac = new FacebookAuthorizationViewController( clientId , new string[] {"read_stream", "publish_stream"} //, "user_groups"} , FbDisplayType.Touch); fac.AccessToken += delegate(string accessToken, DateTime expires) { token = accessToken; //Logged in, got your accessToken here and when it expires! NavController.PopViewControllerAnimated(true); //Do something else here (eg: Save the accessToken and expiry date to be used in your Graph API calls) Console.WriteLine("### Get json"); System.Net.WebClient wc = new System.Net.WebClient(); var b = wc.DownloadData( new Uri("https://graph.facebook.com/me/home?access_token=" + token) ); var s = Encoding.UTF8.GetString(b); //Console.WriteLine("### Output json"); Console.WriteLine(s); // http://www.brettnagy.com/post/2009/11/21/Using-JsonNET-with-MonoTouch.aspx var posts = JsonConvert.DeserializeObject<Posts>( s ); // foreach(var p in posts.data) // { // Console.WriteLine("name: " + p.from.name); // } table.Title = "Facebook"; table.TableView.Source = new TableViewSource(posts); }; NavController.PushViewController(table, false); NavController.PushViewController(fac, true); NavController.NavigationBar.TintColor = new UIColor(0.27f,0.52f,0.73f,1f); window.AddSubview(NavController.View); window.MakeKeyAndVisible (); return true; }