【发布时间】:2012-10-06 03:16:12
【问题描述】:
如何像 iPhone 内置的照片应用程序那样将图片附加到 twitter 帖子? 如果任何机构有一些示例代码,那将是一个很大的帮助。 谢谢。
【问题讨论】:
-
:[tweetViewController addImage:img];
标签: iphone ios twitter file-sharing
如何像 iPhone 内置的照片应用程序那样将图片附加到 twitter 帖子? 如果任何机构有一些示例代码,那将是一个很大的帮助。 谢谢。
【问题讨论】:
标签: iphone ios twitter file-sharing
其他答案建议TWTweetComposeViewController,但是如果你可以避免使用这个类,它现在在 iOS 6 中已被弃用,
请看这里:TWTweetComposeViewController deprecated in IOS6
来自 Apple 自己,WWDC 2012, session 306 presentation PDF:
推特框架
• Twitter 框架已弃用
• 不要使用 TWTweetComposeViewController
现在要使用 Twitter,你应该使用 Social 框架的 SLComposeViewController 类,它的用法几乎与 TWTweetComposeViewController 相同。
您可能需要支持 iOS 5,在这种情况下,您别无选择,只能使用 TWTweetComposeViewController 类,但您应该努力检查 SLComposeViewController 并在可用时使用它,仅仅是因为这个在不久的将来,当对 iOS 5 的支持被取消时,TWTweetComposeViewController 类真的可能会消失,这将为您节省时间和精力。如果您现在为了简单起见而依赖 Twitter 框架,因为它确实适用于 iOS 5 和 6,那么您是目光短浅的,并且您会在稍后的某个时间遇到问题,只需多几行代码就可以做到这一点这意味着您无需担心未来的 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...
}
【讨论】:
Social.framework,进入您的项目,进入目标,然后构建阶段,打开Link Binary With Libraries,点击+按钮并搜索Social.framework。然后在您的视图控制器中确保导入:#import <Social/Social.h>,您可以在标头或实现中执行此操作。不需要 Account 框架,如果需要支持 iOS 5 可以添加 Twitter 框架
addImage: 不是setImage:,抱歉:)
我是如何使用它的:
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];
【讨论】:
TWTweetComposeViewController,它已被弃用。您应该使用SLComposeViewController 类。用法几乎相同。
UIImage,在您的应用程序中,SLComposeViewController 或TWTweetComposeViewController 处理上传到 Twitter。但是请看我的回答,因为你应该避免使用TWTweetComposeViewController:stackoverflow.com/a/12907850/662605
如果你使用的是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];
};
}
}
}
在你想发推特的地方调用这个函数
并根据需要进行适当的更改..
祝你好运..
【讨论】:
我现在只是使用UIActivityViewController 在 Twitter 上发帖。
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[@"Default Text", [UIImage imageNamed:@"imageName"]] applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
这将呈现一个控制器,用户可以在其中决定做什么(发布到 Twitter、发布到 Facebook 等...)
然后它使用系统推文表等...来做。
您不必提供默认文本。这无论如何都可以被覆盖。
哦,另外,这不需要任何框架。
【讨论】: