Created
August 21, 2012 20:22
-
-
Save markrickert/3419059 to your computer and use it in GitHub Desktop.
Load RubyMotion links from a UIWebView in Safari
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Does
WebView
in the ObjC example inherit fromUIViewController
?Seems like the biggest difference between the two is that the RubyMotion code is assigning a value to
self.view
, and theWebView
code is actually adding a subview.