响应UIWebView的点击事件(播放声音),从数据库中取出声音保存在本地。
在iPhone开发中经常会遇到需要在UIWebView中点击按钮并进行相应的操作,比如点击按钮播放声音等,以下是代码,并从数据库中取出声音数据,以文件的形式保存在本地。- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {...AppDelegate *appDelegate=(...AppDelegate *)[ delegate];if (navigationType == UIWebViewNavigationTypeLinkClicked) {NSURL *URL = ;NSLog();NSLog();//.......code here and call the delegate methodNSAutoreleasePool *pool=[ init];BOOL audioResult=FALSE;NSFileManager *fileManager = ;//check if file existed in cache folderNSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachePath = ;NSString *filepath ;filepath=];if (==YES){;}else{audioResult=;if (audioResult==YES){//NSLog(@"Speak the audio by the path=%@",filepath);;}};;}
保存声音的方法:
-(BOOL)saveAudio:(NSString *)key{NSLog(key);BOOL saveSuccess=FALSE;NSString *dbPath = [[ resourcePath] stringByAppendingPathComponent:@"testVoice.db"];if(sqlite3_open(, &database_voice) == SQLITE_OK){NSString *sqlString=;NSLog(sqlString);const char *sql_char=;//prepare the select statementint returnValue = sqlite3_prepare_v2(database_voice, sql_char, -1, &selectStatement, NULL);if(returnValue == SQLITE_OK){//loop all the rows returned by the query.if(sqlite3_step(selectStatement) == SQLITE_ROW){NSFileManager *fileManager = ;NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachePath = ;NSString *filename=;////NSLog(filename);NSData *fileData=;//save the file data//check NSString *filepath = ;filepath=;if (==NO){////NSLog(@"fileManager createDirectoryAtPath:cachePath attributes:nil");}if (){saveSuccess=TRUE;//NSLog(@"saveSuccess");////NSLog(@"returnValue");}else{//NSLog(@"save Fail");}}sqlite3_finalize(selectStatement);}}return saveSuccess;}
页:
[1]