TGPassportKit — iOS & macOS 的 Telegram Passport SDK
TGPassportKit 帮助您轻松地将 Telegram Passport 请求集成到您的 iOS & macOS 应用中。
安装
使用 Cocoapods 安装
要通过 Cocoapods 安装 TGPassportKit,请将以下内容添加到 Podfile
target 'MyApp' do
pod 'TGPassportKit'
end
然后在项目根目录中运行 pod install
使用 Carthage 安装
将以下行添加到 Cartfile
github "telegrammessenger/TGPassportKit"
然后运行 carthage update
,您将在 Carthage 文件夹中获得 TGPassportKit 的最新版本。
项目设置
配置您的Info.plist
通过在项目导航器中右键点击Info.plist,选择“以源代码打开”并添加以下代码片段进行Info.plist的配置
将{bot_id}
替换为您自己的值
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>tgbot{bot_id}</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tg</string>
</array>
连接AppDelegate方法
将以下代码添加到您的UIApplicationDelegate
实现中
#import <TGPassportKit/TGPAppDelegate.h>
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
BOOL handledByPassportKit = [[TGPAppDelegate sharedDelegate] application:application
openURL:url
options:options];
return YES;
}
如果您的应用支持iOS 9及以下版本,请也添加此方法
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(nullable NSString *)sourceApplication
annotation:(id)annotation {
BOOL handledByPassportKit = [[TGPAppDelegate sharedDelegate] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
return YES;
}
用法
添加Telegram Passport按钮
为了添加Telegram Passport按钮,请将以下代码添加到您的视图控制器中
将{bot_id}
、{bot_public_key}
和{request_nonce}
替换为您自己的值
#import <TGPassportKit/TGPButton.h>
@interface ViewController <TGPButtonDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
TGPButton *button = [[TGPButton alloc] init];
button.botConfig = [[TGPBotConfig alloc] initWithBotId:{bot_id}
publicKey:@"{bot_public_key}"];
button.scope = [[TGPScope alloc] initWithJSONString:@"{\"data\":[\"id_document\",\"address_document\",\"phone_number\"],\"v\":1}"];
// You can also construct a scope using provided data type classes like this:
// button.scope = [[TGPScope alloc] initWithTypes:@[[[TGPPersonalDetails alloc] init], [[TGPIdentityDocument alloc] initWithType:TGPIdentityDocumentTypePassport selfie:true translation:true]]];
button.nonce = @"{request_nonce}";
button.delegate = self;
[self.view addSubview:button];
}
- (void)passportButton:(TGPButton *)passportButton
didCompleteWithResult:(TGPRequestResult)result
error:(NSError *)error {
switch (result) {
case TGPRequestResultSucceed:
NSLog(@"Succeed");
break;
case TGPRequestResultCancelled:
NSLog(@"Cancelled");
break;
default:
NSLog(@"Failed");
break;
}
}
@end
或者实现自定义行为
如果您想要设计自定义UI和行为,可以通过以下方式发起Passport请求
将{bot_id}
、{bot_public_key}
和{request_nonce}
替换为您自己的值
#import <TGPassportKit/TGPRequest.h>
- (void)performPassportRequest {
TGPBotConfig *botConfig = [[TGPBotConfig alloc] initWithBotId:{bot_id}
publicKey:@"{bot_public_key}"];
TGPRequest *request = [[TGPRequest alloc] initWithBotConfig:self.botConfig];
[request performWithScope:[[TGPScope alloc] initWithJSONString:@"{\"data\":[\"id_document\",\"phone_number\"],\"v\":1}"]
nonce:@"{request_nonce}"
completionHandler:^(TGPRequestResult result, NSError * _Nullable error) {
switch (result) {
case TGPRequestResultSucceed:
NSLog(@"Succeed");
break;
case TGPRequestResultCancelled:
NSLog(@"Cancelled");
break;
default:
NSLog(@"Failed");
break;
}
}];
}
授权
TGPassportKit采用MIT许可证。有关更多信息,请参阅LICENSE文件。