Skip to content

Instantly share code, notes, and snippets.

@naoya
Created October 23, 2013 12:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoya/7117769 to your computer and use it in GitHub Desktop.
Save naoya/7117769 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(
MainViewController.new
)
@window.makeKeyAndVisible
watch_clipboard
true
end
def applicationWillEnterForeground(application)
watch_clipboard
end
def watch_clipboard
pasteboard = UIPasteboard.generalPasteboard
url = pasteboard.URL
if string = pasteboard.string
if matches = string.match(%r{(https?://.*)})
notify = NSNotification.notificationWithName(
"foundURLInPasteboard",
object:self,
userInfo:{ :url => NSURL.URLWithString(matches[1]) }
)
NSNotificationCenter.defaultCenter.postNotification(notify)
end
end
end
end
class MainViewController < UIViewController
def viewDidLoad
super
self.title = "Main"
self.edgesForExtendedLayout = UIRectEdgeNone
self.view.backgroundColor = UIColor.whiteColor
# ちゃんとするなら removeObserver もしてね
center = NSNotificationCenter.defaultCenter
center.addObserver(
self, selector:"foundURLInPasteboard:", name:"foundURLInPasteboard", object:nil
)
end
def foundURLInPasteboard(center)
url = center.userInfo[:url]
notificationView = DMRNotificationView.alloc.initWithTitle(
"URLみっけ", subTitle:url.absoluteString, targetView:self.view
)
notificationView.setDidTapHandler(
lambda do
controller = WebViewController.new
controller.url = url
self.navigationController.pushViewController(controller, animated:true)
end
)
notificationView.showAnimated(true)
end
end
# 実装超適当
class WebViewController < UIViewController
attr_accessor :url
def viewDidLoad
super
@webview = UIWebView.new.tap do |v|
v.scalesPageToFit = true
v.loadRequest(NSURLRequest.requestWithURL(url))
v.frame = view.frame
v.delegate = self
end
self.view.addSubview(@webview)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment