- 웹
deepLink.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
<style>
html, body {
margin: 0;
padding: 0;
}
div {
padding: 16px;
text-align: center;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
margin: 0;
padding: 0;
}
p {
text-size: 16px;
color: black;
}
a {
display: inline-block;
width: 200px;
height: 50px;
line-height: 50px;
background-color: silver;
color: black;
}
</style>
<script>
// 스키마로 앱 호출해서 3초안에 호출을 못하면 설치페이지로 이동
function openApp() {
// user-agent로 Android, IOS 구분
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Android
if(/android/i.test(userAgent)) {
// 앱 스키마 호출
window.location = "testapp://open?param1=test¶m2=1234";
var now = new Date().getTime();
setTimeout(function() {
if( Date.now() - now < 3000) {
// 구글플레이 이동 url = "https://play.google.com/store/apps/details?id=패키지명"
window.location.href = "https://play.google.com/store/apps/details?id=com.blog.deeplink";
}
}, 2000);
}
// IOS
else if(/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
// 앱 스키마 호출
window.location = "testapp://open?param1=test¶m2=1234";
var now = new Date().getTime();
setTimeout(function() {
// 앱스토어 이동 url = "https://apps.apple.com/app/id앱식별자"
window.location.href = "https://apps.apple.com/app/id362057947";
}, 3000);
}
// x
else {
alert("지원하지 않는 플랫폼 입니다.");
}
}
</script>
<title>딥링크</title>
</head>
<body>
<ul>
<li>
<div>
<p>딥링크 - 미설치시 설치페이지로 이동</p>
<a href="javascript:void(0);" onclick="openApp();">앱 호출</a>
</div>
</li>
<li>
<div>
<p>Intent URL (Android전용)</p>
<!-- intent://파라미터#Intent;scheme=스키마;package=패키지명;end -->
<a href="intent://open?param1=test¶m2=1234#Intent;scheme=testapp;package=com.blog.deeplink;end">안드로이드</a>
</div>
</li>
</ul>
</body>
</html>
|
cs |
- Android
AndroidManifest.xml
<intent-filter>
<!-- 스키마 설정 -->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="testapp" android:host="open" />
</intent-filter>
|
cs |
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView txtParam;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtParam = findViewById(R.id.txtParam);
handleDeepLink(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 앱이 실행중일때
setIntent(intent);
handleDeepLink(intent);
}
// 딥링크의 파라미터 처리
public void handleDeepLink(Intent intent) {
if(intent != null && intent.getData() != null) {
// 인텐트에서 데이터 가져와서 queryParameter 로 파라미터 가져옴
Uri data = intent.getData();
String param1 = data.getQueryParameter("param1");
String param2 = data.getQueryParameter("param2");
txtParam.setText(param1 + " , " + param2);
}
}
}
|
cs |
- IOS
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *main = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"mainVc"];
self.navigation = [[UINavigationController alloc] initWithRootViewController:main];
[self.window setRootViewController:self.navigation];
[self.window makeKeyAndVisible];
return YES;
}
// 앱이 실행중일때 답링크 실행
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
[self handleDeepLink:url];
return true;
}
// 딥링크의 파라미터 처리
- (void) handleDeepLink:(NSURL *)url {
NSString *scheme = url.scheme;
// 스키마 체크
if([scheme isEqualToString:@"testapp"]) {
// 쿼리 파라미터 가져오기
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSArray<NSURLQueryItem *> *queryItems = urlComponents.queryItems;
NSString *paramId = [self getQueryParameter:@"param1" fromItems:queryItems];
NSString *paramPw = [self getQueryParameter:@"param2" fromItems:queryItems];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 뷰컨트롤러에 데이터 전달
ViewController *main = (ViewController *)self.navigation.viewControllers.firstObject;
main.paramId = paramId;
main.paramPw = paramPw;
[main openDeepLink];
});
}
}
// 쿼리에서 파라미터 추출
- (NSString *) getQueryParameter:(NSString *)key fromItems:(NSArray<NSURLQueryItem *>*)items {
for ( NSURLQueryItem *item in items ) {
if( [item.name isEqualToString:key] ) {
return item.value;
}
}
return nil;
}
@end
|
cs |
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *lbParam;
@property NSString *paramId;
@property NSString *paramPw;
- (void) openDeepLink;
@end
|
cs |
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
// 파라미터 표시
- (void)openDeepLink {
NSString *param = [NSString stringWithFormat:@"%@ , %@", self.paramId, self.paramPw];
[self.lbParam setText:param];
}
@end
|
cs |
URL Types에 스키마 추가
결과