RubyMotion Cheat Sheet

Objective C -> RubyMotion
This is an active post where I update references and code snippets to quickly build app using RubyMotion. So lets start . . .

Changing background color 

Objective-C

1
2
[webView setOpaque:NO];
webView.backgroundColor = [UIColor clearColor];

Corresponding RubyMotion Code

1
2
self.view.setOpaque(false)
self.view.backgroundColor = UIColor.clearColor

**
Loading application in Webview + making url open in safari

Objective-C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#import “WebView.h”

@implementation WebView
@synthesize webView = _webView;

(void) viewDidLoad
{

[super viewDidLoad];

UIWebView *webview = [[UIWebView alloc] init];

webview.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[self.view addSubview:webview];

self.title = @”Web View”;

NSString *filePath = [[NSBundle mainBundle] pathForResource:@”WebView” ofType:@”html”];

[_webView loadHTMLString:[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil] baseURL:nil];

_webView.delegate = self;

}

(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType

{

if ( inType == UIWebViewNavigationTypeLinkClicked )

{

[[UIApplication sharedApplication] openURL:[inRequest URL]];

return NO;

}

return YES;

}

@end

Corresponding RubyMotion Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class WebViewController < UIViewController

def viewDidLoad
super

self.title = “Web View”

self.view = UIWebView.alloc.init

view.delegate = self
webviewContent = File.read(App.resources_path + “/WebView.html”) #This requires Bubble-Wrap to be installed

self.view.loadHTMLString(aboutContent, baseURL:nil)

end

#Open UIWebView delegate links in Safari.
def webView(inWeb, shouldStartLoadWithRequest:inRequest, navigationType:inType)

if inType == UIWebViewNavigationTypeLinkClicked

UIApplication.sharedApplication.openURL(inRequest.URL)

false #don’t allow the web view to load the link.

end
true #return true for local file loading.

end

end

**

RubyMotion webview shell

To load a application in webview you can use following shell I have created.
https://github.com/akshatpaul/rubymotion-webview-shell

This shell is used create ios application for responsive urls

You require rubymotion tool chain installed on your system to use this shell.

Steps to create ios application in web view

  • Step 1 (Install Dependencies)
    • Inside cloned repository execute ‘bundle install’
  • Step 2 (Add your URL)
    • Goto main_controller.rb inside method viewDidLoad and replace url with desired url.
  def viewDidLoad
self.view.loadRequest NSURLRequest.requestWithURL(NSURL.URLWithString("http://www.facebook.com"))
end
  • Step 3 (Test application)
    • execute ‘rake’ to test the application.

That’s it ! 

Enjoy. 

**

Share

Leave a Comment

Your email address will not be published. Required fields are marked *

Speaking at React Universe 2024