Skip to content

Instantly share code, notes, and snippets.

View stevenchan's full-sized avatar

Steven Chan stevenchan

View GitHub Profile
@stevenchan
stevenchan / UIPopoverPresentationControllerDelegate.m
Last active August 29, 2015 14:27
Detecting if a view controller is presented in a popover with UITraitCollection
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popover
{
if (!self.adaptivePresentationStyleAsked) {
popover.overrideTraitCollection = [popover.presentingViewController.traitCollection copy];
}
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)popover
traitCollection:(UITraitCollection *)traitCollection
{
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event){
// make sure the message is sent from your minipush server
if(event.origin !== "http://127.0.0.1:8000"){ return; }
// when connection is established / disconnected
// you will be notified with the following messages
if(event.data = "connected"){ return; }
<?php
if(!isset($_POST['message'])){ exit(); }
$host = "127.0.0.1:8001";
$channel = "chat";
$message = '"'.$_POST['message'].'"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://$host/channels/$channel");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="form.js"></script>
<script type="text/javascript">
function receiveMessage(event){
if(event.origin !== "http://127.0.0.1:8000"){ return; }
if(event.data === "connected"){ return; }
if(event.data === "disconnected"){ return; }
<?php
if(!isset($_POST['message'])){ exit(); }
$host = "127.0.0.1:8001";
$channel = "chat";
$message = '"'.$_POST['message'].'"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://$host/channels/$channel");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$("#msg_form").ajaxForm(function(){
//clear the message input field after sending a message
$("#msg_form input[name='message']").attr("value", "");
//focus to the message input field again
$("#msg_form input[name='message']").focus();
});
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="form.js"></script>
<script type="text/javascript">
function receiveMessage(event){
if(event.origin !== "http://127.0.0.1:8000"){ return; }
if(event.data === "connected"){ return; }
if(event.data === "disconnected"){ return; }
$("#msg_textarea").get(0).value += event.data + "\n";
//scroll to the bottom
<iframe style="position:absolute;top:-500px;left:-500px;" src="http://127.0.0.1:8000/iframe?channel=chat"></iframe>
<html>
<head>
<title>Chatroom</title>
</head>
<body>
<textarea id="msg_textarea"
rows="10"
cols="10"
readonly="readyonly"
style="width:500px;height:400px;"></textarea>