PathshareSDK 2.3.3

PathshareSDK 2.3.3

许可证 NOASSERTION
发布最后发布2022年7月

Jaro Habr维护。



  • Jaro Habr、Thomas Maurer 和 Stefan Schurgast

Pathshare SDK for iOS

Pod Version Pod Platform Language Language

Pathshare 是一个实时位置共享平台。更多信息请访问Pathshare 开发者页面

要求

PathshareSDK for iOS 支持 iOS 12.x, 13.x, 14.x, 15.x 和 16.x。

安装

Cocoapods

将以下行添加到您的Podfile

pod 'PathshareSDK', '~> 2.3'

通过执行以下代码将 PathshareSDK 安装到您的项目中:

pod install

手动安装

Pathshare SDK的安装很简单,请按照以下步骤进行

  1. 将您在注册后收到的PathshareSDK.xcframework拖放到您的项目中。
  2. PathshareSDK.xcframework添加到您的目标项目的一般选项卡中的嵌入的二进制文件

基本用法

配置

为了允许访问位置服务和在后台使用位置服务,请在您的项目中添加以下配置

  1. NSLocationAlwaysUsageDescriptionNSMotionUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription键及其相应的描述添加到您的Info.plist文件中。
  2. 如果您正在针对iOS 11.构建,请转到您的项目目标 > 能力 > 后台模式并启用位置更新

初始化

为了初始化Pathshare SDK,在您的项目中创建一个名为pathshare.plist的文件并添加您的账户令牌

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>account_token</key>
    <string>your PathshareSDK account token</string>
</dict>
</plist>

接下来,将以下内容添加到您的AppDelegate类的application:didFinishLaunchingWithOptions:方法中

Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self initPathshare];
    return YES;
}

...

- (void)initPathshare
{
    NSString *pathshare = [NSBundle.mainBundle pathForResource:@"pathshare" ofType:@"plist"];
    NSDictionary *config = [[NSDictionary alloc] initWithContentsOfFile:pathshare];

    [Pathshare setAccountToken:config[@"account_token"]];
    [Pathshare setTrackingMode:PSTrackingModeSmart];
}
Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    initPathshare()
    return true
}

...

private func initPathshare() {
    let pathshare = NSBundle.mainBundle().pathForResource("Pathshare", ofType:"plist") as String!
    let config = NSDictionary(contentsOfFile: pathshare) as NSDictionary!
    Pathshare.setAccountToken(config!.valueForKey("account_token") as! String)
    Pathshare.setTrackingMode(.smart)
}

可选地,您可以指定一种跟踪模式以配置位置跟踪器的行为。以下可用的跟踪模式:

跟踪模式 描述
PSTrackingModeSmart 智能适应环境和使用情况。包括对电池级别、移动速度和运动活动的意识。
PSTrackingModeEco 静态模式,提供具有非常低精度(数公里)和单次位置间距离极大的恒定跟踪数据,确保最大电池寿命。
PSTrackingModeApproximate 静态模式,提供具有低精度(数百米)和单次位置之间距离适中的恒定跟踪数据。当低电池消耗是一个重要标准时很有用。
PSTrackingModeAccurate 静态模式,提供尽可能高的精度(几米)的恒定跟踪数据和位置间的小距离。当高精度是一个基本要求时很有用。

Save User

在创建会话之前,您需要设置一个用户名

Objective-C
[Pathshare saveUser:@"SDK User ios"
               type:UserTypeTechnician
              email:@"[email protected]"
              phone:@"+12345678901"
              image:[UIImage imageNamed:@"image"]
  completionHandler:^(NSError *error) {
          if (error) {
              // ...
          } else {
              // ...
          }
      }
 ];
Swift
Pathshare.saveUser("SDK User",
                    type: .technician,
                    email: "[email protected]",
                    phone: "+12345678901",
                    image: UIImage.init(named: "image")) { (error: NSError!) -> Void in
    if error != nil {
        // ...
    } else {
        // ...
    }
}

电子邮件地址可以是 nil

使用相同的 Pathshare.saveUser() 方法创建或更新用户。

Industry-specific不同类型的用户

用户类型 描述
技术员, 驾驶员 用于路边援助行业或类似行业
司机, 收件人 用于配送服务或类似行业
调查员, 客户 用于法律服务行业或类似行业

创建会话

