unity 引入 ios 第三方sdk

來源:互聯網
上載者:User

標籤:des   style   blog   http   使用   os   

原地址:http://blog.csdn.net/u012085988/article/details/17785023

unity開發中ios應用時,要想成功引入第三方sdk,首先得知道c#與object-c如何互動。這裡有篇博文介紹了unity開發中,如何?c#與oject-c互相調用。

http://blog.csdn.net/u012085988/article/details/17761433

 

下面介紹下unity開發ios應用中,引入第三方sdk的流程:

1、將sdk中要用到的介面用C語言進行封裝。因為上你們那篇博文提到了c#與object-c互動,要通過中繼語言----C語言實現。

2、將封裝好的C函數放在.mm檔案中,然後將這個mm檔案放到unity工程目錄下的Assets/Plugins/IOS目錄下。

3、按照上面那篇博文介紹的方法,引用第1步中封裝的C函數。

4、用unity匯出xcode工程,因為發布app時要用xcode來打包簽名。

5、在xcode中引入第三方SDK。到這裡應該都沒問題了,因為做個ios開發的基本都會用xcode;且一般sdk的使用手冊也都介紹了如何在xcode工程中引入sdk。

6、編譯調試,打包發布。

 

下面以百度Frontia為例,實現社會化分享功能:

1、建立unity工程。在Assets下建立如下目錄結構:Plugins/IOS/

2、建立share.mm檔案,放在1中建立的目錄下。定義一個C函數share(),檔案中加入以下代碼。

 

[objc] view plaincopy 
  1. extern "C"    
  2.     
  3. {    
  4.     
  5.     void share(char* title, char* msg, char* url)    
  6.     
  7.     {    
  8.     
  9.         // 先把函數的格式定好(規定好傳回值和參數表,供c#使用  
  10.     }    
  11.     
  12. }    



 

3、建立c#指令碼,繪製一個按鈕,用於測試分享功能。指令碼內容如下:

 

[csharp] view plaincopy 
  1. using UnityEngine;    
  2. using System.Collections;    
  3. using System.Runtime.InteropServices;    
  4. public class testscript : MonoBehaviour {      
  5.     
  6.     [DllImport("__Internal")]    
  7.     private static extern void share(string title, string msg, string url);    
  8.     
  9.     void OnGUI()    
  10.     {    
  11.         if (GUI.Button (new Rect (100, 100, 100, 50), "test share")) {    
  12.             share("omytitle", "omymsg", "www.baidu.com");            
  13.         }    
  14.     }    
  15. }    

 

關於這兩段代碼有疑問的童鞋,需要先看看文章開頭提到的那篇博文。

4、匯出xcode工程。

5、按照百度Frontia官方文檔,將Fraontia-sdk引入xcode中。

具體參見官網http://developer.baidu.com/wiki/index.php?title=docs/frontia/guide-ios/overview

6、找到步驟2中建立的.mm檔案(注意該檔案還在unity工程中,xcode工程中雖然也有一個這樣的檔案,但此檔案只是一個“替身”,不能修改。所以我們要修改的是unity工程下的Plugins/IOS/share.mm檔案),修改檔案如下:

 

 

[objc] view plaincopy 
  1. #import <Frontia/Frontia.h>  
  2.   
  3.   
  4.   
  5. #define APP_KEY @"iG2ffdkYaq8kIjrSfvjMcUrf"  
  6.   
  7.   
  8. extern "C"  
  9.   
  10. {  
  11.   
  12.     void test(char* title, char* msg, char* url)  
  13.   
  14.     {  
  15.   
  16.         NSString* nstitle = [[NSString alloc] initWithUTF8String:title];  
  17.   
  18.         NSString* nsmsg = [[NSString alloc] initWithUTF8String:msg];  
  19.   
  20.         NSString* nsurl = [[NSString alloc] initWithUTF8String:url];  
  21.   
  22.           
  23.   
  24.         FrontiaShare *share = [Frontia getShare];  
  25.   
  26.           
  27.   
  28.         //授權取消回呼函數  
  29.   
  30.         FrontiaShareCancelCallback onCancel = ^(){  
  31.   
  32.             NSLog(@"OnCancel: share is cancelled");  
  33.   
  34.         };  
  35.   
  36.           
  37.   
  38.         //授權失敗回呼函數  
  39.   
  40.         FrontiaShareFailureCallback onFailure = ^(int errorCode, NSString *errorMessage){  
  41.   
  42.             NSLog(@"OnFailure: %d  %@", errorCode, errorMessage);  
  43.   
  44.         };  
  45.   
  46.           
  47.   
  48.         //授權成功回呼函數  
  49.   
  50.         FrontiaMultiShareResultCallback onResult = ^(NSDictionary *respones){  
  51.   
  52.             NSLog(@"OnResult: %@", [respones description]);  
  53.   
  54.         };  
  55.   
  56.           
  57.   
  58.         FrontiaShareContent *content=[[FrontiaShareContent alloc] init];  
  59.   
  60.         content.url = nsurl;  
  61.   
  62.         content.title = nstitle;  
  63.   
  64.         content.description = nsmsg;  
  65.   
  66.         content.imageUrl = @"http://apps.bdimg.com/developer/static/04171450/developer/images/icon/terminal_adapter.png";  
  67.   
  68.           
  69.   
  70.         NSArray *platforms = @[FRONTIA_SOCIAL_SHARE_PLATFORM_SINAWEIBO,FRONTIA_SOCIAL_SHARE_PLATFORM_QQWEIBO,FRONTIA_SOCIAL_SHARE_PLATFORM_QQ,FRONTIA_SOCIAL_SHARE_PLATFORM_RENREN,FRONTIA_SOCIAL_SHARE_PLATFORM_KAIXIN,FRONTIA_SOCIAL_SHARE_PLATFORM_EMAIL,FRONTIA_SOCIAL_SHARE_PLATFORM_SMS];  
  71.   
  72.           
  73.   
  74.         [share showShareMenuWithShareContent:content menuStyle:FRONTIA_SOCIAL_SHARE_STYLE_LIGHT displayPlatforms:platforms supportedInterfaceOrientations:UIInterfaceOrientationMaskPortrait isStatusBarHidden:NO cancelListener:onCancel failureListener:onFailure resultListener:onResult];  
  75.   
  76.     }  
  77.   
  78. }  



 

7、appDelegate的OpenURL中加入如下代碼

 

[objc] view plaincopy 
  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation  
  2. {  
  3.     //SSO或者分享的回調  
  4.     return [[Frontia getShare] handleOpenURL:url];  
  5. }  



 

8、編譯運行,真機調試。

 

關於分享菜單中,按鈕點擊沒反應問題。

需要在Build Setting中的other Linker Flags 加上 -ObjC 標誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.