iOS UIWebView scrollview unrecognized

先前寫過一篇「iOS Webview scroll bounces」,能夠把webView的scrollView的反彈移除,在目前持有的手機版本iOS 5.1中相當的正常,但在iOS 4.3.3中卻會出現錯誤訊息如下:
[UIWebView scrollView]: unrecognized selector sent to instance

由於無法直接access到UIWebview 的scrollView,但scrollView確實是webView的subview,解決方式如下:
NSArray *webViewSubViews = [NSArray arrayWithArray:[webView subviews]];
UIScrollView *webScroller = (UIScrollView *)[webViewSubViews objectAtIndex:0];
webScroller.bounces = NO;

將WebView的ScrollView取出,再將其進行設定即可。

相關連結
iOS Developer Library
iOS Developer Library - WebView
iOS Webview scroll bounces

iOS Location service 定位服務

取得所在位置是行動裝置的一大特色,在地化、導航、語言、文化都跟這服務脫離不了關係,在iOS中使用這服務需要先import一個framework如下
CoreLocation.framework

加入framework後還必須加入CoreLocation的Delegate,以及因為下面範例中使用者如果將定位服務關閉彈出Alert詢問使用者是否將開啟定位服務,需要加入的Delegate如下(在.h檔中)
CLLocationManagerDelegate, UIAlertViewDelegate

加入後應該會看到錯誤,因為CoreLocation這framework還沒被import,必須在.h檔上方先import這行
#import <CoreLocation/CoreLocation.h>


使用的方式如下(.m檔)
//初始化LocationService
-(void) openLocationService{
//location
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}

//給地址、經緯度後直接開啟Google map導航
-(void)navivationToTheAddress:(NSString *)address myLatitude:(double)latitude myLongitude:(double)longitude{
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?f=d&source=s_d&saddr=%f,%f&daddr=%@", latitude, longitude, address];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
}

//location Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSString *address = [self.myWebView stringByEvaluatingJavaScriptFromString:@"naviAddress"];
[self navivationToTheAddress:address myLatitude:manager.location.coordinate.latitude myLongitude:manager.location.coordinate.longitude];

[manager stopUpdatingLocation];
}

//錯誤發生時回報方式
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//NSLog(@"didFailWithError = %@", error);
NSString *errorString;
[manager stopUpdatingLocation];
NSLog(@"Error: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorDenied:
errorString = @"您已關閉定位服務無法順利導航,是否立即開啟服務?";
break;
case kCLErrorLocationUnknown:
errorString = @"無法順利取得您的位置";
break;
default:
errorString = @"無法順利取得您的位置";
break;
}

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Golf球場指南" message:errorString delegate:self cancelButtonTitle:@"收後啟用" otherButtonTitles:@"立即啟用", nil];
[alert show];
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"clickedButtonAtIndex buttonIndex = %d", buttonIndex);
if(buttonIndex == 1){
//點選"立即開啟"後打開設定中的定位服務開啟畫面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
}
}


相關連結
iOS Developer Library
iOS Developer Library - CoreLocation Framework

iOS WebView Javascript send value to native objective-c

iOS因為每次送審必須花上幾天的審核時間,所以很多需要經常變動的頁面常會使用web的方式來進行設計,但iOS因為WebView沒開放Nitro,所以速度上比起原生的差了不少,然而部分還是可以透過些小技巧來加速,畢竟如Google+ ,Facebook等為了常變更timeline也都會採用此方式。

原生的SDK就有提供Native(Objective-C)直接來呼叫Javascript的function或變數,也可以一並取得function、變數的值,方法很簡單,就下面這一行:
[webView stringByEvaluatingJavaScriptFromString:@"YOUR_FUNCTION_NAME"]

這方式相當的好用,甚至可以直接將Javascript需要另外執行的function等東西直接用這方式塞入html中,可以方變得改變webview所呈現的內容,但如果光靠這種方式還是不太夠的,畢竟這是單向的由Native(Objective-C)來監看webview的狀態,使用個timer在背景掃描也會造成系統的負載,畢竟在Mobile上能省電就省電,一個太耗效能的App沒多久就會被使用者發現,然後就被刪除,所以必須有方法來由Javascript用Event的概念直接將值傳回Native(Objective-C),原理很簡單,就是使用URL scheme的方式來傳,詳細的問題點下面會再說明,先直接來看作法:


首先Html Javascript這邊的準備如下:
sendToNative('哈哈哈哈哈哈');

function sendToNative(msg){
document.location = 'YOUR_SCHEME://myText='+encodeURIComponent(msg);
};

乍看之下不就是把當前的URL改掉,按照正常的WebView不就會直接的轉跳到其他頁面去,但是在webView的Delegate是可以避免掉這問題,方法如下

