Temboo 为他们的 Choreos(API 请求)提供了 Objective-C 代码。我使用了 Temboo指令并能够让代码在 viewVontroller.m 中运行:
#import "ViewController.h"
#import "TMBUtilities.h"
#import "TMBChoreography.h"
#import "TMBTembooSession.h"
@interface Post : NSObject <TMBChoreographyDelegate>
-(void)runPostChoreo;
-(void)choreographyDidFailWithError:(NSError*)error;
-(void)choreographyDidFinishExecuting:
(TMBUtilities_HTTP_Post_ResultSet*)result;
@end
@implementation Post
-(void)runPostChoreo {
TMBTembooSession *session = [[TMBTembooSession alloc] initWithAccount:@"xxxxx" appKeyName:@"xxxxx" andAppKeyValue:@"xxxxx"];
TMBUtilities_HTTP_Post *postChoreo = [[TMBUtilities_HTTP_Post alloc] initWithSession:session];
TMBUtilities_HTTP_Post_Inputs *postInputs = [postChoreo newInputSet];
[postInputs setUsername:@"xxxxx"];
[postInputs setURL:@"https://anywebsite.com/notes"];
[postInputs setPassword:@"xxxxx"];
[postInputs setRequestParameters:@"{\"utf8\":\"xxxxx\",\"xxxxx\":\"Post This Info\"}"];
[postChoreo executeWithInputs:postInputs delegate:self];
}
-(void)choreographyDidFailWithError:(NSError*)error {
NSLog(@"Error - %@", error);
}
-(void)choreographyDidFinishExecuting:(TMBUtilities_HTTP_Post_ResultSet*)result {
NSLog(@"%@", [result getHTTPLog]);
NSLog(@"%@", [result getResponseStatusCode]);
NSLog(@"%@", [result getResponse]);
}
@end
@implementation ViewController
- (IBAction)buttonPost:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
Post *test = [[Post alloc] init];
[test runPostChoreo];
});
}
对于 Swift 转换,我有以下桥接表
#import "TMBUtilities.h"
#import "TMBChoreography.h"
#import "TMBTembooSession.h"
然后我在 ViewController.Swift 的顶部添加了转换后的 Temboo 代码。我在第一行出现以下错误"Type 'Post' does not conform to protocol 'TMBChoreographyDelegate'"。我将不胜感激任何提示。谢谢。
class Post: NSObject, TMBChoreographyDelegate {
func runPostChoreo() {}
func choreographyDidFailWithError(error:NSError) {}
func choreographyDidFinishExecuting(result: TMBUtilities_HTTP_Post_ResultSet) {}
}
class Post{
func runPostChoreo() {
let session: TMBTembooSession = TMBTembooSession(account: "xxxxx", appKeyName: "xxxxx", andAppKeyValue: "xxxxx")
let postChoreo: TMBUtilities_HTTP_Post = TMBUtilities_HTTP_Post(session: session)
let postInputs: TMBUtilities_HTTP_Post_Inputs = postChoreo.newInputSet()
postInputs.setUsername("xxxxx")
postInputs.setURL("https://anywebsite.com/notes")
postInputs.setPassword("xxxxx")
postInputs.setRequestParameters("{\"utf8\":\"xxxx\",\"xxxxx\":\"Post This Info\"}")
postChoreo.executeWithInputs(postInputs, delegate: self)
}
func choreographyDidFailWithError(error: NSError) {
NSLog("Error - %@", error)
}
func choreographyDidFinishExecuting(result: TMBUtilities_HTTP_Post_ResultSet) {
NSLog("%@", result.getHTTPLog())
NSLog("%@", result.getResponseStatusCode())
NSLog("%@", result.getResponse())
}
}
class ViewController: UIViewController {
@IBAction func myButton(sender: AnyObject) {
dispatch_async(dispatch_get_main_queue(), {() -> Void in
var test: Post = Post()
test.runPostChoreo()
myRunLoop.run()
})