Skip to content

Instantly share code, notes, and snippets.

@fhisa
fhisa / CameraAuthorization.swift
Last active January 27, 2022 02:05
[Swift][iOS] request for access camera / カメラへのアクセス許可を求める
import UIKit
import AVFoundation
private let DEFAULT_TITLE = "カメラ"
private let DEFAULT_MESSAGE = "カメラの使用が許可されていません。プライバシー設定でカメラの使用を許可してください"
/// カメラ使用許可を扱う。
class CameraAuthorization {
@fhisa
fhisa / SaveToPhotoLibrary.swift
Last active February 8, 2019 22:58
[Swift][iOS] Save a file to Photo Library / ファイルを写真ライブラリに保存する
import Foundation
import Photos
func saveToPhotoLibrary(url: URL) {
PHPhotoLibrary.requestAuthorization { status in
if status == .authorized {
PHPhotoLibrary.shared().performChanges(
{
let options = PHAssetResourceCreationOptions()
options.shouldMoveFile = false
@fhisa
fhisa / UserDefaults+defaultValue.swift
Last active February 8, 2019 23:02
[Swift][iOS][MacOS] get a default value from Settings.bundle / Settings.bundle に設定したデフォルト値を取得する
extension UserDefaults {
/// get a default value from Settings.bundle, and set it to UserDefaults.
/// return the value.
static func defaultValue(forKey key: String) -> Any? {
guard let path = Bundle.main.path(forResource: "Root", ofType: "plist", inDirectory: "Settings.bundle") else {
return nil
}
guard let settings = NSDictionary(contentsOfFile: path) else {
return nil
@fhisa
fhisa / String+urlEncoded.swift
Last active February 8, 2019 23:03
[Swift] get URL encoded string / 文字列をURLエンコードする
extension String {
/// URLエンコードした文字列を返す。
/// [参考] https://dev.classmethod.jp/smartphone/iphone/urlencode-spec-and-implementation-for-swift/
var urlEncoded: String {
// 半角英数字 + "/?-._~" のキャラクタセットを定義
let charset = CharacterSet.alphanumerics.union(.init(charactersIn: "/?-._~"))
// 一度すべてのパーセントエンコードを除去(URLデコード)
let removed = removingPercentEncoding ?? self
// あらためてパーセントエンコードして返す
@fhisa
fhisa / FullContainerViewController.swift
Last active April 19, 2017 23:41
[iOS][Swift] フレームいっぱいにひとつのコンテンツビューを持つコンテナビューコントローラ
//
// FullContainerViewController.swift
// ContainerSample
//
// Created by fhisa on 2017/04/19.
// Copyright © 2017年 KuronucoKoubou. All rights reserved.
//
import UIKit
@fhisa
fhisa / RoundFrameView.swift
Last active March 23, 2016 04:42
Customized UIView for drawing rounded frame with margin
//
// RoundFrameView.swift
// Rescube
//
// Created by fhisa on 2016/03/23.
// Copyright (c) 2016 Hisakuni Fujimoto. All rights reserved.
//
import UIKit
@fhisa
fhisa / SetupJapaneseLocale.swift
Last active May 25, 2018 16:08
How to change the current locale on Playground of Xcode
import Foundation
public extension NSLocale {
class func mono_currentLocale() -> NSLocale {
return NSLocale(localeIdentifier: "ja_JP")
}
}
public func setupJapaneseLocale() {
let old_method = class_getClassMethod(NSLocale.self, "currentLocale")