SKStoreProductViewController中的坑

作者:代培
地址:http://daipei.me/posts/problem_in_skstoreproductviewcontroller/
转载请注明出处
我的博客搬家了,新博客地址:daipei.me

前言

最近在使用SKStoreProductViewController时遇到很多坑,上网搜索时发现关于这个东西的讨论不多,stack overflow上也并没有找到我需要的答案,经过与其3天的抗争,总算是爬出了这些坑,赶紧写篇博客冷静一下。

正文

首先我们先从SKStoreProductViewController说起,这是一个应用内的应用商店,就是说在应用内部无须跳转到应用商店就可以预览并下载应用,和在应用商店内部并无差异,体验是相当不错,但是如果没有找到正确的使用姿势,那么会让你苦不堪言。

简单使用

先看看这个东西该怎么使用,废话不多说,直接上代码。


- (void)viewDidLoad {
    [super viewDidLoad];

    //首先实例化一个VC
    SKStoreProductViewController *storeVC = [[SKStoreProductViewController alloc] init];
    //然后设置代理,注意这很重要,不如弹出就没法dismiss了
    storeVC.delegate = self;
    //接着弹出VC
    [self presentViewController:storeVC animated:YES completion:nil];
    //最后加载应用数据
    [storeVC2 loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:@(1173412177)} completionBlock:^(BOOL result, NSError * _Nullable error) {
            if (error) {
                //handle the error
            }
    }];
}


- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    //在代理方法里dismiss这个VC
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

简单的几行代码,就可以实现,但是肯定有人会问为何要弹出VC后再加载数据,那样不会比较慢吗?提前加载体验不是会更好吗?的确是这样,我也是为了这个好的体验,跳进了一个深坑,苹果总是为我们考虑很多。

预加载

在我做了预加载之后,遇到了两个问题:

  • 在弹出StoreVC时没有反应,并且应用会卡住几秒
  • 弹出一个连取消都没有的空白VC

在排除了我自身的bug后我确定这是SKStoreProductViewController的问题,上面的两个问题都是由于预加载出问题时导致的。
注意我这里的用词:预加载出问题而不是出错,也就是说加载的completion的block还没调用时,你还不知道这个时候加载出错时只是它内部加载有问题时,这时你present这个VC时就会导致应用卡顿。
对于弹出一个取消按钮都没有的空白VC的问题也是因为加载没有完成,导致取消没有显示出来,当然这个不是必现的,有时还会遇到一种情况就是开始弹出一个没有取消按钮的VC,过一会又加载出来了。

所以如果你一定要做预加载的话,唯一的解决方案就是当收到成功的回掉以后才能弹出这个VC,否则放弃这个VC,重新实例化一个StoreVC然后弹出并加载(不要忘记设置代理),因为这样是一定不会出问题的。
为什么可以这么肯定呢?因为如果呢实例化一个SKStoreProductViewController,不做任何加载工作此时弹出并不会出任何一个问题,只是这是一个空白的VC,但是取消按钮一定是存在的,就是说最多让别人觉得这网速很慢,加载不出来,而不会觉得这是一个bug。

我想补充的是即使这样做仍然没法避免弹出空白的VC(无取消按钮),所以最好的方案就是不做预加载,从表现来看苹果肯定是希望我们不做预加载的,至于loadProduct方法该在何时调用,在弹出VC前后调用都可以,记住别过早的调用就好。

不要两次弹出同一个VC

还有一个很奇怪的现象,就是一个做过加载操作的SKStoreProductViewController,在弹出以后,这个SKStoreProductViewController就被废弃了,什么意思呢?就是说你不能再次弹出这个VC,不然也会出现上面的bug,就是一个没有取消按钮的VC,即使你在弹出前再次调用此VC的load方法,结果都是一样,他不会理你的,就是不给你弹两次,这都是一次性的VC,这显然是苹果故意为之,只是不明白这种设计的背后是出于怎样的考虑。

总结

