提供 Objective C 接口,通过 HP 的 "Link" 服务创建带水印的图片、二维码和手机友好的缩短 URL。
要手动包含 Link iOS 开发者 SDK,请按照以下步骤操作
pod install
.bash "$BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/LivePaperSDK.framework/release_strip_archs.sh"
在源文件顶部导入 LivePaperSDK.h
#import <LivePaperSDK/LivePaperSDK.h>
然后使用您的客户端 ID 和密钥创建一个新的会话。
LPSession *lpSession = [LPSession createSessionWithClientId:"your client id" secret:"your client secret"];
要缩短 URL,最简单的方法是使用 LPSession 的 createShortUrl:destination:completionHandler
方法。
NSString *name = @"My Short URL";
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
[lpSession createShortUrl:name destination:url completionHandler:^(NSURL *shortUrl, NSError *error) {
if (shortUrl) {
[self showAlert:@"Created ShortURL" message:[shortUrl absoluteString]];
} else {
[self showAlert:@"Error" message:[error description]];
}
}];
要创建 QR 码,最简单的方法是使用 LPSession 的 createQrCode:destination:completionHandler
方法。指定扫描 QR 码时应跳转到的 URL。
NSString *name = @"My QR Code";
NSURL *url = [NSURL URLWithString:@"https://www.amazon.com"];
[lpSession createQrCode:name destination:url completionHandler:^(UIImage *image, NSError *error) {
if (image) {
[self showAlert:@"Created QrCode" image:image];
} else {
[self showAlert:@"Error" message:[error description]];
}
}];
目前,Link 开发者 SDK 只返回 jpg 格式的 QR 码图像。
要水印图像,最快的方法是使用LPSession的createWatermark:destination:imageURL:completionHandler
方法。指定要水印的JPEG图像的URL,以及扫描水印图像时将到达的URL。
NSString *name = @"My Watermark";
NSURL *url = [NSURL URLWithString:@"https://www.hp.com"];
NSURL *imageURL = [NSURL URLWithString:@"https://s3-us-west-1.amazonaws.com/linkcreationstudio.com/developer/zion_600x450.jpg"];
[lpSession createWatermark:name destination:url imageURL:imageURL completionHandler:^(UIImage *watermarkedImage, NSError *error) {
if (watermarkedImage) {
[self showAlert:@"Watermarked Image" image:watermarkedImage];
} else {
[self showAlert:@"Error" message:[error description]];
}
}];
目前,链接开发者SDK只返回jpg格式的水印图像。
要水印一个通往交互式移动体验的图像,请使用LPSession的createWatermark:richPayoffData:publicURL:imageURL:completionHandler
方法。指定要水印的JPEG图像的URL,以及扫描水印图像时应显示的丰厚回报数据。
NSString *name = @"My Watermark With Rich Payoff";
NSURL *url = [NSURL URLWithString:@"https://www.hp.com"];
NSURL *imageURL = [NSURL URLWithString:@"http://static.movember.com/uploads/2014/profiles/ef4/ef48a53fb031669fe86e741164d56972-546b9b5c56e15-hero.jpg"];
NSDictionary *richPayoffData = @{
@"type" : @"content action layout",
@"version" : @"1",
@"data" : @{
@"content" : @{
@"type" : @"image",
@"label" : @"Movember!",
@"data" : @{
@"URL" : @"http://static.movember.com/uploads/2014/profiles/ef4/ef48a53fb031669fe86e741164d56972-546b9b5c56e15-hero.jpg"
}
},
@"actions" : @[
@{
@"type" : @"webpage",
@"label" : @"Donate!",
@"icon" : @{ @"id" : @"533" },
@"data" : @{ @"URL" : @"http://MOBRO.CO/oamike" }
},
@{
@"type" : @"share",
@"label" : @"Share!",
@"icon" : @{ @"id" : @"527" },
@"data" : @{ @"URL" : @"Help Mike get the prize of most donations on his team! MOBRO.CO/oamike"}
}
]
}
};
[lpSession createWatermark:name richPayoffData:richPayoffData publicURL:url imageURL:imageURL completionHandler:^(UIImage *watermarkedImage, NSError *error) {
if (watermarkedImage) {
[self showAlert:@"Watermarked Image" image:watermarkedImage];
} else {
[self showAlert:@"Error" message:[error description]];
}
}];
iOS链接开发者SDK支持对底层对象的所有CRUD操作。如果您不需要更新或删除之前创建的对象,请参阅上面的快速入门部分。
触发器代表您希望放在页面上的对象:一个短网址、二维码或水印图像。回报是目的地,可以是网页URL或交互式移动体验。链接将触发器与回报连接起来。项目代表您在其中创建触发器、回报和链接的实体。
NSString *name = @"ShortURL Example";
NSString *projectId = @"project id";
NSURL *url = [NSURL URLWithString:@"https://www.hp.com"];
LPSession *lpSession = [LPSession createSessionWithClientId:@"your client id" secret:@"your client secret"];
[LPTrigger createShortUrlWithName:name projectId:projectId session:lpSession completion:^(LPTrigger *trigger, NSError *error) {
if (trigger) {
[LPPayoff createWebPayoffWithName:name url:url projectId:projectId session:lpSession completion:^(LPPayoff *payoff, NSError *error) {
if (payoff) {
[LPLink createWithName:name triggerId:trigger.identifier payoffId:payoff.identifier projectId:projectId session:lpSession completion:^(LPLink *link, NSError *error) {
if (link) {
trigger.shortURL; // returns url of the form http://hpgo.co/abc123
}
}];
}
}];
}
}];
创建后,您需要以某种形式将链接、回报和触发器ID持久化,以便稍后访问资源。
如果要更改目的地
LPSession *lpSession = [LPSession createSessionWithClientId:@"your client id" secret:@"your client secret"];
NSString *payoffId = @"payoff id";
NSString *projectId = @"project id";
[LPPayoff get:payoffId projectId:projectId session:lpSession completion:^(LPPayoff *payoff, NSError *error) {
if (payoff) {
payoff.url = [NSURL URLWithString:@"http://shopping.hp.com"];
[payoff update:^(NSError *error) {
if (!error) {
payoff.url // returns the new URL
}
}];
}
}];
稍后,如果要删除资源,请执行以下操作(先删除链接以避免资源冲突)
NSString *projectId = @"project id";
NSString *linkId = @"Link Id";
LPSession *lpSession = [LPSession createSessionWithClientId:@"your client id" secret:@"your client secret"];
// delete link first, to avoid resource conflict
[LPLink get:linkId projectId:projectId session:lpSession completion:^(LPLink *link, NSError *error) {
if (link) {
[link delete:^(NSError *error) {
if (!error) {
// delete trigger
[LPTrigger get:link.triggerId projectId:projectId session:lpSession completion:^(LPTrigger * _Nullable trigger, NSError * _Nullable error) {
if (trigger) {
[trigger delete:^(NSError *error) {}];
}
}];
// delete payoff
[LPPayoff get:link.payoffId projectId:projectId session:lpSession completion:^(LPPayoff *payoff, NSError *error) {
if (payoff) {
[payoff delete:^(NSError *error) {}];
}
}];
}
}];
}
}];
您可以使用列表操作列出现有资源。
NSString *projectId = @"project id";
LPSession *lpSession = [LPSession createSessionWithClientId:@"your client id" secret:@"your client secret"];
[LPLink list:lpSession projectId:projectId completion:^(NSArray<LPLink *> *links, NSError *error) {
links; // returns array of LPLink objects
}];
[LPPayoff list:lpSession projectId:projectId completion:^(NSArray<LPPayoff *> *payoffs, NSError *error) {
payoffs; // returns array of LPPayoff objects
}];
[LPTrigger list:lpSession projectId:projectId completion:^(NSArray *triggers, NSError *error) {
triggers; // returns array of LPTrigger objects
}];
NSString *projectId = @"project id";
NSString *name = @"QRCode Example";
NSURL *url = [NSURL URLWithString:@"http://www.hp.com"];
LPSession *lpSession = [LPSession createSessionWithClientId:@"your client id" secret:@"your client secret"];
[LPTrigger createQrCodeWithName:name projectId:projectId session:lpSession completion:^(LPTrigger * _Nullable trigger, NSError * _Nullable error) {
if (trigger) {
[LPPayoff createWebPayoffWithName:name url:url projectId:projectId session:lpSession completion:^(LPPayoff *payoff, NSError *error) {
if (payoff) {
[LPLink createWithName:name triggerId:trigger.identifier payoffId:payoff.identifier projectId:projectId session:lpSession completion:^(LPLink *link, NSError *error) {
if (link) {
[trigger getQrCodeImageWithProgress:^(double progress) {
// Progress
} completion:^(UIImage * _Nullable image, NSError * _Nullable error) {
image; // returns QR Code image
}];
}
}];
}
}];
}
}];
NSString *projectId = @"project id";
NSString *name = @"Watermark Example";
UIImage *image = [UIImage imageNamed:@"image_to_watermark"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.95);
NSURL *url = [NSURL URLWithString:@"http://www.hp.com"];
LPSession *lpSession = [LPSession createSessionWithClientId:@"your client id" secret:@"your client secret"];
[LPTrigger createWatermarkWithName:name projectId:projectId session:lpSession completion:^(LPTrigger * _Nullable trigger, NSError * _Nullable error) {
if (trigger) {
[LPPayoff createWebPayoffWithName:name url:url projectId:projectId session:lpSession completion:^(LPPayoff *payoff, NSError *error) {
if (payoff) {
[LPLink createWithName:name triggerId:trigger.identifier payoffId:payoff.identifier projectId:projectId session:lpSession completion:^(LPLink *link, NSError *error) {
if (link) {
// Download watermark image
[trigger getWatermarkForImageData:imageData strength:10 watermarkResolution:50 imageResolution:120 adjustImageLevels:YES progress:^(double progress) {
// Progress
} completion:^(UIImage * _Nullable image, NSError * _Nullable error) {
if (image) {
image; // returns Watermark image
}
}];
}
}];
}
}];
}
}];
您还可以查看SDK中包含的LivePaperSample应用。此应用演示了如何使用LivePaper iOS SDK。在运行应用程序之前,请将凭证输入到位于LivePaperSDK/LivePaperSample/Controllers/ProjectsTableViewController.swift中的ProjectsTableViewController文件。
let LPP_CLIENT_ID = "CLIENT_ID_HERE"
let LPP_CLIENT_SECRET = "CLIENT_SECRET_HERE"