PhoneGap外掛程式開發樣本(iOS)

來源:互聯網
上載者:User

PhoneGap外掛程式開發樣本(iOS) 重要提示:如果你同時使用phonegap(cordova)和sencha-touch,一定先引入phonegap的js檔案,否則你開發的外掛程式可能不被調用!


PhoneGap相關介紹:
PhoneGap官網
PhoneGap中國
PhoneGap外掛程式開發的簡單步驟:
1 建立一個基於PhoneGap的工程,如果還沒有PhoneGap SDK請去官網下載安裝,我目前使用的是PhoneGap1.3.0
2 在Plugins組下建立一個PGPlugin類的子類,命名為MyPGPlugin
MyPGPlugin.h

複製代碼
  1. #ifdef PHONEGAP_FRAMEWORK
  2. #import
  3. #else
  4. #import "PGPlugin.h"
  5. #endif
  6. @interface MyPGPlugin : PGPlugin {
  7. }
  8. - (void)addStr:(NSMutableArray*)arguments withDict:(NSDictionary*)options;
  9. @end


MyPGPlugin.m
複製代碼
  1. #import "MyPGPlugin.h"
  2. @implementation MyPGPlugin
  3. - (void)addStr:(NSMutableArray*)arguments withDict:(NSDictionary*)options
  4. {
  5. NSLog(@"%@", arguments);
  6. NSString *callbackId = [arguments pop];
  7. NSString *result = [NSString stringWithFormat:@"%@%@", [arguments objectAtIndex:0], [arguments objectAtIndex:1]];
  8. PluginResult *pr = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString:result];
  9. [self writeJavascript:[pr toSuccessCallbackString:callbackId]];
  10. }
  11. @end

3 在www檔案夾下建立一個js檔案,這裡我也命名為MyPGPlugin
複製代碼
  1. /**
  2. * 構造方法
  3. */
  4. function MyPGPlugin() {
  5. };
  6. /**
  7. * 字串相加
  8. *
  9. * @param str1, str2
  10. */
  11. MyPGPlugin.prototype.addStr = function(successCallback, failureCallback, str1, str2) {
  12. PhoneGap.exec(successCallback, failureCallback, "MyPGPlugin", "addStr", [str1, str2]);
  13. };
  14. /**
  15. * 載入MyPlugin對象
  16. */
  17. PhoneGap.addConstructor(function() {
  18. if(!window.plugins)
  19. {
  20. window.plugins = {};
  21. }
  22. window.plugins.myPGPlugin = new MyPGPlugin();
  23. });

4 修改PhoneGap.plist,展開Plugins,按”+”號新加一列.添加key MyPGPlugin和value MyPGPlugin
5 使用,在index.html中添加如下代碼:
匯入js檔案
複製代碼
  1. <script type="text/javascript" charset="utf-8" src="MyPGPlugin.js"></script>

定義js函數,添加在onDeviceReady函數定義後
複製代碼
  1. function addStr(){
  2. var str1 = "str1";
  3. var str2 = "str2";
  4. window.plugins.myPGPlugin.addStr(successCallback, failureCallback, str1, str2);
  5. }
  6. //成功時回調的方法
  7. function successCallback(data){
  8. alert("result:"+ data);
  9. }
  10. //失敗時返回的方法
  11. function failureCallback(data){
  12. alert("error:"+ data);
  13. }

添加一個button,測試一下
複製代碼
  1. MyPGPlugin Test

6 編譯運行。

參考連結:
http://www.adobe.com/devnet/html5/articles/extending-phonegap-with-native-plugins-for-ios.html
http://prostorys.blog.51cto.com/1522778/843699
http://wiki.phonegap.com/w/page/36753496/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20iOS (被牆)

隨後附上代碼免分!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.