【问题标题】:Custom URL Scheme RubyMotion自定义 URL 方案 RubyMotion
【发布时间】:2012-06-15 23:44:32
【问题描述】:

我需要转到我的应用程序中指定的 ViewController。在 Safari 中输入 scheme:// 时,我可以很好地访问 rootViewController,因为我已经编辑了我的 Info.plist。我什至遇到了这个:

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;        
}

这正是我想要解决的一个问题。我不得不使用 RubyMotion(我是新手),因此甚至没有 nib 文件。

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

end

感谢您提供任何可能的帮助,如果您需要更多信息,请告诉我。

2012 年 6 月 14 日更新
我发现 handOpenURL 方法已被弃用,所以现在我正在使用它的替代方法。

我已经包含了我的代码,因为我一生都无法弄清楚为什么会出现黑屏。我什至从 controller.rb 中删除了 if/else 并让它运行初始视图控制器,无论是通过 Safari 还是直接单击应用程序,如果通过 Safari,它仍然是黑色的。如果这应该是一个新问题,请告诉我,我将开始一个新问题。

didFinishLaunchingWithOptions 和 openURL 都必须通过 workFlowController 方法,然后该方法将我们发送到 controller.rb 并运行 viewWillAppear 方法。在那里,有一个 URL 决定了哪个视图控制器运行。

我认为 app_delegate.rb 中的某些内容编码不正确。除了我在下面评论的内容之外,我不知道新的 openURL 方法代码应该有何不同。当我运行 rake 设备时,有没有办法在我的控制台中查看我的日志(我使用日志并放入有问题的方法中)?当我完全退出应用程序并尝试通过 Safari 返回时,我仍需要它记录,因为根据 Apple Docs,“如果由于另一个应用程序请求它打开 URL 资源而启动应用程序,则 UIApplication 首先向应用程序发送 application:didFinishLaunchingWithOptions: 消息,然后调用此方法。”我想看看当应用程序没有运行但我通过 URL 打开它时,这是否发生在我的应用程序中。

任何反馈将不胜感激。再次感谢您在这一点上的帮助。我也会继续寻找。

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

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

【问题讨论】:

    标签: ios url uiviewcontroller url-scheme rubymotion


    【解决方案1】:

    我不知道您的确切应用设置,但您应该可以改用 MyViewController.alloc.init。这是一个完整的工作示例。这会加载默认图像 (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
    

    【讨论】:

    • 我已经更新了我的问题。如果您能看一下,我将不胜感激,否则我将作为一个全新的问题重新发布。我在网上找到与 RubyMotion 相关的材料非常难过。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2011-04-30
    • 2014-10-22
    • 2011-12-14
    • 2016-05-04
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多