@@ -62,7 +62,7 @@ private extension NSURL {
6262}
6363
6464public func videoIDFromYouTubeURL( videoURL: NSURL ) -> String ? {
65- return videoURL. queryStringComponents ( ) [ " v " ] as String ?
65+ return videoURL. queryStringComponents ( ) [ " v " ] as? String
6666}
6767
6868/** Embed and control YouTube videos */
@@ -87,15 +87,13 @@ public class YouTubePlayerView: UIView, UIWebViewDelegate {
8787 /** Used to respond to player events */
8888 public var delegate : YouTubePlayerDelegate ?
8989
90- // MARK: Various methods for initialization
9190
92- override public init ( ) {
93- super. init ( )
94- buildWebView ( playerParameters ( ) )
95- }
91+ // MARK: Various methods for initialization
9692
9793 override public init ( frame: CGRect ) {
9894 super. init ( frame: frame)
95+
96+ printLog ( " initframe " )
9997 buildWebView ( playerParameters ( ) )
10098 }
10199
@@ -217,10 +215,10 @@ public class YouTubePlayerView: UIView, UIWebViewDelegate {
217215
218216 // Check for error
219217 if let error = error {
220- println ( " Lookup error: no HTML file found for path, \( path) " )
218+ printLog ( " Lookup error: no HTML file found for path, \( path) " )
221219 }
222220
223- return htmlString!
221+ return htmlString! as String
224222 }
225223
226224
@@ -255,12 +253,12 @@ public class YouTubePlayerView: UIView, UIWebViewDelegate {
255253
256254 // Check for error and return nil
257255 if let error = error {
258- println ( " Error parsing JSON " )
256+ printLog ( " Error parsing JSON " )
259257 return nil
260258 }
261259
262260 // Success, return JSON string
263- return NSString ( data: jsonData!, encoding: NSUTF8StringEncoding)
261+ return NSString ( data: jsonData!, encoding: NSUTF8StringEncoding) as? String
264262 }
265263
266264
@@ -271,38 +269,37 @@ public class YouTubePlayerView: UIView, UIWebViewDelegate {
271269 // Grab the last component of the queryString as string
272270 let data : String ? = eventURL. queryStringComponents ( ) [ " data " ] as? String
273271
274- if let host = eventURL. host {
275- if let event = YouTubePlayerEvents ( rawValue : host ) {
276- // Check event type and handle accordingly
277- switch event {
278- case . YouTubeIframeAPIReady:
279- ready = true
280- break
272+ if let host = eventURL. host, let event = YouTubePlayerEvents ( rawValue : host ) {
273+
274+ // Check event type and handle accordingly
275+ switch event {
276+ case . YouTubeIframeAPIReady:
277+ ready = true
278+ break
281279
282- case . Ready:
283- delegate? . playerReady ( self )
280+ case . Ready:
281+ delegate? . playerReady ( self )
284282
285- break
283+ break
286284
287- case . StateChange:
288- if let newState = YouTubePlayerState ( rawValue: data!) {
289- playerState = newState
290- delegate? . playerStateChanged ( self , playerState: newState)
291- }
285+ case . StateChange:
286+ if let newState = YouTubePlayerState ( rawValue: data!) {
287+ playerState = newState
288+ delegate? . playerStateChanged ( self , playerState: newState)
289+ }
292290
293- break
291+ break
294292
295- case . PlaybackQualityChange:
296- if let newQuality = YouTubePlaybackQuality ( rawValue: data!) {
297- playbackQuality = newQuality
298- delegate? . playerQualityChanged ( self , playbackQuality: newQuality)
299- }
293+ case . PlaybackQualityChange:
294+ if let newQuality = YouTubePlaybackQuality ( rawValue: data!) {
295+ playbackQuality = newQuality
296+ delegate? . playerQualityChanged ( self , playbackQuality: newQuality)
297+ }
300298
301- break
299+ break
302300
303- default :
304- break
305- }
301+ default :
302+ break
306303 }
307304 }
308305 }
@@ -315,8 +312,12 @@ public class YouTubePlayerView: UIView, UIWebViewDelegate {
315312 let url = request. URL
316313
317314 // Check if ytplayer event and, if so, pass to handleJSEvent
318- if url. scheme == " ytplayer " { handleJSEvent ( url) }
315+ if let url = url where url. scheme == " ytplayer " { handleJSEvent ( url) }
319316
320317 return true
321318 }
322319}
320+
321+ private func printLog( str: String ) {
322+ println ( " [YouTubePlayer] \( str) " )
323+ }
0 commit comments