AppDelegate.m需要将数据发送到单独的ViewController的WebView

时间:2013-12-27 05:43:06

标签: ios objective-c uiwebview viewcontroller appdelegate

- (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操纵。

3 个答案:

答案 0 :(得分:1)

让ViewController加载自己的Web视图会更好,所以你应该在该控制器中创建一个方法来处理该操作(在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
}

在app delegate中,获取对该控制器的引用,并调用其方法

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

     if ([url.scheme isEqualToString:@"mycustomscheme"]) {
         ViewController *vc = (ViewController *)self.window.rootViewController;
         [vc loadWebViewFromAppDelegate];
     }
}

答案 1 :(得分:0)

创建一个用于加载UIWebView的新控制器

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

     if ([url.scheme isEqualToString:@"mycustomscheme"]) {

      // here
      WebViewController* webView = [[WebViewController alloc] init];
      [self.navigationController pushViewController:webView animated:NO];

     }
}

答案 2 :(得分:0)

@daemon使用NSNotificaitonCenter

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
 {
    if ([url.scheme isEqualToString:@"mycustomscheme"]) 
    {
      NSDictionary *aDict=[NSDictionary dictionaryWithObject:@"http://google.com/" forKey:@"urlToLoad"];
      [[NSNotificationCenter defaultCenter] postNotificationName:@"LoadRequestFromAppDel" object:Nil userInfo:aDict];

     }
 }

在viewcontroller中添加观察者

 - (void)viewDidLoad
 {
   [super viewDidLoad];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoadRequestFromAppDel:) name:@"LoadRequestFromAppDel" object:Nil];

   // Do any additional setup after loading the view, typically from a nib.
 }
 -(void)LoadRequestFromAppDel:(NSNotification*)aNotif
 {
    NSString *aStrUrl=[[aNotif userInfo] objectForKey:@"urlToLoad"];
    NSURL *urlString = [NSURL URLWithString:aStrUrl];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [tapView loadRequest:loadObj];
 }