使用会话初始化器来创建会话

Objective-C
Session *session = [[Session alloc] init];
session.expirationDate = expirationDate;
session.name = @"Shopping";
Swift
var session = Session()
session.expirationDate = expirationDate
session.name = "Shopping"

会话必须有一个过期日期和名称。您可以同时创建多个会话,SDK 将为您管理它们。

初始化后请确保保存会话

Objective-C
[session save:^(NSError *error) { ... }];

session.identifier // => 3fd919fe824d8e7b78e2c11c1570a6f168d2c...
[session isExpired] // => false
[session URL] // => https://pathsha.re/6d39d5
Swift
session.save { (error: NSError!) -> Void in ... }

session.identifier // => 3fd919fe824d8e7b78e2c11c1570a6f168d2c...
session.isExpired() // => false
session.URL() // => https://pathsha.re/6d39d5

过期日期

为了响应会话过期,在您的类中实现 SessionExpirationDelegate 协议

Objective-C
@interface ViewController : UIViewController <SessionExpirationDelegate>
    // ...
@end
Swift
class ViewController: UIViewController, SessionExpirationDelegate { ... }

然后在您的会话实例上设置 代理

Objective-C
Session *session = [[Session alloc] init];
session.delegate = self;
Swift
var session = Session()
session.delegate = self

最后,实现您类中的 sessionDidExpire 方法以响应过期事件

Objective-C
- (void)sessionDidExpire { ... }
Swift
func sessionDidExpire() { ... }

目标

可选地,您可以在会话中添加一个目标。具有目标标识符的会话将显示每个用户的预测到达时间 (ETA)。目标标识符用于根据目标对会话进行分组。

Objective-C
Destination *destination = [[Destination alloc] init];
destination.identifier = @"W2342";
destination.latitude = 47.378178;
destination.longitude = 8.539256;

Session *session = [[Session alloc] init];
//...
session.destination = destination;
Swift
var destination = Destination()
destination.identifier = "W2342"
destination.latitude = 47.378178
destination.longitude = 8.539256

var session = Session()
//...
session.destination = destination

加入会议

要加入您创建的会议,请在会话对象上调用join:方法

Objective-C
[session join:^(NSError *error) { ... }];

[session isUserJoined] // => true
Swift
session.join { (error: NSError!) -> Void in ... }

session.isUserJoined() // => true

这个调用将您的Pathshare用户添加到会话中,您将能够在Pathshare Professional Web界面的地图上实时看到他的位置。

邀请客户

要邀请客户加入会议,请在会话对象上调用inviteUser:方法

Objective-C
[self.session inviteUserWithName:@"Customer"
                            type:UserTypeClient
                           email:@"[email protected]"
                           phone:@"+12345678901"
               canSetDestination:YES
               completionHandler:^(NSURL *url, NSError *error) {
    if (error) {
        // ...
    } else {
        // ...
        NSLog(@"Invitation URL: %@", url.absoluteString);
    }
}];
Swift
session.inviteUser(withName: "Customer",
                    type: .client,
                    email: "[email protected]",
                    phone: "+12345678901",
                    canSetDestination: true) { (url, error) in
    if error != nil {
        // ...
    } else {
        // ...
        NSLog("Invitation URL: \(String(describing: url?.absoluteString))")
    }
}

该调用将创建一位客户用户,并返回一个邀请URL,您可以通过您首选的渠道发送给客户。然后客户将看到司机的实时位置以及白标界面中的企业标识ETA。

客户将能够在其智能手机的浏览器中完全享受实时体验

离开会话

为了停止发送用户位置并将用户从会话中移除,请调用leave:方法

Objective-C
[session leaveUser:^(NSError *error) { ... }];
Swift
session.leaveUser { (error: NSError!) -> Void in ... }

查找会话

要查找现有会话,请使用相应的会话标识器调用findSessionWithIdentifier:completionHandler:方法

Objective-C
[Pathshare findSessionWithIdentifier:@"e2e422"
                   completionHandler:^(Session *session, NSError *error) {
                       if (session) {
                           session.delegate = self;
                           self.session = session;
                       }
                   }];
Swift
Pathshare.findSessionWithIdentifier("e2e422") { (session: Session!, error: NSError!) -> Void in
    if session != nil {
        session.delegate = self
        self.session = session
    }
}