2012-08-01 27 views
2

這一天我爲這個設置撓了撓頭。我覺得編寫插件代碼比安裝容易得多。我遵循以下步驟來爲ios編寫一個簡單的電話差距插件。但不幸的是,無法找到我錯過的地方。請打印出錯誤/混淆/錯誤的代碼部分。一步一步爲iOS編寫自己的手機差距插件

PhoneGap的設置:

1)封閉的Xcode。 [我有Xcode 4.3.3] 2)下載最新的手機差距。版本2.0 3)中的PhoneGap-的PhoneGap-2dbbdab-1的目錄,安裝 科爾多瓦-2.0.0.pkg

4)冉以下代碼:

$ ./path/to/cordova-ios/斌/創建/路徑/到/ my_new_cordova_project com.example.cordova_project_name CordovaProjectName

其次http://docs.phonegap.com/en/2.0.0/guide_command-line_index.md.html#Command-Line%20Usage

5)打開Xcode項目。

6)創建包含www目錄下文件HelloPlugin.js ..

var HelloPlugin = { 

callNativeFunction: function (success, fail, resultType) { 
    return Cordova.exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeFunction", [resultType]); 
} 
}; 

7)在插件目錄

HelloPlugin.h包含:

#import "CDVPlugin.h" 

@interface HelloPlugin : CDVPlugin { 
     NSString* callbackID; 
} 

@property (nonatomic, copy) NSString* callbackID; 

- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; 

@end 

與HelloPlugin。 m包含:

#import "HelloPlugin.h" 

@implementation HelloPlugin 

@synthesize callbackID;  


- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { 

    //get the callback id 
    NSString *callbackId = [arguments pop]; 

    NSLog(@"Hello, this i s a native function called from PhoneGap/Cordova!"); 


    NSString *resultType = [arguments objectAtIndex:0]; 
    CDVPluginResult *result; 

    if ([resultType isEqualToString:@"success"]) { 
     result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Success :)"]; 
     [self writeJavascript:[result toSuccessCallbackString:callbackId]]; 
    } 
    else { 
     result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Error :("]; 
     [self writeJavascript:[result toErrorCallbackString:callbackId]]; 
    } 
} 

@end 
在Cordova.plist

是否加入 com.tricedesigns.HelloPlugin,與HelloPlugin以鍵/值和類型爲字符串。

index.html中

<script type="text/javascript" charset="utf-8" src="HelloPlugin.js"></script> 

    <script type="text/javascript"> 
    function callNativePlugin(returnSuccess) { 
      alert("Inside callNativePlugin"); 
      HelloPlugin.callNativeFunction(nativePluginResultHandler, nativePluginErrorHandler, returnSuccess); 
      alert("End of Hello PLugin"); 

     } 

     function nativePluginResultHandler (result) { 
      alert("SUCCESS: \r\n"+result); 
     } 

     function nativePluginErrorHandler (error) { 
      alert("ERROR: \r\n"+error); 
     } 


</script> 

現在,當我按一下按鈕,本地功能不調用。如何前進?

+0

我的回答可能對你有幫助http://stackoverflow.com/questions/11723643/what-kind-of-applications-can-be-created-using-jquery-mobile/11724388#11724388 – 2012-08-01 14:54:02

回答

4

我寫了一個tutorial的分步說明,指導如何爲iOS編寫PhoneGap插件。這可能對您有所幫助。祝你好運!