我在想,我怎样才能为 twitter 或 facebook 或任何东西开设课程。所以只要我将该类导入我的项目,它就开始使用一个代码。例如。如果我使用 twitter 框架,那么我必须编写它的功能。我只想通过导入它来创建一个类,它将在我未来的所有项目中运行。例子:
任何课程或任何东西:
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image
{
// ALL TWITTER Code HERE;
}
在我的任何项目的主视图控制器中:
- (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
主视图控制器:
NSString *title = [webViewOutlet stringByEvaluatingJavaScriptFromString:@"document.title"];
NSURL *url = [[webViewOutlet request] URL];
[self shareOnTwitter:title withUrl:url withImage:nil];