Skip to content

Instantly share code, notes, and snippets.

@wprater
Created August 22, 2012 21:23
Show Gist options
  • Save wprater/3429508 to your computer and use it in GitHub Desktop.
Save wprater/3429508 to your computer and use it in GitHub Desktop.
Minimal ruby files for Cordova and RubyMotion.
class AppDelegate
def init
cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage
cookieStorage.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways
CDVURLProtocol.registerURLProtocol
super
end
def application(application, didFinishLaunchingWithOptions:launchOptions)
url = launchOptions[UIApplicationLaunchOptionsURLKey] if launchOptions
invokeString = url.absoluteString if url and url.isKindOfClass(NSURL.class)
screenBounds = UIScreen.mainScreen.bounds
@window = UIWindow.alloc.initWithFrame(screenBounds)
@window.autoresizesSubviews = true
viewBounds = UIScreen.mainScreen.applicationFrame
@viewController = MainViewController.new
@viewController.useSplashScreen = true
@viewController.wwwFolderName = "www"
@viewController.startPage = "index.html"
@viewController.invokeString = invokeString
@viewController.view.frame = viewBounds
# check whether the current orientation is supported: if it is, keep it,
# rather than forcing a rotation
forceStartupRotation = true
curDevOrientation = UIDevice.currentDevice.orientation
if UIDeviceOrientationUnknown == curDevOrientation
# UIDevice isn't firing orientation notifications yet... go look at the status bar
curDevOrientation = UIApplication.sharedApplication.statusBarOrientation
end
# Make sure curDevOrientation is valid
# Work around for the UIDeviceOrientationIsValidInterfaceOrientation macro
# since callable preprocessor macros are not supported by RubyMotion
if [UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight].any? { |x| x == curDevOrientation }
for orient in @viewController.supportedOrientations
if orient == curDevOrientation
forceStartupRotation = false
break
end
end
end
if forceStartupRotation
NSLog("supportedOrientations: %@", @viewController.supportedOrientations)
# The first item in the supportedOrientations array is the start orientation (guaranteed to be at least Portrait)
newOrient = @viewController.supportedOrientations[0]
NSLog("AppDelegate forcing status bar to: %d from: %d", newOrient, curDevOrientation);
UIApplication.sharedApplication.statusBarOrientation = newOrient
end
@window.addSubview(@viewController.view)
@window.makeKeyAndVisible
true
end
end
class MainViewController < CDVViewController
def webViewDidFinishLoad(theWebView)
# only valid if ___PROJECTNAME__-Info.plist specifies a protocol to handle
if @invokeString
# this is passed before the deviceready event is fired, so you can access it in js when you receive deviceready
NSLog("DEPRECATED: window.invokeString - use the window.handleOpenURL(url) function instead, which is always called when the app is launched through a custom scheme url.")
jsString = "var invokeString = \"#{@invokeString}\";"
theWebView.stringByEvaluatingJavaScriptFromString(jsString)
# Black base color for background matches the native apps
theWebView.backgroundColor = UIColor.blackColor
end
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment