การแก้ปัญหาการโหลดรูปโปรไฟล์ Facebook ใน iOS ไม่ได้เพราะติดการ Redirect by

30
Nov
0

หลังจากที่อัพ iOS + XCode เวอร์ชั่นใหม่แล้ว ทำให้เกิดปัญหา
ซึ่งจาก Error แล้วเดาได้ว่าทาง iOS ไม่ยอมให้มีการโหลดรูปจาก url ที่มีการ redirect แล้ว
เลยมีวิธีแก้ไขดังนี้ครับ

โค้ดเดิม

IEnumerator LoadPictureWWW(string url)
{
WWW www = new WWW(url);
yield return www;
Material mat = new Material(Shader.Find("Unlit/Transparent Colored (AlphaClip)"));
mat.mainTexture = www.texture;
UITexture _uit = gameObject.GetComponent();
if (_uit == null)
{
Debug.Log("playerWWW == null");
gameObject.AddComponent();
}
this.GetComponent().material = mat;
}

ตามด้านบนวิธีเก่าจะโหลดไม่สำเร็จเพราะยิงไปแล้วโดน Redirect แต่ทาง iOS จะไม่ทำการ Redirect ต่อทำให้ www.texture ไม่ได้ค่าออกมา
วิธีการแก้ไขคือเราต้องยิง url ไปรอบนึงแล้วเอา Result มาดูค่า url ที่ได้จากการ redirect แล้วมาเพื่อยิงเองต่ออีกครั้งนึงดังนี้


public static IEnumerator _loadFacebookProfileImage (string userId, UITexture texture, int size)
{
string url = string.Format ("https://graph.facebook.com/{0}/picture?width={1}&height={1}&redirect=false", userId, size);
WWW www1 = new WWW (url);
yield return www1; //ทำการยิงครั้งแรก
if (string.IsNullOrEmpty (www1.error)) {
JSONNode result = JSON.Parse (www1.text) //แปลง return ที่ได้มาแล้วแกะข้อมูล;
string realUrl = result ["data"] ["url"]; //เอาข้อมูล data.url มาใช้ยิงอีกครั้ง
WWW www2 = new WWW (realUrl);
yield return www2; //ยิงครั้งที่สอง จะได้ข้อมูลรูปตามปกติ
Material mat = new Material (Resources.Load ("EmptyMaterial", typeof(Material)) as Material);
if (string.IsNullOrEmpty (www2.error))
mat.mainTexture = www2.texture;
else
Debug.LogError ("_loadFacebookProfileImage www2 Error: " + www2.error);
if(texture != null) texture.material = mat;
} else
Debug.LogError ("_loadFacebookProfileImage www1 Error: " + www1.error);
}

เพียงเท่านี้ก็ใช้ได้แล้วครับ

Enjoy this article?

Consider subscribing to our RSS feed!

ไม่มีความเห็น

ยังไม่มีความเห็น

ใส่ความเห็น

RSS feed for comments on this post

 เราชนะรอบ 4 | ยืมเงิน 3000 ด่วน | แอพกู้เงิน | แอพเงินด่วน | สินเชื่อออนไลน์อนุมัติทันที | Site Map | กู้เงินก้อน | กระเป๋าตัง | thisshop และ ยืมเงินฉุกเฉิน 5000 ด่วน