webview js交互

#define AgreementName @"hybrid://nativechannel/"

#pragma mark - webview delegate

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

{

NSString*requestUrl=[selfURLDecodedString:[requestURL].absoluteString];

//获取分享json

if([requestUrlhasPrefix:AgreementName])

{

NSString*urlData=[requestUrlsubstringFromIndex:AgreementName.length];

@try{

idjsonData=[urlDataobjectFromJSONString];  //此方法为jsonKit自带的方法

if([jsonDataisKindOfClass:[NSDictionaryclass]])

{

NSDictionary* dic = (NSDictionary*)jsonData;

NSLog(@"json dic == %@",dic);

}

}

@catch(NSException *exception) {

}

@finally{

}

returnNO;

}

returnYES;

}

-(NSString*)URLDecodedString:(NSString*)str

{

NSString*decodedString=(__bridge_transferNSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridgeCFStringRef)str,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

returndecodedString;

}

你可能感兴趣的:(webview js交互)