我有这个代码,要求代理打开由URL标识的资源,并希望将此代码放在AppDelegate文件之外,即在不同的文件中:
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
NSLog(@"url --> %@", url);
//Do something...
return YES;
}
如果它在AppDelegate中,它可以正常工作,但如果我在另一个文件中有这个方法,它就不会运行。
为了更详细地介绍我使用此方法的原因,需要进行身份验证过程。所以在我的应用程序的某些时候,Safari会打开并提示用户提供一些凭据。
然后我使用URL Scheme +一些信息启动我的应用程序,例如myurl://someinfo
答案 0 :(得分:1)
似乎使用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;
}
并在您的Custom类(例如: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);
}
请记住在自定义类的-dealloc
中删除添加的观察者-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
H2H,享受编码乐趣:)
答案 1 :(得分:0)
这与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
。
有关详细信息,请阅读official documentation at Apple website。
答案 2 :(得分:-1)
将该代码留在AppDelegate中,而是在另一个类中使用该方法。
在AppDelegate.h中定义全局AppDelegate,如下所示
#define APP_DELEGATE ((AppDelegate *)[[UIApplication sharedApplication] delegate])
执行此操作后,您可以在代码中的任何位置调用AppDelegate.m的方法,如下所示:
[APP_DELEGATE methodYouWantToCall];