App評分功能實現

對于iOS開發者來說,我們的App內如果需要有“關于我們”或者“給xx好評”等類似的設計,就需要實現App的評分功能。我們在這里總結兩種實現思路。如果小伙伴們有其他實現思路不妨提出來大家一起學習,一起完善該功能。

關于StoreKit

首先實現該功能,得益于蘋果的原生框架StoreKit,對于StoreKit功能官方文檔是這么說的:

  • Embed a store in your app. Process financial transactions associated with the purchase of content and services.

  • The Store Kit framework provides classes that allow an app to request payment from a user for additional functionality or content that your application delivers.

在我們的應用程序里面嵌入App Store,實現App Store的相關功能,那這樣的話對于iOS程序來說在不用打開App Store的情況下,就能進行App Store一下相關的操作,比如下載App、評分、查看App詳情等,具體的學習大家可以看文檔,看完檔感覺StoreKit功能會很強大,而且還會完善,個人感覺以后會被廣泛使用在App中。

直接打開手機的App Store并跳轉

//這里有兩個宏,一個是AppId,一個是蘋果商店地址
#define kAppId @"xxxxxx"
#define kAppleAppStoreUrlAddress [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/xxxx/id%@?l=en&mt=8",kAppId]

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:kAppleAppStoreUrlAddress]])
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kAppleAppStoreUrlAddress]];
}

這種直接跳轉至App Store是在自己的App內調起并打開了手機的App Store。

在自己的App中,模態彈出一個App Store的頁面

實現方法如下

// 在App內加載出App Store,但未直接跳轉至評論頁
- (void)evaluate {
    //初始化控制器
    SKStoreProductViewController *storeProductViewContorller = [[SKStoreProductViewController alloc] init];
    //設置代理指針
    storeProductViewContorller.delegate = self;
    //加載一個新的視圖展示
    [storeProductViewContorller loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:kAppId}      completionBlock:^(BOOL result, NSError *error){
         if(error){
             NSLog(@"error: %@ userInfo: %@",error,error.userInfo);
         }else{
             //模態彈出App Store頁面
             [self presentViewController:storeProductViewContorller animated:YES completion:^{
                 
             }];
         }
     }];
}

有個代理方法是彈出視圖后點擊“取消”按鈕的回調,在這里我們實現該代理,并在里面做了dismiss操作

#pragma mark SKStoreProductViewControllerDelegate
//取消按鈕回調代理
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    //點擊了取消按鈕,dissmiss
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

如果不在這里面做dismiss操作,返回之后返回不到原來的界面,會返回位置界面,present和dismiss一對。

SKStoreProductViewController

  • A SKStoreProductViewController object presents a store that allows the user to purchase other media from the App Store. For example, your app might display the store to allow the user to purchase another app.
    ...

直接在自己的App中評分

實現方法如下

- (void)evaluateInApp {
    if ([SKStoreReviewController respondsToSelector:@selector(requestReview)])
    {   // iOS10.3+ 直接在App內彈出評分框
        [SKStoreReviewController requestReview];
    }
    else
    {   // <iOS10.3 跳轉AppStore的評論頁面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kAppleAppStoreUrlAddress]];
    }
}

SKStoreReviewController

  • Controls the process of requesting App Store ratings and reviews from users.

  • Use the requestReview() method to indicate when it makes sense to ask the user for ratings and review within your app.

請求App Store評分和評論的類,通過調用+ (void)requestReview方法,發起評分請求。

demo地址

demo地址: https://github.com/Mexiang/evaluate

low.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容