Search code examples
iphoneobjective-ciosxcodeitunes

link to a book in iBook store from an iphone app


After looking around I could not get a straight answer to the following case: can I link to a book in the iBook store from my iphone app without integrating the in-app purchases API?

As far as I know, iBooks does charge you for placing the book, so my logic says that it should be possible since otherwise you would be charged twice.

Many thanks.


Solution

  • If your app is targeting iOS 6.0 you can use the new SKStoreProductViewController to allow users to purchase iTunes, App Store, and iBooks content directly from your app without having to leave it.

    Here is how to present it from a UIViewController. You must add the StoreKit.framework to your application.

    ViewController.h

    #import <StoreKit/StoreKit.h>
    
    @interface UIViewController : UIViewController <SKStoreProductViewControllerDelegate>
    
    @end
    

    ViewController.m

    -(void)showProductPageForProductID:(NSInteger)productID {
    
        SKStoreProductViewController *sv = [[SKStoreProductViewController alloc] init];
        sv.delegate = self;
    
        NSDictionary *product = @{ SKStoreProductParameterITunesItemIdentifier: @(productID)};
        [sv loadProductWithParameters:product completionBlock:^(BOOL result, NSError *error) {
    
                if(result)
                    [self presentModalViewController:sv animated:YES];
                else {
                   //product not found, handle appropriately
                }
            }];
    
    }    
    -(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
                [viewController dismissModalViewControllerAnimated:YES];
     }
    

    If your targeting devices below iOS 6.0 you can just use this:

     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/us/book/the-casual-vacancy/id518781282?mt=11"]];
    

    Just replace that URL string with your link, and it will leave your app and enter the iBooks app displaying that product.

    If you want to target both iOS 6.0 and lower, you can just check if they have the new SKStoreProductViewController by using the following conditional

     if([SKStoreProductViewController class]) {
         //show the SKStoreProductViewController
    }
    else {
         //use UIApplication's openURL:
    }
    

    In order to get the Apple product ID for a product, you can just check the URL to the product for example:

    http://itunes.apple.com/us/book/the-casual-vacancy/id518781282?mt=11

    The product ID is 518781282. It comes after the id portion in the URL. Don't include the ? or anything after it.