//init webview
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"YOUR_FILE_NAME" ofType:@"html" inDirectory:@"YOUR_FOLDER_NAME"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[webView setDelegate:self];
[self.view addSubview:webView];;


//webView delegate
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//取得Scheme
NSString *scheme = [[[request URL] scheme] lowercaseString];

//如果scheme與自己定義的相同
if([scheme isEqualToString:@"YOUR_SCHEME"]) {
NSString *url = [NSString stringWithFormat:@"%@", [request URL]];
NSDictionary *myDic = [self urlToDictionaryWithURL:url scheme:scheme];

return NO;
}
return YES;
}

//將所有參數轉為Dictionary
-(NSMutableDictionary *) urlToDictionaryWithURL:(NSString *)url scheme:(NSString *)scheme{
NSArray *tempArray = [[url substringFromIndex:scheme.length+3] componentsSeparatedByString:@"&"] ;
NSMutableDictionary *myDic = [NSMutableDictionary new];

for (int i = 0; i < [tempArray count]; i++) { NSString *myString = [[tempArray objectAtIndex:i] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSArray *temp = [myString componentsSeparatedByString:@"="]; [myDic setValue:[[temp objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:[temp objectAtIndex:0]]; } return myDic; }

webView的Delegate 當return為YES時會允許webview的url作他該做的動作,NO時則不會進行,但還是可以順利取得當時的url來運用,上面範例中的"urlToDictionaryWithURL"為將URL參數轉為Dictionary方便使用,URL參數格式如下,也就是GET的格式,使用"&"來做區隔。
YOUR_SCHEME://a=11111&b=22222&c=33333


但使用URL來傳值也是會有些限制,在字數、部分符號上都會造成問題,根據"LinkedIn for iPad: The Native/Web Messaging Bridge and WebSockets"這篇文章所提還是會建議使用URL方式來呼叫Native(Objective-C),再用"stringByEvaluatingJavaScriptFromString"來取得Javascript參數,可以避免URL的限制,文章中也提到使用WebSocket來進行傳送,是效率最高,但相同也會有些其他問題,最好的方式應該還是混合使用。



相關連結
LinkedIn for iPad: The Native/Web Messaging Bridge and WebSockets
iOS Developer Library
iOS Developer Library - WebView

iOS UIWebView video auto play

iOS使用WebView來作為MediaPlayer相當的方便,不但可以播放Youtube,也可以播放local的影音檔案,但在iOS使用html5的autoplay 功能卻會無法正常使用必須在Native(objective-c)這端加上這段
[webView setMediaPlaybackRequiresUserAction:NO];

而html這邊設定自動播放方式如下:
<video src="YOURFILEPATH" autoplay ></video>

光是使用video tag來播放還是會遇上些播放狀態的問題,由於autoplay會直接進行全螢幕播放,在Native SDK可以取得進入全螢幕、離開全螢幕事件,只要放在Init webview之後,使用Notification Center方式來傳送訊號
//進入全螢幕
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
//離開全螢幕
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];

而取得這些Notification的function如下
-(void)youTubeStarted:(NSNotification *)notification{
// your code here
NSLog(@"ENTER!!!");
}

-(void)youTubeFinished:(NSNotification *)notification{
// your code here
NSLog(@"EXIT!!!!!");
}


如果有需要使用javascript來控制影片的播放,如暫停、播放、播放完畢、目前播放秒數...等資訊也是相當簡單,以下為使用jQuery為例
//播放完畢
$("video").bind("ended", function() {
//end
});

//暫停
$('video').bind("pause",function(e){
//pause
});

//播放
$('video').bind("play",function(e){
//play
});

//目前播放時間(秒)
$('video').bind("timeupdate",function(e){
//timeupdate
});


其中播放完畢後預設為停在結束畫面並不會自動關閉播放模式,必須使用下面這行來進行離開全螢幕播放模式
$('video').get(0).webkitExitFullScreen();


如果要使用Javascript取得目前是否全螢幕瀏覽可使用下面方式判斷,回復為true為全螢幕,若為false為離開全螢幕
$('video').get(0).webkitDisplayingFullscreen;


相關連結
Opera Dev - Introduction to HTML5 video
iOS Developer Library
iOS Developer Library - WebView

iOS Webview scroll bounces

iOS使用Webview在上下滑到底的同時通常還可多滑出一段,也就是與UIScrollView一樣的反彈,來告知使用者已經滑到底,我覺得是個相當好的回饋,但如果自己已經有做好另外的回饋,也是可以將這個效果關閉,只需要在UIWebView加上下面這行就會關閉。

webView.scrollView.bounces = NO;

相關連結
iOS Developer Library
iOS Developer Library - WebView
iOS UIWebView scrollview unrecognized