私はTwitterをiOS5アプリと統合しました。つまり、ツイートシートは正常に機能しますが、ツイートボタンを押すたびにTwitterインターフェイスが表示され、自己表示のモーダルビューコントローラーを含めないことでそれを取り除くことができました。コード内にありますが、ツイートシートの送信ボタンを押さない限りツイートはツイートされないため、ツイートを自動化する方法が見つかりません。
これを回避する方法はありますか?本質的には、ツイートボタンを押した瞬間にツイートをツイートしたいのですが、ツイートシートの送信ボタンをもう一度押したくないのですか?可能?
編集:以下の回答に投稿されたリンクに従って、Twitter APIの例をいくつか調べて、このコードをhttps://dev.twitter.com/docs/ios/posting-images-using-twrequestで見つけました。正常に動作します。しかし、アプリがクラッシュするというコメントでマークされた部分にアプリが到達するとクラッシュします。エラー「NSInvalidARgumentException」「データパラメータ」はnilで、数字の束です。
また、この行を使用してアカウントを設定することはできません[request setAccount:[self.accounts objectAtIndex:0]];
。タイプ'myviewcontrollername'のオブジェクトに見つからないエラープロパティアカウントが表示されます。
(void)performTWRequestUpload
{
NSURL *url =
[NSURL URLWithString:
@"https://upload.twitter.com/1/statuses/update_with_media.json"];
// Create a POST request for the target endpoint
TWRequest *request =
[[TWRequest alloc] initWithURL:url
parameters:nil
requestMethod:TWRequestMethodPOST];
// self.accounts is an array of all available accounts;
// we use the first one for simplicity
[request setAccount:[self.accounts objectAtIndex:0]];
// The "larry.png" is an image that we have locally
UIImage *image = [UIImage imageNamed:@"larry.png"];
// Obtain NSData from the UIImage
NSData *imageData = UIImagePNGRepresentation(image);
// Add the data of the image with the
// correct parameter name, "media[]"
[request addMultiPartData:imageData
withName:@"media[]"
type:@"multipart/form-data"];
// NB: Our status must be passed as part of the multipart form data
NSString *status = @"just setting up my twttr #iOS5";
// Add the data of the status as parameter "status"
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
withName:@"status"
type:@"multipart/form-data"];
// Perform the request.
// Note that -[performRequestWithHandler] may be called on any thread,
// so you should explicitly dispatch any UI operations to the main thread
//Problem code part upon reaching which app crashes
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *dict =
(NSDictionary *)[NSJSONSerialization
JSONObjectWithData:responseData options:0 error:nil];
// Log the result
NSLog(@"%@", dict);
dispatch_async(dispatch_get_main_queue(), ^{
// perform an action that updates the UI...
});
}];
}****