To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 9.0
AMRouter is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'AMRouter'
-
Create a class, and it's name format should be like this:
ShareComponent
,MessageComponent
.If your module function is share, then you should create a file with name
ShareComponent
.For example:
#import <Foundation/Foundation.h> @interface MessageComponent : NSObject @end
-
Create a protocol file, and define the methods that you want the class(i.e. just created) exploded, for example:
@protocol MessageComponentInterface <NSObject> @required - (BOOL)notificationEnabled; @end
Now let the class you created in step 1 conform the protocol you just created, and implement it, then it should be like this now:
// MessageComponent.h #import <Foundation/Foundation.h> #import "MessageComponentInterface.h" @interface MessageComponent : NSObject <MessageComponentInterface> @end // MessageComponent.m #import "MessageComponent.h" @implementation MessageComponent - (BOOL)notificationEnabled { return YES; }
-
Now, here comes to step 3, the very important one. Create a category file which class is
AMComponent
. And create a public class method, like this:// AMComponent+Message.h #import <AMRouter/AMRouter.h> #import "MessageComponentInterface.h" @interface AMComponent (Message) + (id<MessageComponentInterface>)message; @end // AMComponent+Message.m #import "AMComponent+Message.h" static NSString * const kTargetName = @"Message"; @implementation AMComponent (Message) + (id<MessageComponentInterface>)message { return [self targetWithName:kTargetName classPrefix:nil componentNameSuffix:nil shouldCache:YES]; } @end
-
Now create TWO private pods, in this example, it should be like these:
MessageComponentInterface
MessageComponent
-
One pod, it's name is
XxxComponentInterface
, it should contain these 3 files:XxxComponentInterface.h
(it is also a protocol file),AMComponent+Xxx.h
andAMComponent+Xxx.m
. And let this pods be dependent onAMRouter
. -
Second one is
XxxComponent
, it should containXxxComponent.h
andXxxComponent.m
and any other necessary files which is in the component. **And make sure thatXxxComponentInterface
**is in it's.podspec
dependency list, e.g.Pod::Spec.new do |s| s.name = 'MessageComponent' s.dependency 'MessageComponentInterface' ... end
-
🎉Finished! Now you can import the function in another module now, import way is just like this:
BOOL enabled = [[AMComponent message] notificationEnabled];
And the
Podfile
in that module is only need 1 pod, that is:pod 'MessageComponentInterface'
Don't forget to import both
MessageComponent
in your main project!
archmagees, archmagees.dev@gmail.com
AMRouter is available under the MIT license. See the LICENSE file for more info.