我在想如何为Twitter或Facebook或任何其他类创建一个类。所以只需将该类导入到我的项目中,并开始使用一个代码。例如。如果我使用twitter框架,那么我必须编写它的功能。我只是想通过导入它来创建一个功能类,它将在我所有未来的项目中起作用。 例如:如何使方法与uiviewcontroller一起工作
任何类或任何东西:
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image
{
// ALL TWITTER Code HERE;
}
,在任何项目中我主要的viewController:
- (IBAction)socialBtn:(id)sender
{
[self shareOnTwitter:@"This is Text" withUrl:nil withImage:nil];
}
更新:只是试图分类的方式,但没有响应返回,任何想法,我错了吗?:
UIViewController + CustomMethods.h
#import <UIKit/UIKit.h>
#import <Social/Social.h>
@interface UIViewController (CustomMethods)
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image;
@end
的UIViewController + CustomMethods.m
#import "UIViewController+CustomMethods.h"
@implementation UIViewController (CustomMethods)
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:text];
[tweetSheet addImage:image];
[tweetSheet addURL:url];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"You can't Post a Status right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
@end
mainViewController:
NSString *title = [webViewOutlet stringByEvaluatingJavaScriptFromString:@"document.title"];
NSURL *url = [[webViewOutlet request] URL];
[self shareOnTwitter:title withUrl:url withImage:nil];
Thanks一个..特别为克里斯Loonam,学会了如何使委托方法.... – user777304