总之,对于SKStoreProductViewController来说,记住尽量不要去做预加载,因为在网速不错的情况下弹出后再加载体验也很不错,加载速度是很快的,这时根本不需要做什么预加载,而对于网速不好的用户,即使做了预加载也可能加载不出来,也不需要做预加载。所以能不做预加载就尽量别做,这里的预加载只会让你无比抓狂!如果呢一定要做预加载,那么记住一点很重要那就是:只有收到成功回掉SKStoreProductViewController才能弹出,否则乖乖的弹出后加载,这样才能避免一些不可描述的bug。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Title: iOS 8 App Development Essentials Author: Neil Smyth Length: 824 pages Edition: 1 Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2014-12-16 ISBN-10: 1505586410 ISBN-13: 9781505586411 iOS 8 App Development Essentials is the latest edition of this popular book series and has now been fully updated for the Swift programming language, the iOS 8 SDK and Xcode 6. Beginning with the basics, this book provides an outline of the steps necessary to set up an iOS development environment. An introduction to the architecture of iOS 8 and programming in Swift is provided, followed by an in-depth look at the design of iOS applications and user interfaces. More advanced topics such as file handling, database management, in-app purchases, graphics drawing and animation are also covered, as are touch screen handling, gesture recognition, multitasking, iAds integration, location management, local notifications, camera access and video and audio playback support. Other features are also covered including Auto Layout, Twitter and Facebook integration, App Store hosted in-app purchase content, collection views, Sprite Kit-based game development, local map search and user interface animation using UIKit dynamics. The key new features of the iOS 8 SDK and Xcode 6 are also covered, including Swift playgrounds, universal user interface design using size classes, app extensions, Interface Builder Live Views, embedded frameworks, CloudKit data storage and TouchID authentication. The aim of this book is to teach the range of skills necessary to build apps for iOS 8. iOS 8 App Development Essentials takes a modular approach to the subject of iOS 8 application development for both the iPhone and iPad, with each chapter covering a self contained topic area consisting of detailed explanations, examples and step-by-step tutorials. This makes the book both an easy to follow learning aid and an excellent reference resource. Table of Contents Chapter 1. Start Here Chapter 2. Joining the Apple iOS Developer Program Chapter 3. Installing Xcode 6 and the iOS 8 SDK Chapter 4. A Guided Tour of Xcode 6 Chapter 5. Testing Apps on iOS 8 Devices with Xcode 6 Chapter 6. An Introduction to Swift Playgrounds Chapter 7. Swift Data Types, Constants and Variables Chapter 8. Swift Operators and Expressions Chapter 9. Swift Flow Control Chapter 10. The Swift Switch Statement Chapter 11. An Overview of Swift Functions and Closures Chapter 12. The Basics of Object Oriented Programming in Swift Chapter 13. An Introduction to Swift Inheritance Chapter 14. Working with Array and Dictionary Collections in Swift Chapter 15. The iOS 8 Application and Development Architecture Chapter 16. Creating an Interactive iOS 8 App Chapter 17. Understanding iOS 8 Views, Windows and the View Hierarchy Chapter 18. An Introduction to Auto Layout in iOS 8 Chapter 19. Working with iOS 8 Auto Layout Constraints in Interface Builder Chapter 20. An iOS 8 Auto Layout Example Chapter 21. Implementing iOS 8 Auto Layout Constraints in Code Chapter 22. Implementing Cross-Hierarchy Auto Layout Constraints in iOS 8 Chapter 23. Understanding the iOS 8 Auto Layout Visual Format Language Chapter 24. Using Size Classes to Design Universal iOS User Interfaces Chapter 25. Using Storyboards in Xcode 6 Chapter 26. Using Xcode 6 Storyboards to Create an iOS 8 Tab Bar Application Chapter 27. An Overview of iOS 8 Table Views and Xcode 6 Storyboards Chapter 28. Using Xcode 6 Storyboards to Build Dynamic TableViews with Prototype Table View Cells Chapter 29. Implementing iOS 8 TableView Navigation using Storyboards in Xcode 6 Chapter 30. An iOS 8 Split View Master-Detail Example Chapter 31. Implementing a Page based iOS 8 Application using UIPageViewController Chapter 32. An Example iOS 8 UIPageViewController Application Chapter 33. Working with Directories in Swift on iOS 8 Chapter 34. Working with Files in Swift on iOS 8 Chapter 35. iOS 8 Directory Handling and File I/O in Swift – A Worked Example Chapter 36. Preparing an iOS 8 App to use iCloud Storage Chapter 37. Managing Files using the iOS 8 UIDocument Class Chapter 38. Using iCloud Storage in an iOS 8 Application Chapter 39. Synchronizing iOS 8 Key-Value Data using iCloud Chapter 40. iOS 8 Data Persistence using Archiving Chapter 41. iOS 8 Database Implementation using SQLite Chapter 42. An Example SQLite based iOS 8 Application using Swift and FMDB Chapter 43. Working with iOS 8 Databases using Core Data Chapter 44. An iOS 8 Core Data Tutorial Chapter 45. An Introduction to CloudKit Data Storage on iOS 8 Chapter 46. An iOS 8 CloudKit Example Chapter 47. An iOS 8 CloudKit Subscription Example Chapter 48. An Overview of iOS 8 Multitouch, Taps and Gestures Chapter 49. An Example iOS 8 Touch, Multitouch and Tap Application Chapter 50. Detecting iOS 8 Touch Screen Gesture Motions Chapter 51. Identifying Gestures using iOS 8 Gesture Recognizers Chapter 52. An iOS 8 Gesture Recognition Tutorial Chapter 53. Implementing TouchID Authentication in iOS 8 Apps Chapter 54. An Overview of iOS 8 Collection View and Flow Layout Chapter 55. An iOS 8 Storyboard-based Collection View Tutorial Chapter 56. Subclassing and Extending the iOS 8 Collection View Flow Layout Chapter 57. Drawing iOS 8 2D Graphics with Core Graphics Chapter 58. Interface Builder Live Views and iOS 8 Embedded Frameworks Chapter 59. An iOS 8 Graphics Tutorial using Core Graphics and Core Image Chapter 60. Basic iOS 8 Animation using Core Animation Chapter 61. iOS 8 UIKit Dynamics – An Overview Chapter 62. An iOS 8 UIKit Dynamics Tutorial Chapter 63. An Introduction to iOS 8 Sprite Kit Programming Chapter 64. An iOS 8 Sprite Kit Level Editor Game Tutorial Chapter 65. An iOS 8 Sprite Kit Collision Handling Tutorial Chapter 66. An iOS 8 Sprite Kit Particle Emitter Tutorial Chapter 67. Integrating iAds into an iOS 8 App Chapter 68. iOS 8 Multitasking, Background Transfer Service and Fetching Chapter 69. Scheduling iOS 8 Local Notifications Chapter 70. An Overview of iOS 8 Application State Preservation and Restoration Chapter 71. An iOS 8 State Preservation and Restoration Tutorial Chapter 72. Integrating Maps into iOS 8 Applications using MKMapItem Chapter 73. An Example iOS 8 MKMapItem Application Chapter 74. Getting Location Information using the iOS 8 Core Location Framework Chapter 75. An Example iOS 8 Location Application Chapter 76. Working with Maps on iOS 8 with MapKit and the MKMapView Class Chapter 77. Working with MapKit Local Search in iOS 8 Chapter 78. Using MKDirections to get iOS 8 Map Directions and Routes Chapter 79. An Introduction to Extensions in iOS 8 Chapter 80. An iOS 8 Today Extension Widget Tutorial Chapter 81. Creating an iOS 8 Photo Editing Extension Chapter 82. Creating an iOS 8 Action Extension Chapter 83. Receiving Data from an iOS 8 Action Extension Chapter 84. Using iOS 8 Event Kit to Create Date and Location Based Reminders Chapter 85. Accessing the iOS 8 Camera and Photo Library Chapter 86. An Example iOS 8 Camera Application Chapter 87. iOS 8 Video Playback using AVPlayer and AVPlayerViewController Chapter 88. Playing Audio on iOS 8 using AVAudioPlayer Chapter 89. Recording Audio on iOS 8 with AVAudioRecorder Chapter 90. Integrating Twitter and Facebook into iOS 8 Applications Chapter 91. An iOS 8 Facebook Integration Tutorial using UIActivityViewController Chapter 92. iOS 8 Facebook and Twitter Integration using SLRequest Chapter 93. An iOS 8 Twitter Integration Tutorial using SLRequest Chapter 94. Making Store Purchases with the SKStoreProductViewController Class Chapter 95. Building In-App Purchasing into iOS 8 Applications Chapter 96. Preparing an iOS 8 Application for In-App Purchases Chapter 97. An iOS 8 In-App Purchase Tutorial Chapter 98. Configuring and Creating App Store Hosted Content for iOS 8 In-App Purchases Chapter 99. Preparing and Submitting an iOS 8 Application to the App Store

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太肥小次郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值