ขั้นตอนที่อธิบายไว้ในหน้านี้อธิบายวิธีสร้างแอปพลิเคชัน iOS แบบง่ายที่ส่งคําขอไปยัง YouTube Data API ได้อย่างรวดเร็ว ตัวอย่างนี้แสดงวิธีเรียกข้อมูลเกี่ยวกับช่อง YouTube ของ GoogleDeveloper นอกจากนี้ โค้ดยังมีความคิดเห็นที่อธิบายวิธีการแก้ไขคําค้นหาเพื่อดึงข้อมูลเกี่ยวกับช่อง YouTube ของผู้ใช้ปัจจุบันด้วย
สิ่งที่ต้องดำเนินการก่อน
หากต้องการเรียกใช้คู่มือเริ่มต้นฉบับย่อนี้ คุณจะต้องมีสิ่งต่อไปนี้
- Xcode 8.0 ขึ้นไป
- ผู้จัดการทรัพยากร Dependency CocoaPods
- การเข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์
- บัญชี Google
ขั้นตอนที่ 1: เปิดใช้ YouTube Data API
-
ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console แล้วเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลเข้าสู่ระบบ
-
คลิกปุ่มยกเลิกในหน้าสร้างข้อมูลเข้าสู่ระบบ
-
เลือกแท็บหน้าจอขอความยินยอม OAuth ที่ด้านบนของหน้า เลือกอีเมล ป้อนชื่อผลิตภัณฑ์ หากยังไม่ได้ตั้งค่าไว้ แล้วคลิกปุ่มบันทึก
-
เลือกแท็บข้อมูลเข้าสู่ระบบ คลิกปุ่มสร้างข้อมูลเข้าสู่ระบบ แล้วเลือกรหัสไคลเอ็นต์ OAuth
- เลือกประเภทแอปพลิเคชัน iOS ป้อนชื่อ "YouTube Data API"คู่มือเริ่มใช้งานฉบับย่อ, รหัสชุด
com.example.QuickstartApp
แล้วคลิกปุ่มสร้าง
ขั้นตอนที่ 2: เตรียมพื้นที่ทํางาน
- เปิด Xcode และสร้างโปรเจ็กต์ใหม่
- คลิกไฟล์ > ใหม่ > โปรเจ็กต์ เลือกเทมเพลต iOS > แอปพลิเคชัน > แอปพลิเคชันมุมมองเดียว แล้วคลิกถัดไป
- ตั้งชื่อผลิตภัณฑ์เป็น "QuickstartApp" ตัวระบุองค์กรเป็น "com.example" และภาษาเป็น
Objective-C
คุณควรระบุรหัสชุดที่สร้างขึ้นซึ่งตรงกับรหัสชุด iOS (
com.example.QuickstartApp
) ที่คุณป้อนในขั้นตอนที่ 1.ข ด้านล่างรหัสองค์กร - คลิกถัดไป
- เลือกไดเรกทอรีปลายทางสําหรับโปรเจ็กต์ แล้วคลิกสร้าง
- ปิดโครงการโดยคลิกไฟล์ > ปิดโครงการ
- เปิดหน้าต่างเทอร์มินัลและไปยังไดเรกทอรีที่มีไฟล์
QuickstartApp.xcodeproj
ที่คุณเพิ่งสร้าง เรียกใช้คําสั่งต่อไปนี้เพื่อสร้าง Podfile, ติดตั้งไลบรารีแล้วเปิดโปรเจ็กต์ XCode ที่ได้
cat << EOF > Podfile && platform :ios, '8.0' target 'QuickstartApp' do pod 'GoogleAPIClientForREST/YouTube', '~> 1.2.1' pod 'Google/SignIn', '~> 3.0.3' end EOF pod install && open QuickstartApp.xcworkspace
ใน XCode Project Navigator ให้เลือกโหนดของโปรเจ็กต์ "QuickstartApp" จากนั้นคลิกรายการในเมนู File > Add files to "QuickstartApp"
ค้นหาไฟล์
GoogleService-Info.plist
ที่ดาวน์โหลดไว้ก่อนหน้านี้ แล้วเลือกไฟล์ คลิกปุ่มตัวเลือกเลือกตัวเลือกต่อไปนี้ในหน้าต่างตัวเลือก แล้วคลิกปุ่มเพิ่ม
- เลือกช่องทําเครื่องหมายคัดลอกรายการหากจําเป็น
- ตรวจสอบเป้าหมายทั้งหมดที่ระบุในส่วนเพิ่มลงในเป้าหมาย
เมื่อเลือกโหนดโปรเจ็กต์อยู่ ให้เลือก "QuickstartApp" ในส่วนเป้าหมายดังที่แสดงในรูปภาพ 2 รูปด้านล่าง
คลิกพื้นที่ที่แสดงในภาพหน้าจอนี้
จากนั้นเลือกเป้าหมายที่เหมาะสมดังนี้
เลือกแท็บข้อมูล และขยายส่วนประเภท URL
คลิกปุ่ม + และเพิ่มรูปแบบ URL สําหรับรหัสไคลเอ็นต์แบบย้อนกลับ หากต้องการดูค่านี้ ให้เปิดไฟล์การกําหนดค่า
GoogleService-Info.plist
ที่คุณเลือกไว้ในขั้นตอนที่ 2.f มองหาคีย์ REVERSED_CLIENT_ID คัดลอกค่าของคีย์นั้นและวางลงในช่องรูปแบบ URL ในหน้าการกําหนดค่า เว้นช่องอื่นๆ ว่างไว้สร้างโปรเจ็กต์อีกครั้ง โดยทําดังนี้
- คลิกผลิตภัณฑ์ > ล้างโฟลเดอร์บิลด์ (ขณะกดปุ่มตัวเลือกค้างไว้)
- คลิกผลิตภัณฑ์ > สร้าง
ขั้นตอนที่ 3: ตั้งค่าตัวอย่าง
แทนที่เนื้อหาของไฟล์ต่อไปนี้ด้วยโค้ดที่ระบุ#import <UIKit/UIKit.h>
@import GoogleSignIn;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize Google sign-in.
[GIDSignIn sharedInstance].clientID = @"<YOUR_CLIENT_ID>";
return YES;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
@end
#import <UIKit/UIKit.h>
@import GoogleSignIn;
#import <GTLRYouTube.h>
@interface ViewController : UIViewController <GIDSignInDelegate, GIDSignInUIDelegate>
@property (nonatomic, strong) IBOutlet GIDSignInButton *signInButton;
@property (nonatomic, strong) UITextView *output;
@property (nonatomic, strong) GTLRYouTubeService *service;
@end
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Configure Google Sign-in.
GIDSignIn* signIn = [GIDSignIn sharedInstance];
signIn.delegate = self;
signIn.uiDelegate = self;
signIn.scopes = [NSArray arrayWithObjects:kGTLRAuthScopeYouTubeReadonly, nil];
[signIn signInSilently];
// Add the sign-in button.
self.signInButton = [[GIDSignInButton alloc] init];
[self.view addSubview:self.signInButton];
// Create a UITextView to display output.
self.output = [[UITextView alloc] initWithFrame:self.view.bounds];
self.output.editable = false;
self.output.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
self.output.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.output.hidden = true;
[self.view addSubview:self.output];
// Initialize the service object.
self.service = [[GTLRYouTubeService alloc] init];
}
- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (error != nil) {
[self showAlert:@"Authentication Error" message:error.localizedDescription];
self.service.authorizer = nil;
} else {
self.signInButton.hidden = true;
self.output.hidden = false;
self.service.authorizer = user.authentication.fetcherAuthorizer;
[self fetchChannelResource];
}
}
// Construct a query and retrieve the channel resource for the GoogleDevelopers
// YouTube channel. Display the channel title, description, and view count.
- (void)fetchChannelResource {
GTLRYouTubeQuery_ChannelsList *query =
[GTLRYouTubeQuery_ChannelsList queryWithPart:@"snippet,statistics"];
query.identifier = @"UC_x5XG1OV2P6uZZ5FSM9Ttw";
// To retrieve data for the current user's channel, comment out the previous
// line (query.identifier ...) and uncomment the next line (query.mine ...).
// query.mine = true;
[self.service executeQuery:query
delegate:self
didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}
// Process the response and display output
- (void)displayResultWithTicket:(GTLRServiceTicket *)ticket
finishedWithObject:(GTLRYouTube_ChannelListResponse *)channels
error:(NSError *)error {
if (error == nil) {
NSMutableString *output = [[NSMutableString alloc] init];
if (channels.items.count > 0) {
[output appendString:@"Channel information:\n"];
for (GTLRYouTube_Channel *channel in channels) {
NSString *title = channel.snippet.title;
NSString *description = channel.snippet.description;
NSNumber *viewCount = channel.statistics.viewCount;
[output appendFormat:@"Title: %@\nDescription: %@\nViewCount: %@\n", title, description, viewCount];
}
} else {
[output appendString:@"Channel not found."];
}
self.output.text = output;
} else {
[self showAlert:@"Error" message:error.localizedDescription];
}
}
// Helper for showing an alert
- (void)showAlert:(NSString *)title message:(NSString *)message {
UIAlertController *alert =
[UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =
[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
@end
ขั้นตอนที่ 4: เรียกใช้ตัวอย่าง
เปลี่ยนไปใช้รูปแบบ QuickstartApp โดยคลิกผลิตภัณฑ์ > รูปแบบ > QuickstartApp และเรียกใช้ตัวอย่าง (Cmd+R) โดยใช้เครื่องจําลองอุปกรณ์หรืออุปกรณ์ที่กําหนดค่า ครั้งแรกที่คุณเรียกใช้ตัวอย่าง จะมีข้อความแจ้งให้คุณเข้าสู่ระบบบัญชี Google และให้สิทธิ์เข้าถึง
หมายเหตุ
- ระบบจะจัดเก็บข้อมูลการให้สิทธิ์ไว้ใน Keychain ดังนั้นการเรียกใช้ครั้งต่อๆ ไปจะไม่ได้รับแจ้งให้ให้สิทธิ์