简体   繁体   English

自定义URL方案RubyMotion

[英]Custom URL Scheme RubyMotion

I am needing to go to a specified ViewController in my app. 我需要在我的应用程序中转到指定的ViewController。 I can get to the rootViewController just fine when entering scheme:// in Safari as I've edited my Info.plist. 我在Safari中输入scheme://时可以很好地访问rootViewController,因为我编辑了我的Info.plist。 I even came across this: 我甚至遇到过这个:

https://stackoverflow.com/a/10925872/1327809 https://stackoverflow.com/a/10925872/1327809

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
[self.viewController presentModalViewController:controller animated:YES];
[controller release];

return YES;        
}

which is exactly what I want to do with just one problem. 这正是我想要解决的一个问题。 I'm having to use RubyMotion (which I'm new to) and therefore don't even have a nib file. 我不得不使用RubyMotion(我是新手),因此甚至没有nib文件。

def application( application, handleOpenURL:url )
// desired code

end

Thank you for any possible help and let me know if you need more info. 感谢您提供任何可能的帮助,如果您需要更多信息,请告诉我们。

Updated 6/14/12 更新于6/14/12
I found out that the handOpenURL method is deprecated so now I'm using its replacement. 我发现handOpenURL方法已被弃用,所以现在我正在使用它的替代品。

I've included my code because I cannot for the life of me figure out why I'm getting the black screen. 我已经包含了我的代码,因为我不能为我的生活弄清楚为什么我会得到黑屏。 I even removed the if/else from controller.rb and just had it run the initial view controller no matter if through Safari or by directly clicking app and if through Safari, it still was black. 我甚至从controller.rb中删除了if / else,只是让它运行初始视图控制器,无论是通过Safari还是直接点击应用程序,如果通过Safari,它仍然是黑色的。 If this should be in a new question, please let me know and I will start a new question. 如果这应该是一个新问题,请告诉我,我将开始一个新问题。

Both didFinishLaunchingWithOptions and openURL have to pass through the workFlowController method which then sends us to controller.rb and runs the method viewWillAppear. didFinishLaunchingWithOptions和openURL都必须通过workFlowController方法,然后将我们发送到controller.rb并运行方法viewWillAppear。 It is there that having a URL determines which view controller runs. 有一个URL确定哪个视图控制器运行。

I think it's something in app_delegate.rb that isn't coded right. 我认为这是app_delegate.rb中没有正确编码的东西。 I don't know how the new openURL method code should be different other than what I've commented below. 我不知道新的openURL方法代码应该如何与我在下面评论的内容不同。 Is there a way to see my logs (I use log and puts within the methods in question) in my console when I run rake device? 当我运行rake设备时,有没有办法在我的控制台中查看我的日志(我使用日志并放入相关方法)? I would need it to still log when I completely exit the app and try to go back in via Safari because according to the Apple Docs, "If an application is launched as a result of another application requesting it to open a URL resource, UIApplication first sends the application a application:didFinishLaunchingWithOptions: message and then it invokes this method." 当我完全退出应用程序并尝试通过Safari返回时我还需要它才能登录,因为根据Apple Docs,“如果应用程序是由于另一个应用程序要求它打开URL资源而启动的,那么首先是UIApplication向应用程序发送一个应用程序:didFinishLaunchingWithOptions:message然后它调用此方法。“ I'd like to see if this happens in my app when the app isn't running but I open it via URL. 我想看看当应用程序没有运行时我的应用程序是否会发生这种情况,但是我是通过URL打开的。

Any feedback would be greatly appreciated. 任何反馈将不胜感激。 Again, I thank you for your help up to this point. 我再次感谢你在这一点上提供的帮助。 I will keep looking as well. 我会继续看。

app_delegate.rb app_delegate.rb

class AppDelegate
...
  def application( application, didFinishLaunchingWithOptions: launchOptions )
  ...
     @window.rootViewController = self.workFlowController
  ...
  true
  end

  def application( application, openURL: url, sourceApplication: sourceApplication, annotation: annotation )

     if sourceApplication !=nil
       #should I now be using this instead of from_open_url? 
     end
     ...
     @window.rootViewController = self.WorkFlowController

     @window.rootViewController.from_open_url = true
     ...
     true
  end

  def workFlowController

     @_workFlowController ||= begin

     wfController = Auth::Controller.alloc.init

     wfController.delegate = self

     # return wf controller
     wfController

     end
  end
  ...
end

controller.rb controller.rb

module Auth

  class Controller < BaseController
  ...
     attr_accessor :delegate, :from_open_url
  ...
     def viewWillAppear( animated )

        super

        # app loads from URL, password reset controller loads first
        if (@from_open_url)

           self.presentViewController( 
             self.urlController,
             animated: false 
           )       

        else 

           self.presentViewController( 
             self.otherController,
             animated: false
           )       

        end

     end
     ...
  end    
end

I don't know your exact app setup, but you should just be able to use MyViewController.alloc.init instead. 我不知道你确切的应用程序设置,但你应该只能使用MyViewController.alloc.init Here's a full working example. 这是一个完整的工作示例。 This loads a default image (foo.png), but if the app is opened via the URL, it presents a modal view with a different image (bar.png). 这会加载默认图像(foo.png),但如果通过URL打开应用程序,则会显示带有不同图像的模态视图(bar.png)。

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @viewController = Foo.alloc.init
    @window.rootViewController = @viewController
    @window.rootViewController.wantsFullScreenLayout = true
    @window.makeKeyAndVisible
    true
  end

  def application(application, openURL:url, sourceApplication:sourceApp, annotation:annotation)
    # Here's where you might parse the url and decide which view controller to use
    controller = Bar.alloc.init
    @viewController.presentModalViewController(controller, animated:true)
    true
  end
end

class Foo < UIViewController
  def viewDidLoad
    self.view = UIImageView.alloc.initWithImage(UIImage.imageNamed('foo.png'))
  end
end

class Bar < UIViewController
  def viewDidLoad
    self.view = UIImageView.alloc.initWithImage(UIImage.imageNamed('bar.png'))
  end
end

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

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