简体   繁体   English

在URL方案OpenURL之后从AppDelegate调用ViewController方法

[英]Call ViewController Method from AppDelegate after URL Scheme OpenURL

I am trying to call a function LoadWebpage in ViewController.m from AppDelegate.m. 我试图从AppDelegate.m调用ViewController.m中的函数LoadWebpage

I have enabled my app with a URL Scheme such that "urlscheme://?querystring" links in Safari open my app. 我已经使用URL方案启用了我的应用程序,以便Safari中的“urlscheme://?querystring”链接打开我的应用程序。 I am capturing the url scheme in AppDelegate (I can tell by logging that this is working) and would like to fill a global variable with the querystring and then call my LoadWebPage method so that the view controller can use the global variable and open the requested website in a WebView control. 我正在AppDelegate中捕获url方案(我可以通过记录这可以告诉它这是有效的)并且想用querystring填充一个全局变量然后调用我的LoadWebPage方法,以便视图控制器可以使用全局变量并打开请求WebView控件中的网站。

I am following this tutorial . 我正在学习本教程

This is is AppDelegate.m: 这是AppDelegate.m:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query:%@", [url query]);
    URLString = (NSString *)[url query]; //extern variable

    [self.ViewController loadWebpage];
    return YES;

}

And in ViewController.m: 在ViewController.m中:

- (void)LoadWebpage{

    NSString *strURL=URLString;  //more logic will be required to get the appropriate url from the query string.
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:urlRequest];

}

But the line [self.ViewController loadWebpage]; 但行[self.ViewController loadWebpage]; has an error "Property 'ViewController' not found on object of type 'AppDelegate'". 在“AppDelegate”类型的对象上找不到“Property'ViewController'”错误。

I'm guessing I need to synthesize a reference to the open viewcontroller, but I can't find info on how to do this. 我猜我需要合成对open viewcontroller的引用,但我找不到有关如何执行此操作的信息。 Is this the best way to handle this? 这是处理这个问题的最佳方法吗?

Edit: Based on Yan's comment below, I tried to add 编辑:根据Yan的评论,我尝试添加

ViewController* mainController = (ViewController*)  self.window.rootViewController;
[mainController LoadWebpage];

Instead of [self.ViewController loadWebpage]; 而不是[self.ViewController loadWebpage]; but it just ends debugging with lldb? 但它只是用lldb结束调试?

Rather than trying to have the AppDelegate trigger something off in your View Controller, I would suggest having the View Controller register to receive a notification when the app starts. 我建议让View Controller注册以在应用启动时收到通知,而不是试图在视图控制器中触发AppDelegate。 You can then check the variable you're setting in AppDelegate and react accordingly. 然后,您可以检查您在AppDelegate中设置的变量并做出相应的反应。

For example: 例如:

YourViewController.m: YourViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];

    // This notification is sent the first time the app launches
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(applicationBecameActive:)
                                                 name: UIApplicationDidBecomeActiveNotification
                                               object: nil];

    // This notification is sent when the app resumes from the background
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(applicationEnteredForeground:)
                                                 name: UIApplicationWillEnterForegroundNotification
                                               object: nil];
}

For the ones, who have want it in Swift! 对于那些在Swift中想要它的人!

First, create NotificationCenter post method (In Swift 2.0 - NSNotification Center), where you want to send data: 首先,创建要在其中发送数据的NotificationCenter post方法(在Swift 2.0 - NSNotification Center中):

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"), object: nil)

    return true
}

In your ViewController class, where you want to receive the data, add the following in super.viewDidLoad(): 在要在其中接收数据的ViewController类中,在super.viewDidLoad()中添加以下内容:

NotificationCenter.default.addObserver(self,selector: #selector(self.YOUR_METHOD_NAME),
name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"),
object: nil)

And the method you want to call: 并且您要调用的方法:

func YOUR_METHOD_NAME(notification: NSNotification) {
    // Your method calling, or what you want to do with received data
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM