如何像iPhone内置的照片应用程序那样,在Twitter帖子中附加一张图片? 如果有人有一些示例代码,那将是大大的帮助。 谢谢。
如何像iPhone内置的照片应用程序那样,在Twitter帖子中附加一张图片? 如果有人有一些示例代码,那将是大大的帮助。 谢谢。
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...
}
这是我如何使用它的方法:
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
类,它的使用方式几乎相同。 - DanielUIImage
,SLComposeViewController
或 TWTweetComposeViewController
处理上传到 Twitter。但是请查看我的答案,因为你应该避免使用 TWTweetComposeViewController
:https://dev59.com/umnWa4cB1Zd3GeqP2qRc#12907850 - Daniel添加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
来发布推特。
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[@"Default Text", [UIImage imageNamed:@"imageName"]] applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
这将展示一个控制器,用户可以决定要做什么(发布到 Twitter、发布到 Facebook 等等)
然后它使用系统的 tweetaheet 等来完成。
您无需提供默认文本。这可以被覆盖。
哦,还有,这不需要任何框架。