In-App Purchase: SKRequest.start bug?

Since I’m using a third party component for In-App Purchasing, I’m not sure where this bug is, but it seems that SKRequest.start crashes when there is no connectivity to the Internet in general (or to the AppStore in particular).

My solution is to check for reachability to the AppStore, because that’s what we really care about. I’m using Indy for a multi-platform solution (as opposed to iOS APIs).

I offer the following code up as a solution. Tear it apart… ;)

uses
  ..., IdComponent, IdTCPClient;

type
  TForm2 = class(TForm)
    ...
  public
    Conn : TIdTCPClient;
    procedure TCPClientStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string);
    procedure TCPClientConnected(Sender: TObject);
    procedure TCPClientDisconnected(Sender: TObject);
  end;

implementation

procedure TForm2.Button1Click(Sender: TObject);
begin
  // Attempt to connect to the AppStore
  Conn := TIdTCPClient.Create(nil);
  Conn.Host := 'itunes.apple.com';
  Conn.Port := 80;
  Conn.OnConnected := TCPClientConnected;
  Conn.OnDisconnected := TCPClientDisconnected;
  Conn.OnStatus := TCPClientStatus;

  try
    Conn.Connect;
  except
    ; // Failure - No Internet Connection
  end;
end;

procedure TForm2.TCPClientConnected(Sender: TObject);
begin
  // Disconnect gracefully
  Conn.Disconnect;
end;

procedure TForm2.TCPClientDisconnected(Sender: TObject);
begin
  // No need for this TCP client anymore
  Conn.DisposeOf;
end;

procedure TForm2.TCPClientStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string);
begin
  if Pos('Connecting to',AStatusText) > 0 then
    // We have reachability to the AppStore, so go ahead and call LoadProducts
    InAppPurchase1.LoadProducts;
end;

10 Responses to “In-App Purchase: SKRequest.start bug?”

  1. jiang Says:

    Hi,Anders, Could you share the third party component or the links of it?

  2. Anders Ohlsson Says:

    http://www.flashavconverter.com/content/app-purchase-delphi-component-ios

  3. olecramoak Says:

    hello Anders! Why 3rd party and not direct head parsing with phil hess tools? Currently, I know two apps live on Appstore developed with firemonkey that use Storekit smoothly (there even is a function for testing store reachability)…maybe a more direct approach would be more efficient.

  4. Blong Says:

    I don’t think start is the cause of the exception. You should find that request:didFailWithError: gets called if there is no connectivity.

    Granted it gets called with a useless empty error object, but it gets called nevertheless.

    The question is whether the 3rd party code catches that delegate method, and maybe whether it assumes there is something useful in the error object, checks for nil etc.

  5. Anders Ohlsson Says:

    @ole: I didn’t feel like re-inventing the wheel… ;)

    @Brian: This is actually before that. As soon as start is called with no connection it crashes hard - the exception handling doesn’t even get to it. Could still be in the 3rd party component though. No fail event whatsoever.

  6. Olecramoak Says:

    @anders: but this wheel isn’t rolling very well…still a polygon with not near-infinite sides

  7. jiang Says:

    I put the third component in a Form, then compile the project. but it’s failed: [DCC Fatal Error] F2051 Unit IOSapi.StoreKit was compiled with a different version of Macapi.ObjectiveC.TOCGenericImport`2.GetOCClass

    tears once more!!!

  8. Tom Marchione Says:

    Has anyone successfully installed the IAP component in XE5? I am having difficulty. Offhand, it appears that XE5 includes some sort of StoreKit support that was not in XE4, and requires implentation of a certain interface method called "paymentQueueUpdatedDownloads". Am I seeing things? Has anyone made this work in XE5 yet?

  9. Tom Marchione Says:

    To follow up on my last comment, if you remove the reference to StoreKit in the IAP package, and implement "paymentQueueUpdatedDownloads" and "paymentQueueUpdatedTransactions" in FMX.IAP.IOS, the component will compile and install in XE5. However, I am not familiar with the internals of StoreKit, so I am not entirely sure what the above methods should do.

  10. Tom Marchione Says:

    One more follow-up… I contacted the IAP developer (who is extremely responsive, BTW — a pleasure to deal with). He confirmed that XE5 has an incomplete implementation of StoreKit in the RTL, which is conflicting with his component. He created a workaround by renaming one of his units, which removes the conflict and allows the component’s StoreKit implementation to coexist peacefully with the incomplete references in the XE5 RTL. Problem solved. That said, I don’t know if the methods that are not implemented in his component but are required in the native XE5 interface are worth investigating someday. For now, I am mainly interested in moving my development to XE5, so that I can sunset XE4. And this seems to accomplish that.

Leave a Reply


Bad Behavior has blocked 512 access attempts in the last 7 days.

Close