首页 > Unity > 如何通过Unity iOS应用程序在Twitter上分享图片和链接
2018
02-14

如何通过Unity iOS应用程序在Twitter上分享图片和链接

游戏引擎和游戏开发方面,Unity这一技术看起来是不可多得的,因为它不仅是最受欢迎和强大的游戏引擎,它具有用于开发iOS和游戏的优秀视觉编辑器,同时也具有很多功能,功能和社区的问题处理以及良好的定价选项。 毫无疑问,Unity被认为是最好的构建iOS和Android游戏和应用程序的引擎。
今天在本教程中,我正在分享一个与iOS应用程序相关的主题,帮助您轻松地从应用程序分享图片或链接到twitter。 而分享图像不是一项重大任务,而是包含iOS,用户必须首先上传图像或先在设备路径上写入图像,然后从该路径读取图像,因为iOS中不存在资源文件夹。 所以借助下面的代码,您可以轻松地将图像读取和写入设备路径。
让我们看看如何实现:
首先在Twitter上创建一个应用程序,并保存应用程序的消费者密钥,秘密密钥,媒体URL和Tweet URL。
那么为了在Twitter上分享,用户必须在Twitter上登录。 所以让我们使用下面提到的代码登录用户。
现在,我们创建三个UI按钮。
1.Twitter登录按钮//点击TwitterLogin
2.提交Pin Button //点击SubmitPin()
3.Twitter分享按钮//在点击中调用Twitter()。
并创建一个InputField来输入您在调用TwittterLogin()函数后收到的PIN码。
 
1. InputFieldPin
启动Twitter登录按钮。 然后成功禁用登录按钮并启用输入框并提交按钮。 然后在成功登录后,启用“共享”按钮并禁用所有其他按钮
现在创建两个脚本:
TwitterHandller和
Twitter
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;
using SimpleJSON;
using System.Runtime.InteropServices;
public class TwitterHandller: MonoBehaviour
{
     Twitter.RequestTokenResponse m_RequestTokenResponse;
 
    Twitter.AccessTokenResponse m_AccessTokenResponse;
    public string CONSUMER_KEY;//Put oun consumer key here
 
     public string CONSUMER_SECRET;//Put own consumer secret here
    // Use this for initialization
 
