Ios 要求代理从AppDelegate外部打开由URL标识的资源
我有一个代码,它要求代理打开一个由URL标识的资源,并希望此代码位于AppDelegate文件之外,即位于另一个文件中:Ios 要求代理从AppDelegate外部打开由URL标识的资源,ios,objective-c,Ios,Objective C,我有一个代码,它要求代理打开一个由URL标识的资源,并希望此代码位于AppDelegate文件之外,即位于另一个文件中: -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
NSLog(@"url --> %@", url);
//Do something...
return YES;
}
如果它在AppDelegate中,它可以正常工作,但如果我在另一个文件中有此方法,它将不会运行
为了给出我为什么使用这种方法的更多上下文,这里涉及到一个auth过程。因此,在我的应用程序中,Safari会打开并向用户提示一些凭据。
然后我用URL方案+一些信息启动我的应用程序,例如
myurl://someinfo
将该代码保留在AppDelegate中,而在另一个类中使用该方法
在AppDelegate.h中全局定义AppDelegate,如下所示
#define APP_DELEGATE ((AppDelegate *)[[UIApplication sharedApplication] delegate])
完成此操作后,您可以在代码中的任何位置调用AppDelegate.m的方法,如下所示:
[APP_DELEGATE methodYouWantToCall];
似乎使用NSNotificationCenter最适合您的需求 在AppDelegate中,使用NSNotificationCenter发布通知
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
NSLog(@"url --> %@", url);
// Do something...
// 1 - Post recieved URL by usig NSNotificationCenter
[[NSNotificationCenter defaultCenter] postNotificationName:@"APP_LAUNCHED_BY_URL_SCHEMA_NOTIFICATION" object:url];
return YES;
}
并在自定义类(例如:ViewController)中添加以下代码
- (void)viewDidLoad
{
[super viewDidLoad];
// 2 - Add observer to the notification name posted from AppDelegate
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidLaunchedByURLSchemaNotification:) name:@"APP_LAUNCHED_BY_URL_SCHEMA_NOTIFICATION" object:nil];
}
-(void)appDidLaunchedByURLSchemaNotification:(NSNotification *)notification
{
// 3 - Here you can get the URL Object posted with notification,you will get this Callback on every launch of your application using Your Custom URL Scheme by other applications.
NSURL *url = notification.object;
NSLog(@"url --> %@", url);
}
记住删除自定义类的-dealoc中添加的观察者
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
H2H,享受编码的乐趣:)这与Naresh的答案非常相似,因为来自
NSNotificationCenter
的所有通知都以相同的方式工作
基本上,您将为通知选择一个名称(例如:@“NOTIFICACION_name”
),然后您需要接收该事件的每个对象都将使用该名称订阅所有通知
因此,在您的应用程序代理中,您将发布(例如“广播”)一个通知,其名称为:
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
// First, create a dictionary with all the data needed to pass.
NSDictionary *notificationInfo = @{
@"url" : url,
@"sourceApplication" : sourceApplication,
@"annotation" : annotation
};
// Post the notification to all objects that listen to it.
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_NAME"
object:self
userInfo:notificationInfo];
}
现在,我们需要在另一个文件中捕获您需要它的通知。因此:
- (instancetype)init {
self = [super init];
if ( self ) {
// Subscribe to the notifications called "NOTIFICATION_NAME"
// The "object" parameter is nil so we get notification from all objects that post that particular notification name.
// It could also be [[UIApplication sharedApplication] delegate] to receive notifications only from the app delegate.
// If you process the event in a UIViewController, do this in viewDidLoad instead of init.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(openedWithURLScheme:)
name:@"NOTIFICATION_NAME"
object:nil];
}
}
- (void)dealloc {
// Unsubscribe from all notifications at dealloc time. If you don't do this,
// your app will crash when the app delegate posts the notifications
// after this particular object was deallocated.
// If it's a UIViewController, do this in viewDidUnload.
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)openedWithURLScheme:(NSNotification *)notification {
// The notification was received, so this method is called with the
// notification object as a parameter. Now we get the dictionary
// we sent with all the info.
NSDictionary *notificationInfo = notification.userInfo;
// Now get the data you need:
NSURL *url = notificationInfo[@"url"];
NSString *sourceApplication = notificationInfo[@"sourceApplication"];
id annotation = notificationInfo[@"annotation"];
// Do what you need with that info...
}
简单地说:当您的应用程序代理接收到该特定方法时,我们会将接收到的信息广播给每个正在“监听”(观察)该特定通知的对象。应使用NSDictionary
完成此操作
有关更多信息,请阅读。您想要什么还不太清楚。你能解释一下吗?当我使用URL方案打开我的应用程序时,会调用这个函数。但它只有在我的
AppDelegate.m
文件中才起作用。我想让它在同一目录下的另一个文件中工作。啊,你有几个选择。例如,可以使用自定义类定义委托对象。您是否尝试将通知与NSNotificationCenter
一起使用?我认为这是一个更好的选择。@AlejandroIván对于这个特定的应用程序委托方法没有相应的通知。当然可以创建和使用特定于应用程序的通知。@rmaddy是的,我想他可以使用NSNotificationCenter
发布通知并在其他对象中捕获它。恰恰相反。他希望应用程序代理通知其他对象。换句话说:将该方法的业务逻辑委托给另一个对象。我以为他想从AppDelegate文件外部运行该代码。这是他在第一行中所说的。@AkashKR很抱歉,这不清楚,我已经编辑了这个问题以使它更清楚:)