ios app唤起页面跳转
有些时候我们需要再其他地方把app唤起,并打开跳转到指定的vc上面。这里我自己写了一个vc的mgr,最主要的技术是method swizzle。原理就不详述,看代码吧。
//
// ViewControllerMgr.h
//
//
// Created by Tommy on 13-8-14.
// Copyright (c) 2013年 Tommy. All rights reserved.
// #import <Foundation/Foundation.h> @protocol ViewControllerMgrDelegate <NSObject> - (BOOL) willCreateVC:(NSURL*)url;
- (BOOL) willPresentVC:(UIViewController*)onVc currentVC:(UIViewController*) presentVC url:(NSURL*)url; //if return no, will not dispatch delay url
- (BOOL) willDispatchDelayedUrl:(NSURL*)url;
//- (BOOL) needchangeToNextVC:(UIViewController*)onVc; @optional
//if return no, will not set the param by vcmgr
//please set param by yourself in delegate, and return no
- (BOOL) willSetParamter:(UIViewController*)onVc key:(NSString*)key value:(NSString*)value; @optional - (UIViewController*) creatViewController:(NSString*)vcKey paramters:(NSDictionary*)parameters; @end #define dispatch_delayed_notification_name @"_dispatchDelayedViewControllers" @interface ViewControllerMgr : NSObject @property(weak) id<ViewControllerMgrDelegate> delegate; +(id) sharedInstance; //如果当前的vc刚好和需要显示的vc是同一个类,如果不需要再这个之上弹出,而只是修改当前vc的内容,请设置为YES,否则为NO
//默认为NO
@property (assign) BOOL enablePresentOnSameVC;
@property (strong) NSString * scheme;
//保持需要被推迟的vc 的url
@property (strong) NSMutableArray * delayedUrlArray; - (BOOL) handleUrl:(NSURL*)url; - (void) registerViewController:(NSString*)key ClassName:(NSString*)vcName;
- (void) registerViewController:(NSString*)key Class:(Class) vcClass;
- (void) registerViewController:(NSDictionary*)dic; //register vc init paramters
- (void) registerInitParameters:(NSArray*) array ClassName:(NSString*)vcName;
- (void) registerVCWithClassName:(NSString*)vcName;
- (void) registerVCInitWithCoder:(NSCoder *)aDecoder ClassName:(NSString*)vcName;
- (void) registerVCInitWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil ClassName:(NSString*)vcName; //delay
- (void) addToDelay:(NSURL*)url;
//call by
- (void) dispatchDelayedViewControllers;
- (void) addViewControllerToDispatchQueue:(UIViewController*)vc; //暂时不支持
//- (void) presentViewController:(NSString*)key;
//- (void) presentModalViewController:(NSString*)key paramters:(NSString*)paramters; - (void) presentModalViewController:(NSURL*)url; - (UIView*) topView;
- (UIViewController*) topViewController; @end
//
// ViewControllerMgr.m
//
//
// Created by Tommy on 13-8-14.
// Copyright (c) 2013年 Tommy. All rights reserved.
// #import "ViewControllerMgr.h"
#import <objc/runtime.h>
#import <objc/objc.h> //static TomStack* s_vcStack = nil;
static NSMutableDictionary* s_vcInitParametersDic = nil; #pragma mark -
#pragma mark implement BaseViewController
UIViewController * g_lastViewController = nil; #pragma mark -
#pragma mark implement ViewControllerMgr
static ViewControllerMgr* s_vcmgr = nil;
@implementation ViewControllerMgr
{
NSMutableDictionary* vcDic; BOOL dispatchOpened;
} - (id) init
{
if(self =[super init])
{
vcDic = [NSMutableDictionary new];
_enablePresentOnSameVC = NO;
dispatchOpened = NO; [self installHook];
} return self;
} +(id) sharedInstance
{ static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (s_vcmgr == nil)
{
s_vcmgr = [[self alloc] init]; //autorelease];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dispatchDelayedViewControllers) name:dispatch_delayed_notification_name object:nil];
}
}); return s_vcmgr;
} +(id) allocWithZone:(NSZone *)zone
{
@synchronized(self)
{
if (s_vcmgr == nil)
{
s_vcmgr = [super allocWithZone:zone];
return s_vcmgr;
}
}
return nil;
} - (BOOL) handleUrl:(NSURL*)url
{
NSAssert(_scheme,@"scheme is null");
NSAssert(_delegate,@"delegate is null"); @try {
if(url && _scheme && [_scheme isEqualToString:[url scheme]])
{
[[ViewControllerMgr sharedInstance] presentModalViewController:url];
return YES;
}
}
@catch (NSException *exception) {
NSLog(@"严重错误!!!!!");
} return NO;
} //register vc
-(void) registerViewController:(NSString*)key ClassName:(NSString*)vcName
{
[self registerViewController:key Class:NSClassFromString(vcName)];
}
-(void) registerViewController:(NSString*)key Class:(Class) vcClass
{
//if([vcClass isKindOfClass:[UIViewController class]])
[vcDic setObject:vcClass forKey:key];
}
- (void) registerViewController:(NSDictionary*)dic
{
for(id obj in dic)
{
[self registerViewController:obj ClassName:[dic valueForKey:obj]];
}
} //register
#pragma mark -
#pragma mark register vc init paramters
- (void) registerInitParameters:(NSArray*) array ClassName:(NSString*)vcName
{
if(!s_vcInitParametersDic)
{
s_vcInitParametersDic = [NSMutableDictionary new];
} [s_vcInitParametersDic setValue:array forKey:vcName];
} - (void) registerVCWithClassName:(NSString*)vcName
{
[self registerInitParameters:@[[NSNull null]] ClassName:vcName]; }
- (void) registerVCInitWithCoder:(NSCoder *)aDecoder ClassName:(NSString*)vcName
{
[self registerInitParameters:@[aDecoder?aDecoder:[NSNull null],[NSNull null]] ClassName:vcName];
}
- (void) registerVCInitWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil ClassName:(NSString*)vcName
{
[self registerInitParameters:@[nibNameOrNil?nibNameOrNil:[NSNull null],nibBundleOrNil?nibBundleOrNil:[NSNull null],[NSNull null]] ClassName:vcName];
} //presetn vc
- (NSDictionary*) parseURIQueryString:(NSString*)query
{
NSMutableDictionary* param = [[NSMutableDictionary alloc] initWithCapacity:2];
NSArray* array = [query componentsSeparatedByString:@"&"];
for(NSString* ss in array)
{
NSArray* key = [ss componentsSeparatedByString:@"="]; switch ([key count]) {
case 1:
[param setValue:@"" forKey:[key objectAtIndex:0]];
break;
case 2:
[param setValue:[key objectAtIndex:1] forKey:[key objectAtIndex:0]];
break;
default:
break;
}
}
return param;
}
- (UIViewController*) createViewController:(NSString*) key parameters:(NSDictionary*) paramters
{
UIViewController* vc = nil;
Class vcClass = [vcDic objectForKey:key]; if(vcClass)
{
if(_enablePresentOnSameVC && g_lastViewController && [g_lastViewController isKindOfClass:vcClass])
{
[self setParametersForVC:g_lastViewController paramters:paramters];
}
else
{
vc = [[vcClass alloc] initByVCMgr];
[self setParametersForVC:vc paramters:paramters];
} }
else
{
NSAssert(0, @"call error %@ or %@ not inhert from BaseViewController",key,key);
} return vc;
} - (void) setParametersForVC:(UIViewController*)vc paramters:(NSDictionary*) paramters
{
for (id key in paramters) { @try { if(_delegate && [_delegate respondsToSelector:@selector(willSetParamter:key:value:)])
{
if([_delegate willSetParamter:vc key:key value:[paramters valueForKey:key]])
{
[vc setValue:[paramters valueForKey:key] forKey:key];
}
} }
@catch (NSException *exception) {
NSLog(@"param invalid %@",paramters);
// NSAssert(0, @"param invalid %@",paramters);
} }
} //- (void) presentViewController:(NSString*)key
//{
// [self presentModalViewController:key paramters:nil];
//}
- (void) presentModalViewController:(NSURL*)url
{
if([_delegate willCreateVC:url ])
{
NSString* path = [[url pathComponents] lastObject];
NSString* key = path?path:[url host];
NSDictionary* parameters = [self parseURIQueryString:[url query]];
UIViewController* vc = nil; if([_delegate respondsToSelector:@selector(creatViewController:paramters:)])
{
vc = [_delegate creatViewController:key paramters:parameters];
} if(!vc)
vc = [self createViewController:key parameters:parameters]; if(vc && g_lastViewController)
{
UIViewController* onVC = g_lastViewController; if(onVC && [_delegate willPresentVC:onVC currentVC:vc url:url] && vc != onVC)
{
if(onVC.navigationController)
{
[onVC.navigationController pushViewController:vc animated:YES];
}
else
{
//[vc setValue:@(YES) forKey:@"modalPresent"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[onVC presentModalViewController:nav animated:YES];
}
} }
} } - (UIView*) topView
{
return [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
} - (UIViewController*) topViewController
{ return g_lastViewController;
} - (void) addToDelay:(NSURL*)aurl
{
if(!_delayedUrlArray)
{
_delayedUrlArray = [NSMutableArray new];
} for (NSURL* url in _delayedUrlArray) {
if([[url absoluteString] isEqualToString:[aurl absoluteString]])
{
return;
} }
dispatchOpened = NO;
[_delayedUrlArray addObject:aurl];
} - (void) dispatchDelayedViewControllers
{ dispatchOpened = YES;
[self dispatchDelayedViewController];
} - (void) dispatchDelayedViewController
{
if ([_delayedUrlArray count])
{
NSURL * url = [_delayedUrlArray objectAtIndex:0];
if([_delegate willDispatchDelayedUrl:url])
{
if ([_delayedUrlArray count] ) {
[_delayedUrlArray removeObject:url];
[self handleUrl:url];
} }
} } - (void) addViewControllerToDispatchQueue:(UIViewController*)vc
{ } #pragma mark -
#pragma mark hooked method imp //define #define Hooked_Orignal_Selector(_orgSelName) @selector(_vc_orignal_##_orgSelName)
#define Hooked_Method(_name) _hooked_##_name #define Add_Method_To_Class(_class,_selName) do{ \
Method add_method = class_getInstanceMethod([self class], @selector(_selName)); \
IMP add_imp = method_getImplementation(add_method); \
class_addMethod(_class, @selector(_selName), add_imp, method_getTypeEncoding(add_method)); \
}while(0) #define HOOK_OBJC_CLASS(_class,_orgSelName,_hookedSelName) do{ \
Method org_method = class_getInstanceMethod(_class, @selector(_orgSelName)); \
Method rep_method = class_getInstanceMethod([self class], @selector(_hookedSelName)); \
IMP org_imp = method_getImplementation(org_method); \
class_addMethod(_class, Hooked_Orignal_Selector(_orgSelName), org_imp, method_getTypeEncoding(org_method)); \
IMP rep_imp = method_getImplementation(rep_method); \
class_replaceMethod(_class, @selector(_orgSelName), rep_imp, method_getTypeEncoding(org_method)); \
}while(0) #define Set_Instance_Var(_obj,_name,_value) objc_setAssociatedObject(_obj,"_append_"#_name,_value,OBJC_ASSOCIATION_RETAIN_NONATOMIC)
#define Get_Instance_Var(_obj,_name) objc_getAssociatedObject(_obj,"_append_"#_name) #define REAL_SELF() UIViewController* realSelf = (UIViewController*)self - (void) installHook
{ @try {
HOOK_OBJC_CLASS([UIViewController class],viewWillAppear:,Hooked_Method(viewDidAppearHooked:));
HOOK_OBJC_CLASS([UIViewController class],viewDidAppear:,Hooked_Method(viewDidAppear:));
HOOK_OBJC_CLASS([UIViewController class],presentModalViewController:animated:,Hooked_Method(presentModalViewController:animated:)); Add_Method_To_Class([UIViewController class],initByVCMgr);
// Add_Method_To_Class([UIViewController class],_modalClose:);
// Add_Method_To_Class([UIViewController class],_addCloseBtn:);
// Add_Method_To_Class([UIViewController class],goBack);
// Add_Method_To_Class([UIViewController class],goHome); //class_addProperty need decalre in interface
//class_addIvar cannot support for exist class }
@catch (NSException *exception) {
NSLog(@"install hook occur exception");
}
@finally { } } //hooked method
//note
//self not viewcontrollermgr, is viewcontroller instance
// -(id) initByVCMgr
{
REAL_SELF();
NSArray * parameters = [s_vcInitParametersDic valueForKey:[NSString stringWithUTF8String:class_getName([self class])]];
NSAssert(parameters, @"%@ initByVCMgr failed :init parameter error",self); id bself = nil;
switch ([parameters count]) {
case 1:
bself = [realSelf init];
break;
case 2:
bself = [realSelf initWithCoder:[parameters objectAtIndex:0]==[NSNull null]?nil:[parameters objectAtIndex:0]];
break;
case 3:
bself = [realSelf initWithNibName:[parameters objectAtIndex:0]==[NSNull null]?nil:[parameters objectAtIndex:0] bundle:[parameters objectAtIndex:1]==[NSNull null]?nil:[parameters objectAtIndex:1]];
break;
default:
NSAssert(parameters, @"%@ initByVCMgr failed:too many paramter:%@",self,parameters);
break;
} if(bself)
{
Set_Instance_Var(self,presentByMgr, @(YES));
} return bself;
} - (void) Hooked_Method(viewWillAppear:(BOOL)animated)
{
[self performSelector:Hooked_Orignal_Selector(viewWillAppear:) withObject:@(animated)];
if(!g_lastViewController)
{
g_lastViewController = (UIViewController*)self;
[[ViewControllerMgr sharedInstance] performSelector:@selector(dispatchDelayedViewController)];
}
} - (void) Hooked_Method(viewDidAppearHooked:(BOOL)animated)
{
[self performSelector:Hooked_Orignal_Selector(viewDidAppear:) withObject:@(animated)];
UIViewController* realSelf = (UIViewController*) self;
CGRect frame = realSelf.view.frame; if(frame.origin.x == frame.origin.y && frame.origin.x == 0)
g_lastViewController = realSelf; [[ViewControllerMgr sharedInstance] performSelector:@selector(dispatchDelayedViewController)];
} - (void) Hooked_Method(presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated)
{
if([modalViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController * nav = (UINavigationController*)modalViewController; if([nav.viewControllers count])
{
Set_Instance_Var([nav topViewController],modalPresent, @(YES));
}
}else
{
Set_Instance_Var(modalViewController,modalPresent, @(YES));
} [self performSelector:Hooked_Orignal_Selector(presentModalViewController:animated:) withObject:modalViewController withObject:@(animated)];
} @end
ios app唤起页面跳转的更多相关文章
- iOS APP之间到跳转,以及热门应用,手机自带到应用跳转
应用之间的跳转 在第一个APP中,做如下操作:1.在info.plist文件中的"信息属性列表"下添加一项:"URL类型"; 2.点开"URL类型&q ...
- MUI框架开发HTML5手机APP(二)--页面跳转传值&底部选项卡切换
概 述 JRedu 在上一篇博客中,我们学习了如何使用Hbuilder创建一个APP,同时如何使用MUI搭建属于自己的第一款APP,没有学习的同学可以戳链接学习: http://www.cnblo ...
- iOS——使用StroryBoard页面跳转及传值
之前在网上搜iOS的页面跳转大多都是按回以前的那种xib的形式,但鄙人是使用storyboard的.这篇就只介绍利用storyboard进行页面跳转与传值. 新建页面 iOS的程序也是使用了MVC的思 ...
- iOS使用StroryBoard页面跳转及传值
之前在网上iOS的页面跳转大多都是按回以前的那种xib的形式,但鄙人是使用storyboard的.这篇就只介绍利用storyboard进行页面跳转与传值. 新建页面 iOS的程序也是使用了MVC的思想 ...
- 用weexplus从0到1写一个app(2)-页面跳转和文章列表及文章详情的编写
说明 结束连续几天的加班,最近的项目终于告一段落,今天抽点时间开始继续写我这篇拖了很久的<用weexplus从0到1写一个app>系列文章.写这篇文章的时候,weexplus的作者已经把w ...
- WP开发笔记——WP APP添加页面跳转动画
微软的toolkit团队为我们为我们提供了这样一套组件,叫做TransitionServices服务. 简单说一下它具备的效果: turnstile(轴旋转效果): turnstile feather ...
- iOS app url scheme跳转到淘宝商品详情页 唤醒app
最近涉及的一个业务,在app内的一个广告,点击打开webView,加载的是一个淘宝商品详情页,效果是打开该webView自动跳转至淘宝对应的页面,同时在自己的app仍然加载页面,点击评论等也同样能跳转 ...
- iOS从不同页面跳转回到指定控制器
HomeViewController *homeVC = [[HomeViewController alloc] init]; UIViewController *target = nil; for ...
- iOS之safari调试iOS app web页面
Overview 当下移动端开发过程中大量使用前段H5.js等等技术,而这些web页面的调试在Xcode控制台中不太明了,经常我们移动app运行了就是方法,但是不能显示响应的效果,这时候或许就是已经报 ...
随机推荐
- GoF——状态模式
状态模式:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. “状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂的情况.把状态的判断逻辑转移到表示不同状态的一系列类 ...
- CSS自学笔记(4):CSS样式表的使用
当浏览器读到一个样式表时,浏览器会根据这个样式表来格式化html文档,从而表现出各式各样的网页. 想要浏览器读到样式表,有三种方法: 1.外部样式表 外部样式表可以理解为.CSS文件.当多个页面使用同 ...
- Fidder 工具使用
Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发还是测试来说,都有很大 ...
- android ViewHolder 使用
android中使用ListView ExpandableListView 数据适配器adapter很多都是自己定义,自己定义数据适配器时,要重写getView.重写getView为了不让每次调 ...
- Delphi通过IE窗口句柄获取网页接口(IWebBrowser2) good
主要用到的是MSAA(Microsoft Active Accessibility) 函数:ObjectFromLResult,该函数在动态链接库 oleacc.dll 中定义. uses SHDoc ...
- 强烈推荐visual c++ 2012入门经典适合初学者入门
强烈推荐visual c++ 2012入门经典适合初学者入门 此书循序渐进,用其独特.易于理解的教程风格来介绍各个主题,无论是编程新手,还是经验丰富的编程人员,都很容易理解. 此书的目录基本覆盖了Wi ...
- Android开发实例之闹钟提醒
本实例通过TimePickerDialog时间选择对话框让用户设置闹钟.并通过AlarmManager全局定时器在指定的时间启动闹钟Activity . 程序执行效果图: 实例代码: package ...
- hadoop技术基本架构
一.Hadoop概述 hadoop由两部分组成.各自是分布式文件系统和分布式计算框架MapReduce.当中.分布式文件系统主要用于大规模数据的分布式存储.而MapReduce 则构建在分布式文件系 ...
- css Tab选项卡
css tab 选项卡据说有2中实现方式 1. target css3 2. 描点 2的 核心原理是利用描点显示问题(描点父级 overflow). <style> body,div,ul ...
- hive 学习笔记精简
创建表: drop table t create table if not exists t (t string) partitioned by (log_date string) row forma ...