Advertisement
Guest User

TwitterBinding

a guest
May 5th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using Prime31;
  6.  
  7.  
  8.  
  9. #if UNITY_IPHONE
  10.  
  11. namespace Prime31
  12. {
  13.     public class TwitterBinding
  14.     {
  15.         static TwitterBinding()
  16.         {
  17.             TwitterManager.noop();
  18.         }
  19.    
  20.    
  21.         [DllImport("__Internal")]
  22.         private static extern void _twitterInit( string consumerKey, string consumerSecret );
  23.    
  24.         // Initializes the Twitter plugin and sets up the required oAuth information
  25.         public static void init( string consumerKey, string consumerSecret )
  26.         {
  27.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  28.                 _twitterInit( consumerKey, consumerSecret );
  29.         }
  30.    
  31.    
  32.         [DllImport("__Internal")]
  33.         private static extern bool _twitterIsLoggedIn();
  34.    
  35.         // Checks to see if there is a currently logged in user
  36.         public static bool isLoggedIn()
  37.         {
  38.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  39.                 return _twitterIsLoggedIn();
  40.             return false;
  41.         }
  42.    
  43.    
  44.         [DllImport("__Internal")]
  45.         private static extern string _twitterLoggedInUsername();
  46.    
  47.         // Retuns the currently logged in user's username
  48.         public static string loggedInUsername()
  49.         {
  50.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  51.                 return _twitterLoggedInUsername();
  52.             return string.Empty;
  53.         }
  54.    
  55.    
  56.         [DllImport("__Internal")]
  57.         private static extern void _twitterShowOauthLoginDialog();
  58.    
  59.         // Shows the login dialog via an in-app browser
  60.         public static void showLoginDialog()
  61.         {
  62.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  63.                 _twitterShowOauthLoginDialog();
  64.         }
  65.    
  66.    
  67.         [DllImport("__Internal")]
  68.         private static extern void _twitterLogout();
  69.    
  70.         // Logs out the current user
  71.         public static void logout()
  72.         {
  73.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  74.                 _twitterLogout();
  75.         }
  76.    
  77.    
  78.         // Posts the status text. Be sure status text is less than 140 characters!
  79.         public static void postStatusUpdate( string status )
  80.         {
  81.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  82.             {
  83.                 var dict = new Dictionary<string,string>();
  84.                 dict.Add( "status", status );
  85.                 TwitterBinding.performRequest( "POST", "1.1/statuses/update.json", dict );
  86.             }
  87.         }
  88.    
  89.    
  90.         [DllImport("__Internal")]
  91.         private static extern void _twitterPostStatusUpdateWithImage( string status, string imagePath );
  92.    
  93.         // Posts the status text and an image. Note that the url will be appended onto the tweet so you don't have the full 140 characters
  94.         public static void postStatusUpdate( string status, string pathToImage )
  95.         {
  96.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  97.                 _twitterPostStatusUpdateWithImage( status, pathToImage );
  98.         }
  99.    
  100.    
  101.         [DllImport("__Internal")]
  102.         private static extern void _twitterPostStatusUpdateWithRawImage( string status, byte[] data, int length );
  103.    
  104.         // Posts the status text and an image. Note that the url will be appended onto the tweet so you don't have the full 140 characters
  105.         public static void postStatusUpdate( string status, byte[] imageData )
  106.         {
  107.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  108.                 _twitterPostStatusUpdateWithRawImage( status, imageData, imageData.Length );
  109.         }
  110.    
  111.    
  112.         // Receives tweets from the users home timeline
  113.         public static void getHomeTimeline()
  114.         {
  115.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  116.                 TwitterBinding.performRequest( "GET", "1.1/statuses/home_timeline.json", null );
  117.         }
  118.    
  119.    
  120.         [DllImport("__Internal")]
  121.         private static extern void _twitterPerformRequest( string methodType, string path, string parameters );
  122.    
  123.         // Performs a request for any available Twitter API methods.  methodType must be either "get" or "post".  path is the
  124.         // url fragment from the API docs (excluding https://api.twitter.com) and parameters is a dictionary of key/value pairs
  125.         // for the given method.  Path must request .json!  See Twitter's API docs for all available methods.
  126.         public static void performRequest( string methodType, string path, Dictionary<string,string> parameters )
  127.         {
  128.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  129.                 _twitterPerformRequest( methodType, path, parameters != null ? parameters.toJson() : null );
  130.         }
  131.    
  132.    
  133.         #region iOS 5 Tweet Sheet methods
  134.    
  135.         [DllImport("__Internal")]
  136.         private static extern bool _twitterIsTweetSheetSupported();
  137.    
  138.         // Checks to see if the current iOS version supports the tweet sheet
  139.         public static bool isTweetSheetSupported()
  140.         {
  141.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  142.                 return _twitterIsTweetSheetSupported();
  143.             return false;
  144.         }
  145.    
  146.    
  147.         [DllImport("__Internal")]
  148.         private static extern bool _twitterCanUserTweet();
  149.    
  150.         // Checks to see if a user can tweet (are they logged in with a Twitter account) using the native UI via the showTweetComposer method
  151.         public static bool canUserTweet()
  152.         {
  153.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  154.                 return _twitterCanUserTweet();
  155.             return false;
  156.         }
  157.    
  158.    
  159.         [DllImport("__Internal")]
  160.         private static extern void _twitterShowTweetComposer( string status, string imagePath, string url );
  161.    
  162.         public static void showTweetComposer( string status )
  163.         {
  164.             showTweetComposer( status, null, null );
  165.         }
  166.    
  167.         public static void showTweetComposer( string status, string pathToImage )
  168.         {
  169.             showTweetComposer( status, pathToImage, null );
  170.         }
  171.    
  172.         // Shows the tweet composer with the status message and optional image and link
  173.         public static void showTweetComposer( string status, string pathToImage, string link )
  174.         {
  175.             if( Application.platform == RuntimePlatform.IPhonePlayer )
  176.                 _twitterShowTweetComposer( status, pathToImage, link );
  177.         }
  178.    
  179.         #endregion
  180.    
  181.     }
  182.  
  183. }
  184. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement