ios game

iAd được sử dụng để hiển thị quảng cáo, được phân phát bởi máy chủ apple. iAd giúp chúng tôi kiếm doanh thu từ ứng dụng iOS.

Tích hợp iAd – Các bước đã thực hiện

Bước 1 – Tạo một ứng dụng dựa trên chế độ xem đơn giản.

Bước 2 – Chọn tệp dự án của bạn, sau đó chọn mục tiêu và sau đó thêm iAd.framework trong chọn khung.

Bước 3 – Cập nhật ViewController.h như sau:

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface ViewController : UIViewController<ADBannerViewDelegate> {
   ADBannerView *bannerView;
}
@end

Bước 4 – Cập nhật ViewController.m như sau:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   bannerView = [[ADBannerView alloc]initWithFrame:
   CGRectMake(0, 0, 320, 50)];
   
   // Optional to set background color to clear color
   [bannerView setBackgroundColor:[UIColor clearColor]];
   [self.view addSubview: bannerView];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

#pragma mark - AdViewDelegates

-(void)bannerView:(ADBannerView *)banner 
   didFailToReceiveAdWithError:(NSError *)error {
   NSLog(@"Error loading");
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
   NSLog(@"Ad loaded");
}

-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
   NSLog(@"Ad will load");
}

-(void)bannerViewActionDidFinish:(ADBannerView *)banner {
   NSLog(@"Ad did finish");
}
@end

Đầu ra

Khi chúng tôi chạy ứng dụng, chúng tôi sẽ nhận được kết quả sau:

iOS - Tích hợp iAd

iOS – GameKit

Gamekit là một khung cung cấp bảng xếp hạng, thành tích và nhiều tính năng khác cho ứng dụng iOS. Trong hướng dẫn này, chúng tôi sẽ giải thích các bước liên quan đến việc thêm bảng thành tích và cập nhật điểm số.

Các bước liên quan

Bước 1 – Trong kết nối iTunes, hãy đảm bảo rằng bạn có một ID ứng dụng duy nhất và khi chúng tôi tạo bản cập nhật ứng dụng bằng ID gói và ký mã trong Xcode với hồ sơ cấp phép tương ứng.

Bước 2 – Tạo ứng dụng mới và cập nhật thông tin ứng dụng. Bạn có thể biết thêm về điều này trong tài liệu thêm ứng dụng mới của apple.

Bước 3 – Thiết lập bảng thành tích trong Quản lý Trung tâm trò chơi của trang ứng dụng của bạn, nơi thêm một bảng thành tích và cung cấp ID bảng thành tích và Loại điểm. Ở đây chúng tôi cung cấp ID ban lãnh đạo dưới dạng hướng dẫnPoint.

Bước 4 – Các bước tiếp theo liên quan đến xử lý mã và tạo giao diện người dùng cho ứng dụng của chúng tôi.

Bước 5 – Tạo một ứng dụng xem duy nhất và nhập số nhận dạng gói là số nhận dạng được chỉ định trong kết nối iTunes .

Bước 6 – Cập nhật ViewController.xib như hình dưới đây

iOS - Tích hợp iAd

Bước 7 – Chọn tệp dự án của bạn, sau đó chọn mục tiêu và sau đó thêm GameKit.framework .

Bước 8 – Tạo IBActions cho các nút chúng ta đã thêm.

Bước 9 – Cập nhật tệp ViewController.h như sau:

#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface ViewController : UIViewController
<GKLeaderboardViewControllerDelegate>

-(IBAction)updateScore:(id)sender;
-(IBAction)showLeaderBoard:(id)sender;

@end

Bước 10 – Cập nhật ViewController.m như sau:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   if([GKLocalPlayer localPlayer].authenticated == NO) {
      [[GKLocalPlayer localPlayer] 
      authenticateWithCompletionHandler:^(NSError *error) {
         NSLog(@"Error%@",error);
      }];
   }    
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

- (void) updateScore: (int64_t) score 
   forLeaderboardID: (NSString*) category {
   GKScore *scoreObj = [[GKScore alloc]
   initWithCategory:category];
   scoreObj.value = score;
   scoreObj.context = 0;
   
   [scoreObj reportScoreWithCompletionHandler:^(NSError *error) {
      // Completion code can be added here
      UIAlertView *alert = [[UIAlertView alloc]
      initWithTitle:nil message:@"Score Updated Succesfully" 
      delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
      [alert show];
   }];
}

-(IBAction)updateScore:(id)sender {
   [self updateScore:200 forLeaderboardID:@"tutorialsPoint"];
}

-(IBAction)showLeaderBoard:(id)sender {
   GKLeaderboardViewController *leaderboardViewController =
   [[GKLeaderboardViewController alloc] init];
   leaderboardViewController.leaderboardDelegate = self;
   [self presentModalViewController:
   leaderboardViewController animated:YES];
}

#pragma mark - Gamekit delegates
- (void)leaderboardViewControllerDidFinish:
(GKLeaderboardViewController *)viewController {
   [self dismissModalViewControllerAnimated:YES];
}
@end

Đầu ra

Khi chúng tôi chạy ứng dụng, chúng tôi sẽ nhận được kết quả sau:

iOS - Tích hợp iAd

Khi chúng tôi nhấp vào “hiển thị bảng lãnh đạo”, chúng tôi sẽ nhận được một màn hình tương tự như sau:

iOS - Tích hợp iAd

Khi chúng tôi nhấp vào “cập nhật điểm”, điểm sẽ được cập nhật vào bảng xếp hạng của chúng tôi và chúng tôi sẽ nhận được thông báo như hình dưới đây

iOS - Tích hợp iAd

iOS – Bảng phân cảnh xem thêm

Trả lời