---
title: "サンプルコード"
slug: "appsafer-sample"
tags: ["android", "App Safer", "ios", "sdk"]
updated: 2026-09-17T09:28:45Z
published: 2026-09-17T09:28:45Z
canonical: "guide.ncloud-docs.com/appsafer-sample"
stale: true
---
> ## Documentation Index
> Fetch the complete documentation index at: https://guide.ncloud-docs.com/llms.txt
> Use this file to discover all available pages before exploring further.
# サンプルコード
Classic/VPC環境で利用できます。
App Saferのご利用に役立つ様々なサンプルコードを確認できます。
参考
OS別のサンプルコードは以下のような言語で提供されます。
- Android: Kotlin, Java
- iOS: Swift, Objective-C
## Android
Androidで使用できるサンプルコードを確認します。
### SDK
SDK連携方法で適用した App Saferを実行するサンプルコードは、次の通りです。
**Kotlin**
```kotlin
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.nbp.appsafer.AppSafer
import java.io.File
import java.util.*
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)
```
// prevent screenshot
AppSafer.INSTANCE.preventScreenshot(window)
// set user id
AppSafer.INSTANCE.userId = "ExampleUserId"
// get user id
val userId = AppSafer.INSTANCE.userId
// app safer start
runBlocking {
AppSafer.INSTANCE.start(applicationContext);
// get udid
val udid = AppSafer.INSTANCE.userDeviceId
}
}
```
}
```
Java
```java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.nbp.appsafer.AppSafer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// prevent screenshot
AppSafer.INSTANCE.preventScreenshot(getWindow());
// set user id
AppSafer.INSTANCE.setUserId("ExampleUserId");
// get user id
String userId = AppSafer.INSTANCE.getUserId();
// app safer start
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
AppSafer.INSTANCE.start(applicationContext);
// get udid
String udid = AppSafer.INSTANCE.getUserDeviceId();
}
});
thread.start();
}
}
```
## iOS
iOSで使用できるサンプルコードを確認します。
### 初期化
初期化するためのサンプルコードは、次の通りです。
**Swift**
```swift
import SwiftUI
struct ContentView: View { func initAppSaferExample() { // APP_SAFER_KEY generated when registering an app to the Console let result = AppSafer().initAppSafer(AppSaferDelegator.delegator, serviceCode: "ncloud", key: "")
```
NSLog("initAppSafer() result: %d", result);
if(result == SUCCESS) {
// init success
} else if(result == FAIL) {
// init fail
}
}
var body: some View {
Button("initAppSafer") {
initAppSaferExample()
}
}
```
}
class AppSaferDelegator: NSObject, AppSaferDelegate { static let delegator = AppSaferDelegator() private override init() {}
```
func appSaferDidInitFinish(_ result: Int32) {
print("appSaferDidInitFinish result \(result)");
if(result == BLOCK) {
print("Device is blocked!");
// add to exit application logic
}
}
```
}
```
Objective-C
```objc
#import
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AppSafer *appSafer = [[AppSafer alloc] init];
int res = FAIL;
if(appSafer != nil) {
// APP_SAFER_KEY generated when registering an app to the Console
res = [appSafer initAppSafer:self serviceCode:@"ncloud" key:""];
NSLog(@"initAppSafer result %d", res);
if(res == SUCCESS) {
// init success
} else if(res == FAIL) {
// init fail
}
} else {
NSLog(@"AppSafer is nil");
}
}
- (void)appSaferDidInitFinish:(NSUInteger)result msg:(NSString *)message {
NSLog(@"appSaferDidInitFinish result %d", result);
if(result == BLOCK) {
NSLog(@"Device is blocked!");
// add to exit application logic
}
}
```
### リアルタイムのセキュリティ検知
リアルタイムのセキュリティ検知を実行するサンプルコードは、次の通りです。
**Swift**
```swift
import SwiftUI
struct ContentView: View { func checkTamperingExample() { let result = AppSafer().checkTampering()
```
switch(result) {
case FAIL:
print("Failed to check tampering")
break
case SUCCESS:
print("Check Tampering Success")
break
case BLOCK:
print("Device is blocked")
break
case BEFOREINIT:
print("AppSafer is not initialized")
break
default:
break
}
}
var body: some View {
Button("checkTampering") {
checkTamperingExample()
}
}
```
}
class AppSaferDelegator: NSObject, AppSaferDelegate { static let delegator = AppSaferDelegator() private override init() {}
```
func appSaferDidCheckTamperingFinish(_ result: Int32) {
print("appSaferDidCheckTamperingFinish result: " + (
result == FAIL ? "FAIL" :
result == SAFE ? "SAFE" :
result == DETECT ? "DETECT" :
result == BLOCK ? "BLOCK": "UNKNOWN"))
if(result == BLOCK) {
print("Device is blocked!")
// add to exit application logic
}
}
func appSaferDetectedJailbreak() {
print("[DETECT] appSaferDetectedJailbreak");
}
func appSaferDetectedSimulator() {
print("[DETECT] appSaferDetectedSimulator");
}
func appSaferDetectedDebugging() {
print("[DETECT] appSaferDetectedDebugging");
}
func appSaferDetectedMemoryTampered() {
print("[DETECT] appSaferDetectedMemoryTampered");
}
```
}
```
Objective-C
```objc
@implementation ViewController
- (IBAction)checkAppSafer:(id)sender {
AppSafer *appSafer = [[AppSafer alloc] init];
if(appSafer != nil) {
int res = [appSafer checkTampering];
switch(res) {
case FAIL:
NSLog(@"Failed to check tampering");
break;
case SUCCESS:
NSLog(@"Check Tampering Success");
break;
case BLOCK:
NSLog(@"Device is blocked");
break;
case BEFOREINIT:
NSLog(@"AppSafer is not initialized");
break;
}
} else {
NSLog(@"AppSafer is nil");
}
}
- (void)appSaferDidCheckTamperingFinish:(int)result {
if(result == BLOCK) {
[self.msgbox setText:@"Device is blocked!"];
// add to exit application
}
NSLog(@"appSaferDidCheckTamperingFinish result %@",
result == FAIL ? @"FAIL" :
result == SAFE ? @"SAFE" :
result == DETECT ? @"DETECT" :
result == BLOCK ? @"BLOCK": @"UNKNOWN");
}
- (void)appSaferDetectedJailbreak {
NSLog(@"[DETECT] appSaferDetectedJailbreak");
}
- (void)appSaferDetectedSimulator {
NSLog(@"[DETECT] appSaferDetectedSimulator");
}
- (void)appSaferDetectedDebugging {
NSLog(@"[DETECT] appSaferDetectedDebugging");
}
- (void)appSaferDetectedMemoryTampered {
NSLog(@"[DETECT] appSaferDetectedMemoryTampered");
}
```