发布到 yFrog objc
我正在尝试通过我的应用程序使用 yFrog 将图像和视频发布到 twitter,但当我提出请求时似乎什么也没有发生...任何人都可以看到我做错了什么或为我指出正确的方向吗?谢谢
-(IBAction)yFrogToTwitter
{
// create the URL
//used to render bigger images videos
//NSURL *postURL = [NSURL URLWithString:@"http://render.imageshack.us/renderapi/start"];
//below is used to directly upload to twitter
NSURL *postURL = [NSURL URLWithString:@"http://yfrog.com/api/uploadAndPost"];
// create the connection
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
// change type to POST (default is GET)
[postRequest setHTTPMethod:@"POST"];
// create data
NSMutableData *postBody = [NSMutableData data];
//NSString *media = PickedImage.image;
NSString *username = twitterEngine.username;
NSString *password = twitterEngine.password;
NSString *message = TweetBody.text;
NSString *source = @"ThemeCatcher";
NSString *api_key= kYFrogKey;
// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data;
boundary=%@",stringBoundary];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
// username part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// password part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[password dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// api_key
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[api_key dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// message part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// source part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[source dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// media part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"media\";
filename=\"fish.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = UIImagePNGRepresentation(PickedImage.image);
// add Image to body
[postBody appendData:imageData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post
[postRequest setHTTPBody:postBody];
// pointers to some necessary objects
//NSURLResponse* response;
//NSError* error;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest
delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
I am trying to post images and videos to twitter with yFrog through my application, but nothing seems to even happen when I make the request... can anyone see what I am doing wrong or point me in the right direction? thank you
-(IBAction)yFrogToTwitter
{
// create the URL
//used to render bigger images videos
//NSURL *postURL = [NSURL URLWithString:@"http://render.imageshack.us/renderapi/start"];
//below is used to directly upload to twitter
NSURL *postURL = [NSURL URLWithString:@"http://yfrog.com/api/uploadAndPost"];
// create the connection
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
// change type to POST (default is GET)
[postRequest setHTTPMethod:@"POST"];
// create data
NSMutableData *postBody = [NSMutableData data];
//NSString *media = PickedImage.image;
NSString *username = twitterEngine.username;
NSString *password = twitterEngine.password;
NSString *message = TweetBody.text;
NSString *source = @"ThemeCatcher";
NSString *api_key= kYFrogKey;
// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data;
boundary=%@",stringBoundary];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
// username part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// password part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[password dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// api_key
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[api_key dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// message part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// source part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data;
name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[source dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// media part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"media\";
filename=\"fish.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = UIImagePNGRepresentation(PickedImage.image);
// add Image to body
[postBody appendData:imageData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post
[postRequest setHTTPBody:postBody];
// pointers to some necessary objects
//NSURLResponse* response;
//NSError* error;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest
delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有实现委托回调方法(至少在上面的代码中)我看到您将 self 设置为委托,但我没有看到回调。还要确保您正在检查错误回调
以下是您需要实现的委托回调方法:
http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate
didFailWithError 是如果您遇到问题,那就是大问题。确保您读取了所有 NSError 数据并至少记录它。
例如:
You're not implementing the delegate call back methods (at least in the code above) I see you setting self to the delegate but I don't see the callbacks. Also ensure that you're checking the error callback
Here's the delegate callback methods you need to implement:
http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate
didFailWithError is the big one if you're having issues. make sure you read all the NSError data and at a minimum log it.
For example:
我的 XMLParser 下面
做一些这种性质的事情
在您的委托中以共享的心情 !希望这有帮助
My XMLParser below
In your Delegate do something of this nature
In the mood of sharing! Hope this helps