Showing posts with label Snippet. Show all posts
Showing posts with label Snippet. Show all posts

Tuesday, January 26, 2016

Swift: Request Authorization to Dietary Potassium Data

Swift

@IBOutlet weak var aButton: UIButton!

@IBAction func buttonTapped(sender : AnyObject){

let healthKitStore:HKHealthStore = HKHealthStore()

let healthKitTypesToWrite = Set(
arrayLiteral: HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryPotassium)! )

healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes:nil){ (success, error) -> Void in
if success == false {
}
}

}

Swift: Display an alert

Swift
@IBOutlet weak var aButton: UIButton!

@IBAction func buttonTapped(sender : AnyObject){

let alertController = UIAlertController(title: "My alert", message:

"This is an alert!", preferredStyle: UIAlertControllerStyle.Alert)

alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

self.presentViewController(alertController, animated: true, completion: nil)
}

Thursday, July 16, 2015

PHP: Snippet: 任意のURLパラメータの値を取得


// Urlのパラメータの値を見つけて取得。?page_id%3D100&param2%3D200となっている場合、$pageIDは100になる。
   if (preg_match('/page_id%3D([0-9]+)/', $line, $match))
   {
      $pageID = $match[1];
   }

PHP: Snippet: 1行内の任意の標識文字に対する値の取得

// Label: の後に書いてある文字列を取得する
if (mb_strpos($line, “Label:”) !== false)
{
   $str = mb_substr($line, mb_strpos($line, “Label:”) + strlen(“Label:“));
}

PHP: Snippet: XML Parsing Example

// XMLを取得
$myXMLData = file_get_contents($xmlURL . $xmlURLParameter);

// XMLをパース
$xml = simplexml_load_string($myXMLData) or die (“Error: Cannot create object”);

// タイトルを取得
$title = $xml->entry->title;

// 概要を取得
$summary = $xml->entry->summary;

// 概要を行で分解
$lines = explode(PHP_EOL, $summary);

PHP: function: isToday_new

// 今日か?
function isToday_new($y, $m, $d)
{  
   if (date('Y-m-d') == date ('Y-m-d', strtotime($y . "-" . $m . "-" . $d)))
   {
      $retval = true;
   }
   
   return $retval;
}

PHP: function: monthENtoJP

// 月を英語から数値へ
function monthENtoJP($en)
{
   $monthENtoNum = array(
      "Jan" => 1 , 
      "Feb" => 2 , 
      "Mar" => 3, 
      "Apr" => 4, 
      "May" => 5, 
      "Jun" => 6, 
      "Jul" => 7, 
      "Aug" => 8, 
      "Sep" => 9, 
      "Oct"=> 10, 
      "Nov" => 11, 
      "Dec" => 12);

   return $monthENtoNum[$en];
}

PHP: function: weekdayENtoJP

// 曜日を英語から日本語へ
function weekdayENtoJP($en)
{
   $weekdayENtoJP = array(
      "Sun" => "日",
      "Mon" => "月",
      "Tue" => "火",
      "Wed" => "水",
      "Thu" => "木",
      "Fri" => "金",
      "Sat" => "土");

   return $weekdayENtoJP[$en];
}