iPhone の組み込みの写真アプリのように、 Twitterの投稿に写真を添付するにはどうすればよいですか? 体にサンプルコードがあれば、それは大きな助けになります。ありがとう。
4 に答える
他の回答は示唆していTWTweetComposeViewController
ますが、このクラスの使用を避けることができる場合は、iOS 6 で非推奨になりました。
こちらをご覧ください: TWTweetComposeViewController deprecated in IOS6
Apple 自身から、WWDC 2012、セッション 306 プレゼンテーション PDF :
Twitter フレームワーク
• Twitter フレームワークは廃止されました
• TWTweetComposeViewController を使用しないでください
Twitter を使用するSLComposeViewController
には、フレームワークのクラスを使用する必要があります。Social
使用方法は とほとんど同じですTWTweetComposeViewController
。
iOS 5 をサポートする必要があるかもしれません。その場合、TWTweetComposeViewController
クラスを使用する以外に選択肢はありませんが、可能な場合はそれを確認して使用するように努力する必要がSLComposeViewController
あります。これにより、時間と労力を節約できるからです。将来、iOS 5 のサポートが終了すると、このTWTweetComposeViewController
クラスは本当になくなる可能性があります。iOS 5 および 6 で動作するように、簡単にするために現在 Twitter フレームワークに依存している場合、近視眼的であり、後で問題が発生することになります。今後の iOS SDK のリリースについて心配する必要はありません。
と をインポートTwitter.framework
し、両方をオプションのインポート (必須ではない)Social.framework
としてマークする必要があります。
コード例:
UIImage *myImage = [...]; // an image
if( NSClassFromString(@"SLComposeViewController") ){
// We have the Social framework in our iOS system
// iOS 6 and later will use this
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
SLComposeViewController *twitterCompose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitterCompose addImage:myImage]; // Adding your UIImage
twitterCompose.completionHandler = ^(SLComposeViewControllerResult result){
// Handle result, dismiss view controller
[self dismissViewControllerAnimated:YES
completion:nil];
};
[self presentViewController:twitterCompose
animated:YES
completion:nil];
}else{
// the user does not have Twitter set up
}
}else if( NSClassFromString(@"TWTweetComposeViewController") ){
// We don't have the Social framework, work with the Twitter framework
// iOS 5 only will use this
if( [TWTweetComposeViewController canSendTweet] ){
TWTweetComposeViewController *twitterCompose = [[TWTweetComposeViewController alloc] init];
[twitterCompose addImage:myImage];
twitterCompose.completionHandler = ^(TWTweetComposeViewControllerResult result){
// Handle result, dismiss view controller
[self dismissViewControllerAnimated:YES
completion:nil];
};
[self presentViewController:twitterCompose
animated:YES
completion:nil];
}else{
// the user hasn't go Twitter set up on their device.
}
}else{
// Wow you're going retro with this app,
// you must be on iOS 4 if you ever get here...
}
ここで私はそれをどのように使用しますか:
NSLog(@"Ready to Tweet.");
TWTweetComposeViewController *tweetComposer = [[TWTweetComposeViewController alloc] init];
[tweetComposer setInitialText:[NSString stringWithFormat:@"My message"]];
[tweetComposer addImage:[UIImage imageNamed:@"114x114"]];
[tweetComposer addURL:[NSURL URLWithString:@"http://myPage"]];
tweetComposer.completionHandler = ^(TWTweetComposeViewControllerResult result){
if(result == TWTweetComposeViewControllerResultDone){
NSLog(@"Tweeted.");
} else if(result == TWTweetComposeViewControllerResultCancelled) {
NSLog(@"Cancelled.");
}
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
};
[self presentModalViewController:tweetComposer animated:YES];
iOS 5.0を使用している場合は、次のような画像を直接投稿できます
フレームワークを追加する twitter.framework
Twitter/TWTweetComposeViewController.h をインポートします。
-(void)postToTwittert
{
Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");
if (TWTweetComposeViewControllerClass != nil) {
if([TWTweetComposeViewControllerClass respondsToSelector:@selector(canSendTweet)]) {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:@"text"];
[twitter addImage:[UIImage imageNamed:@"imagename"]];
[twitter addURL:[NSURL URLWithString:@"http://www.google.com"]];
[self presentViewController:twitter animated:YES completion:nil];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone)
{
NSLog(@"success for twitter post");
}
else if(res == TWTweetComposeViewControllerResultCancelled)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Canceled" message:@"Your Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
[self dismissModalViewControllerAnimated:YES];
};
}
}
}
Twitter の投稿が必要な場所でこの関数を呼び出します
必要な適切な変更を行います..
幸運..
UIActivityViewController
今はTwitterに投稿するだけです。
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[@"Default Text", [UIImage imageNamed:@"imageName"]] applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
これにより、ユーザーが何をすべきかを決定できるコントローラーが表示されます (Twitter への投稿、Facebook への投稿など)。
その後、システム ツイート シートなどを使用して実行します。
デフォルトのテキストを提供する必要はありません。これはとにかく上書きできます。
また、これにはフレームワークは必要ありません。