- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if ([url.scheme isEqualToString:@"mycustomscheme"]) {
NSString *urlString = @"http://google.com/";
NSURL *urlString = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[tapView loadRequest:loadObj];
}
}
“tapView”(一个 UIWebView)在“ViewController.m”中工作正常,但是当我在“AppDelegate.m”中使用它时它就无法工作。我需要做些什么才能让这段代码在我的“AppDelegate.m”文件中运行。我会很感激一步一步的解释,因为我是一名网页设计师,而不是程序员,呵呵。 :)
我的代码试图做的是检测在 Safari 中加载的“mycustomscheme://”,当然还会启动应用程序,然后将 google.com 加载到 UIWebView 中(以证明它正在运行)。我需要以某种方式连接 tapView,以便它由 AppDelegate.m 操纵。
最佳答案
最好让 ViewController 加载自己的 Web View ,因此您应该在该 Controller 中创建一个方法来处理该操作(在 ViewController.m 中):
-(void)loadWebViewFromAppDelegate {
NSString *urlString = @"http://google.com/";
NSURL *urlString = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[tapView loadRequest:loadObj]; // I'm assuming that tapView is a property or outlet in this controller
}
在应用程序委托(delegate)中,获取对该 Controller 的引用,并调用其方法,
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if ([url.scheme isEqualToString:@"mycustomscheme"]) {
ViewController *vc = (ViewController *)self.window.rootViewController;
[vc loadWebViewFromAppDelegate];
}
}
关于ios - AppDelegate.m 需要将数据发送到单独的 ViewController 的 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20795450/