我在想如何为twitter或facebook或其他任何课程上课.所以我只需将该类导入到我的项目中,然后开始使用一个代码.例如.如果我使用twitter框架,那么我必须编写它的函数.我只是想通过导入它来
例:
任何类别或任何东西:
-(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];
我相信这是一个可以使用名为“Categories”的Objective-C功能的实例.类别是已建立的类的扩展.例如,我们可以为UIViewController创建一个类别,它将为所有UIViewController类添加新方法.
为此,我们执行以下操作:
制作.h文件来声明原型:
@interface UIViewController (CategoryName) // EG. (Twitter) // PROTOTYPES... EG: -(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image; @end
以及实现它们的相应.m文件:
@implementation UIViewController (CategoryName)
// IMPLEMENTATIONS... EG:
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image {
// Code...
}
@end
(请注意,在XCode中声明新文件时,它具有可以使用的“类别”模板.)
现在,无论何时导入.h文件,UIViewController作为一个类都将包含您扩展它的方法!
Here’s一个关于我通过快速谷歌搜索找到的类别的教程.
