前言:網(wǎng)上許多一些推送的demo瞎抛,功能是實現(xiàn)了,但是很多并沒有做到却紧,隨來隨去的效果桐臊,即可以在多個項目來回切換钞艇,每次還要多次集成或者復(fù)制代碼,為了解決這樣問題豪硅,今天我采用工廠模式去實現(xiàn)這樣的功能模塊封裝。
1挺物、demo中我采用了工廠模式懒浮、協(xié)議代理、runtime運行時识藤;
2砚著、demo中我對極光和友盟采用了一個簡潔的工廠管理,來實現(xiàn)將對象抽象出來痴昧,最后采用更高的工廠來對極光和友盟工廠進行管理稽穆,通過這個工廠來快速切換極光和友盟推送;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FactoryManager *manager = [FactoryManager new];manager.delegate = self;
//? ? 需要極光推送赶撰,把此注釋打開舌镶,然后把友盟推送的代碼注釋掉,其余均不用操作idfactory = [manager getJPushFactory:launchOptions];
//? ? 友盟推送
//? ? idfactory = [manager getUMessageFactory:launchOptions];return YES;}
3豪娜、通過在最高的工廠設(shè)置代理使用runtime餐胀,可以避免導(dǎo)入極光工廠的頭文件(JPushFactory)和友盟工廠的頭文件(UMessageFactory),這樣一來瘤载,刪除他們其中否灾,都不會報錯
#import "FactoryManager.h"
/** 使用運行動態(tài)時,以免刪除JPushFactory或者UMessageFactory報錯 */
#import
@interface FactoryManager ()
@end@implementation FactoryManager
- (id)getJPushFactory:(NSDictionary *)launchOptions{
Class c_ss = NSClassFromString(@"JPushFactory");
id JPushF = [[c_ss alloc] initWithLaunchOptions:launchOptions];
[self updateVariable:JPushF];return JPushF;
}
- (id)getUMessageFactory:(NSDictionary *)launchOptions{
Class c_ss = NSClassFromString(@"UMessageFactory");
id UM = [[c_ss alloc] initWithLaunchOptions:launchOptions];
[self updateVariable:UM];return UM;
}
/** 修改變量 */
-(id)updateVariable:(id)otherClass{
unsigned int count = 0;
//獲取屬性列表
Ivar *members = class_copyIvarList([otherClass class], &count);
//遍歷屬性列表for (int i = 0 ; i < count; i++) {Ivar var = members[i];
//獲取變量名稱const char *memberName = ivar_getName(var);
//獲取變量類型const char *memberType = ivar_getTypeEncoding(var);
NSLog(@"%s----%s", memberName, memberType);
Ivar ivar = class_getInstanceVariable([otherClass class], memberName);
NSString *typeStr = [NSString stringWithCString:memberType encoding:NSUTF8StringEncoding];/
/判斷類型
if ([typeStr isEqualToString:@"@\"\""]) {
//修改值object_setIvar(otherClass, ivar, self);
}
}
return otherClass;
}