我使用代码从以下问题向wordpress博客发表评论:
Post comment to WordPress Blog from iPhone programmatically
我做了一些修改以从UITextFields获取文本:
- (IBAction)postComment:(id)sender {
NSString *post_url = @"http://movilarena.com/wp-comments-post.php";
NSString *post_content = @"comment_post_ID=%@&comment_parent=%@&author=%@&email=%@&comment=%@";
NSString *post_str = [NSString stringWithFormat:post_content, @"1", @"0", self.txtName.text, self.txtEmail.text, self.txtComment.text];
NSData *data = [NSData dataWithBytes:[post_str UTF8String] length:[post_str length]];
NSURL * url = [NSURL URLWithString:post_url];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[req setHTTPBody:data];
//Synchronous
NSURLResponse *response;
NSError *err;
NSData *ret = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *retString = [NSString stringWithUTF8String:[ret bytes]];
NSLog(@"%@",retString);
}
我的问题是sendSynchronousRequest返回错误:
执行被中断,原因:EXC_BAD_ACCESS(代码= 1, 地址=为0x0)。该过程已恢复到之前的状态 执行。
我会感谢任何建议,以确定这里有什么问题。我使用XCode 4.5.2并在带有iOS 6.0.1的iPhone 4S上运行代码
答案 0 :(得分:0)
解决了自己。字段comment_post_ID
应该是我要回复的帖子的ID。
NSString *post_content = @"comment_post_ID=%d&comment_parent=%@&author=%@&email=%@&comment=%@";
NSString *post_str = [NSString stringWithFormat:post_content, self.postID, @"0", self.txtName.text, self.txtEmail.text, self.txtComment.text];