Skip to content

Instantly share code, notes, and snippets.

@markrickert
Created August 21, 2012 20:22
Show Gist options
  • Save markrickert/3419059 to your computer and use it in GitHub Desktop.
Save markrickert/3419059 to your computer and use it in GitHub Desktop.
Load RubyMotion links from a UIWebView in Safari
//Here's the same thing in Objective-C
#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
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
@markrickert
Copy link
Author

The obj-c code is a subclass of UIViewController as well (can't tell because i didn't include the .h file). Honestly, i could have done it the exact same way in RubyMotion, but decided to jsut set the view as the alloc.init'd uiwebview instead of adding it as a subview.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment