我在想如何爲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