how to make a class for method to work with uiviewcontroller
ios, objective-c
Solution
I believe this is an instance where you can use an Objective-C feature called "Categories". A category is an extension to an already established class. For example, we can make a category for UIViewController which will add new methods to ALL UIViewController classes.
To do this, we do the following:
Make a .h file declaring prototypes:
@interface UIViewController (CategoryName) // EG. (Twitter)
// PROTOTYPES... EG:
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image;
@end
And the corresponding .m file that implements them:
@implementation UIViewController (CategoryName)
// IMPLEMENTATIONS... EG:
-(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image {
// Code...
}
@end
(Note that when you declare a new file in XCode, it has a "Category" template that you can use.)
Now, whenever you import your .h file, UIViewController as a class will contain the methods you extended it with!
Here's a tutorial on categories that I found with a quick google search.
Problem
I am thinking that how can I make a class for twitter or facebook or anything. So just I import that class to my project and it start working with one code. For example. If I use twitter framework then I have to write its functions. I just want to make a class of that funcation which will work on my all future projects by just importing it. example: Any class or anything: ``` -(void)shareOnTwitter:(NSString *)text withUrl:(NSURL *)url withImage:(UIImage *)image { // ALL TWITTER Code HERE; } ``` And in my Main viewController of any project: ``` - (IBAction)socialBtn:(id)sender { [self shareOnTwitter:@"This is Text" withUrl:nil withImage:nil]; } ``` UPDATE: Just tried Category way but no response return, any idea where I am wrong?: 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]; ```