您可以复制和使用的Swift便捷功能参考


*我们已经确认仅在iOS12和iOS13上可以进行操作。

警报

正常警报

1
2
3
4
5
6
7
8
9
10
let alert: UIAlertController = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle:  UIAlertController.Style.alert)

let defaultButton: UIAlertAction = UIAlertAction(title: "ボタン
", style: UIAlertAction.Style.default, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})

alert.addAction(defaultButton)

present(alert, animated: true, completion: nil)

通常アラート.png

确认警报

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let alert: UIAlertController = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle:  UIAlertController.Style.alert)

let defaultButton: UIAlertAction = UIAlertAction(title: "ボタン
", style: UIAlertAction.Style.default, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})
let cancelButton: UIAlertAction = UIAlertAction(title: "キャンセル
", style: UIAlertAction.Style.cancel, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})

alert.addAction(defaultButton)
alert.addAction(cancelButton)

present(alert, animated: true, completion: nil)

確認アラート.png

带有文本框的警报

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let alert: UIAlertController = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle:  UIAlertController.Style.alert)

let defaultButton: UIAlertAction = UIAlertAction(title: "ボタン", style: UIAlertAction.Style.default, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})
let cancelButton: UIAlertAction = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})

alert.addTextField(configurationHandler: {(text:UITextField!) -> Void in
    text.placeholder = "テキストボックス"
})

alert.addAction(defaultButton)
alert.addAction(cancelButton)

present(alert, animated: true, completion: nil)

テキストボックス付きのアラート.png

警报

从下方发出

称为操作表。
带有文本框的警报不能用于此ActionSheet。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let alert: UIAlertController = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle:  UIAlertController.Style.actionSheet)

let button1: UIAlertAction = UIAlertAction(title: "ボタン1", style: UIAlertAction.Style.default, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})
let button2: UIAlertAction = UIAlertAction(title: "ボタン2", style: UIAlertAction.Style.default, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})
let button3: UIAlertAction = UIAlertAction(title: "ボタン3", style: UIAlertAction.Style.destructive, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})
let cancelButton: UIAlertAction = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: {(action: UIAlertAction!) -> Void in
    // ボタンが押された時のコード
})

alert.addAction(button1)
alert.addAction(button2)
alert.addAction(button3)
alert.addAction(cancelButton)

present(alert, animated: true, completion: nil)

下から出てくるアラート.png

共用工作表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let activityVC = UIActivityViewController(activityItems: [
    "共有したいテキスト",
    NSURL(string: "共有したいウェブサイト"),
    UIImage(named: "共有したい画像")
], applicationActivities: nil)

// 使用しないアクティビティタイプ
activityVC.excludedActivityTypes = [
    UIActivity.ActivityType.postToFacebook,
    UIActivity.ActivityType.postToTwitter,
    UIActivity.ActivityType.message,
    UIActivity.ActivityType.saveToCameraRoll,
    UIActivity.ActivityType.print,
    UIActivity.ActivityType.airDrop,
    UIActivity.ActivityType.mail
]

self.present(activityVC, animated: true, completion: nil)

iOS 13就是这种情况。

共有シート-iOS13.png

iOS 12就是这种情况。

共有シート-iOS12.png

触摸ID认证

1
import LocalAuthentication

您需要导入

LocalAuthentication

TouchID.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let context: LAContext = LAContext()
var error: NSError?
if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
    context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "認証の理由", reply: { success, error in
        if (success) {
            // 認証成功
        } else {
            // 認証失敗
            // error!.localizedDescription で詳細なエラーを出力できる
        }
    })
} else {
    // Touch ID 非対応機種
}
1
2
3
4
5
6
7
8
9
10
11
switch context.biometryType {
case .typeFaceID: // Xcode 9.2 以上は .faceID
    description = "アカウント情報を閲覧するためにFace IDを認証として用います。"
    break
case .typeTouchID: // Xcode 9.2 以上は .touchID
    description = "アカウント情報を閲覧するためにTouch IDを認証として用います。"
    break
case .none:
    description = "アカウント情報を閲覧するためにログインしてください。"
    break
}

您也可以使用

context.biometryType来检查Touch ID或Face ID。

TouchID認証.png

用户默认值

UserDefaults.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
// 保存
UserDefaults.standard.set(true, forKey: "boolKeyName")
UserDefaults.standard.set("UserDefaults", forKey: "stringKeyName")
UserDefaults.standard.set(1, forKey: "integerKeyName")
UserDefaults.standard.set(1.0, forKey: "doubleKeyName")
UserDefaults.standard.set(1.0, forKey: "floatKeyName")

// 読み込み
UserDefaults.standard.bool(forKey: "boolKeyName")
UserDefaults.standard.string(forKey: "stringKeyName")
UserDefaults.standard.integer(forKey: "integerKeyName")
UserDefaults.standard.double(forKey: "doubleKeyName")
UserDefaults.standard.float(forKey: "floatKeyName")

参考页

警报
https://qiita.com/funafuna/items/b76e62eb82fc8d788da5
https://qiita.com/kitanoow/items/8e12c575f574b0f22016

共享屏幕
https://qiita.com/nashirox/items/56894599013d712faa0a

触摸ID
https://qiita.com/SRAUFactory/items/82ab85c166eee9e5fa0e
https://qiita.com/aokiplayer/items/a43d1b302ea7fba40970
https://qiita.com/MilanistaDev/items/b0cd432290d18f336766

UserDefaults
https://qiita.com/KokiEnomoto/items/c79c7f3793a244246fcf