    void Start()
    {
            LoadUserTwitterInfo();
     }
  void LoadUserTwitterInfo()
    {
           m_AccessTokenResponse = new Twitter.AccessTokenResponse();                    
           m_AccessTokenResponse.UserId =          PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
 
           m_AccessTokenResponse.ScreenName =  PlayerPrefs.GetStrin(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
 
           m_AccessTokenResponse.Token = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
 
           m_AccessTokenResponse.TokenSecret = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);
            //Debug.Log ("ID"+m_AccessTokenResponse.UserId );
 
           // Debug.Log ("Name"+m_AccessTokenResponse.ScreenName );
 
          // Debug.Log ("Token"+m_AccessTokenResponse.Token );
 
          // Debug.Log ("Token Secret"+m_AccessTokenResponse.TokenSecret );
 
           if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
          !string.IsNullOrEmpty                (m_AccessTokenResponse.ScreenName) &&
         !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
         !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
          {
                string log = "LoadTwitterUserInfo - succeeded";
 
                 log += "\n    UserId : " + m_AccessTokenResponse.UserId;
 
                log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
 
                 log += "\n    Token : " + m_AccessTokenResponse.Token;
 
                 log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
 
                 print(log);
             }
 
      }
public void TwitterLogin()
{ 
    //Calling GetRequestToken function of Twitter API use your own consumer key and consumer secret.
     StartCoroutine(Twitter.API.GetRequestToken(CONSUMER_KEY, CONSUMER_SECRET,
                                                           new  Twitter.RequestTokenCallback(this.OnRequestTokenCallback)));
}
public void  SubmitPin()
{
   StartCoroutine(Twitter.API.GetAccessToken(CONSUMER_KEY, CONSUMER_SECRET, m_RequestTokenResponse.Token, m_PIN,
                           new Twitter.AccessTokenCallback(this.OnAccessTokenCallback)));
}
public void TwitterShare()
{
//put url of the image that you want to share
         string url="https://is1-ssl.mzstatic.com/image/thumb/Purple128/v4/8e/d7/e9         /8ed7e940-9fda-d1ca-995e-d4e2086a438c/pr_source.jpg/150x150bb.jpg";
        string savePath = Path.Combine(Application.persistentDataPath, "data");
//downloading image from url and saving to path
        downloadImage(url, savePath);
//loading image from the path in byte array
        byte[] imageBytes = loadImage(savePath);
//Calling PostTweet  function of  Twitter   API
StartCoroutine(Twitter.API.PostTweet(imageBytes,m_Tweet,CONSUMER_KEY,CONSUMER_SECRET,     m_AccessTokenResponse,  new Twitter.PostTweetCallback(this.OnPostTweet)));
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;
using SimpleJSON;
using System.Runtime.InteropServices;
public class TwitterHandller: MonoBehaviour
{
     Twitter.RequestTokenResponse m_RequestTokenResponse;
 
    Twitter.AccessTokenResponse m_AccessTokenResponse;
    public string CONSUMER_KEY;//Put oun consumer key here
 
     public string CONSUMER_SECRET;//Put own consumer secret here
    // Use this for initialization
 
    void Start()
    {
            LoadUserTwitterInfo();
     }
  void LoadUserTwitterInfo()
    {
           m_AccessTokenResponse = new Twitter.AccessTokenResponse();                    
           m_AccessTokenResponse.UserId =          PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
 
           m_AccessTokenResponse.ScreenName =  PlayerPrefs.GetStrin(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
 
           m_AccessTokenResponse.Token = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
 
           m_AccessTokenResponse.TokenSecret = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);
            //Debug.Log ("ID"+m_AccessTokenResponse.UserId );
 
           // Debug.Log ("Name"+m_AccessTokenResponse.ScreenName );
 
          // Debug.Log ("Token"+m_AccessTokenResponse.Token );
 
          // Debug.Log ("Token Secret"+m_AccessTokenResponse.TokenSecret );
 
           if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
          !string.IsNullOrEmpty                (m_AccessTokenResponse.ScreenName) &&
         !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
         !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
          {
                string log = "LoadTwitterUserInfo - succeeded";
 
                 log += "\n    UserId : " + m_AccessTokenResponse.UserId;
 
                log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
 
                 log += "\n    Token : " + m_AccessTokenResponse.Token;
 
                 log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
 
                 print(log);
             }
 
      }
public void TwitterLogin()
{ 
    //Calling GetRequestToken function of Twitter API use your own consumer key and consumer secret.
     StartCoroutine(Twitter.API.GetRequestToken(CONSUMER_KEY, CONSUMER_SECRET,
                                                           new  Twitter.RequestTokenCallback(this.OnRequestTokenCallback)));
}
public void  SubmitPin()
{
   StartCoroutine(Twitter.API.GetAccessToken(CONSUMER_KEY, CONSUMER_SECRET, m_RequestTokenResponse.Token, m_PIN,
                           new Twitter.AccessTokenCallback(this.OnAccessTokenCallback)));
}
public void TwitterShare()
{
//put url of the image that you want to share
         string url="https://is1-ssl.mzstatic.com/image/thumb/Purple128/v4/8e/d7/e9         /8ed7e940-9fda-d1ca-995e-d4e2086a438c/pr_source.jpg/150x150bb.jpg";
        string savePath = Path.Combine(Application.persistentDataPath, "data");
//downloading image from url and saving to path
        downloadImage(url, savePath);
//loading image from the path in byte array
        byte[] imageBytes = loadImage(savePath);
//Calling PostTweet  function of  Twitter   API
StartCoroutine(Twitter.API.PostTweet(imageBytes,m_Tweet,CONSUMER_KEY,CONSUMER_SECRET,     m_AccessTokenResponse,  new Twitter.PostTweetCallback(this.OnPostTweet)));
}
}
最后编辑:
作者:freeman
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据