蘋(píng)果的StoreKit庫(kù)提供了一些方便的API給我們調(diào)用裸准,但很多時(shí)候我們都沒(méi)有注意酪刀。這里簡(jiǎn)單的記錄一下應(yīng)用內(nèi)跳轉(zhuǎn)App Store和應(yīng)用內(nèi)評(píng)分边琉。
一贡这、 導(dǎo)入系統(tǒng)StoreKit的頭文件
import StoreKit
二倘屹、 應(yīng)用內(nèi)評(píng)分
應(yīng)用內(nèi)評(píng)分主要使用SKStoreReviewController银亲,只需要一個(gè)方法,UI也是非常的簡(jiǎn)潔美觀纽匙。
1.兩個(gè)需要注意點(diǎn):
1.#available(iOS 10.3, *) 也就是iOS 10.3之后支持
2. a.測(cè)試的時(shí)候只有用到TestFlight測(cè)試务蝠,“提交”按鈕才能點(diǎn)擊。 b.Apple 限制開(kāi)發(fā)者在一年最多只能向用戶(hù)調(diào)用三次評(píng)分UI烛缔。所以就會(huì)出現(xiàn)點(diǎn)擊了評(píng)分按鈕但是不彈窗的情況
- 具體使用很簡(jiǎn)單馏段,就一句代碼(是不是很驚喜??)
SKStoreReviewController.requestReview()
- 跟一般的方式一起使用
func lg_iTunesScoreComment(appId: String) {
if #available(iOS 10.3 , *) {
SKStoreReviewController.requestReview()
} else {
let openStr = "itms-apps://itunes.apple.com/app/id\(appId)?action=write-review"
if UIApplication.shared.canOpenURL(URL(string: openStr)) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: openStr), options: [], completionHandler: nil)
} else {
UIApplication.shared.canOpenURL(URL(string: openStr))
}
} else {
print("無(wú)法打開(kāi)鏈接")
}
}
}
三、 應(yīng)用內(nèi)跳轉(zhuǎn)App Store
使用應(yīng)用內(nèi)跳轉(zhuǎn)主要是 present SKStoreProductViewController
- 創(chuàng)建和設(shè)置 Parameters
import UIKit
import StoreKit
class LGStoreProduct: NSObject {
static let share = LGStoreProduct()
private override init() { super.init()}
private var parentVc:UIViewController?
func lg_openStore(currentVc: UIViewController, appID: String) {
parentVc = currentVc
currentVc.present(self.storeVc, animated: true, completion: nil)
storeVc.loadProduct(withParameters: [SKStoreProductParameterITunesItemIdentifier: appID], completionBlock: {
(result, error) in
if result && error == nil {
print("鏈接加載成功<伞T合病!")
} else {
print(error as Any)
}
})
}
lazy var storeVc: SKStoreProductViewController = {
let storeVc = SKStoreProductViewController()
storeVc.delegate = self
return storeVc
}()
}
- SKStoreProductViewControllerDelegate方法晕翠,設(shè)置dissmiss回調(diào)
extension LGStoreProduct: SKStoreProductViewControllerDelegate {
// Sent if the user requests that the page be dismissed
func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
parentVc?.dismiss(animated: true, completion: nil)
}
}
- Parameters的可選值有這些喷舀,有興趣的朋友自行研究
// iTunes Store item identifier (NSNumber) of the product
@available(iOS 6.0, *)
public let SKStoreProductParameterITunesItemIdentifier: String
// SKU for the In-App Purchase product (NSString) to render at the top of the product page
@available(iOS 11.0, *)
public let SKStoreProductParameterProductIdentifier: String
// iTunes Store affiliate token (NSString)
@available(iOS 8.0, *)
public let SKStoreProductParameterAffiliateToken: String
// iTunes Store affiliate campaign token (NSString)
@available(iOS 8.0, *)
public let SKStoreProductParameterCampaignToken: String
// Analytics provider token (NSString)
@available(iOS 8.3, *)
public let SKStoreProductParameterProviderToken: String
// Advertising partner token (NSString)
@available(iOS 9.3, *)
public let SKStoreProductParameterAdvertisingPartnerToken: String
應(yīng)用內(nèi)跳轉(zhuǎn)App Store我們這里使用 SKStoreProductParameterITunesItemIdentifier
記錄到此為止!淋肾!