在我的iOS 5应用程序中,我有一个NSString包含JSON字符串的应用程序.我想将JSON字符串表示反序列化为本机NSDictionary对象.
"{\"password\" : \"1234\", \"user\" : \"andreas\"}"
Run Code Online (Sandbox Code Playgroud)
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:@"{\"2\":\"3\"}"
options:NSJSONReadingMutableContainers
error:&e];
Run Code Online (Sandbox Code Playgroud)
-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c'
Run Code Online (Sandbox Code Playgroud) 我有一个WebView,我想从JavaScript调用Objective-C中的一个视图.有人知道我怎么做吗?
我的ViewController中有这个代码:
- (BOOL)webView:(UIWebView *)webView2
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 &&
[(NSString *)[components objectAtIndex:0] isEqualToString:@"myapp"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"myfunction"])
{
NSLog([components objectAtIndex:2]); [[Airship shared] displayStoreFront]; //<- This is the code to open the Store
NSLog([components objectAtIndex:3]); // param2
// Call your method in Objective-C method using the above...
}
return NO;
}
return YES; // Return YES to make sure regular navigation works as …Run Code Online (Sandbox Code Playgroud) 我试图让UIGestureRecognizer使用UIWebview,它是UIScrollView的子视图.这听起来很奇怪但是当我将numberOfTouchesRequired设置设置为2时,选择器会触发,但是当numberOfTouchesRequired设置为1时,选择器不会触发.
这是我的代码:
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(select1:)];
tap1.numberOfTouchesRequired = 2;
tap1.numberOfTapsRequired = 1;
tap1.delegate = self;
[self.ans1WebView addGestureRecognizer:tap1];
[tap1 release];
- (void) select1:(UILongPressGestureRecognizer *)sender {
//Do Stuff
}
Run Code Online (Sandbox Code Playgroud)
我已经通过使用Apple样本用于UIGestureRecognizer并在其笔尖中插入webview来证实了这一点.他们的tap代码可以在webview区域内的任何地方使用.
我的iOS应用程序中有.html文件.HTML文件使用onClick方法有几个div块.当我点击这些块时,我在Web视图中调用了一些javascript代码,但我还需要在源代码中了解这些事件.
例如,当我点击web元素并调用onClick时,我需要调用代码中的某些方法,例如 - (void)didTouchedWebElementWithId
我可以做这个吗 谢谢.
我正在尝试加载一个公交网站,这样我就可以为一个停止时间刮掉停止时间.加载url后,稍后会通过javascript动态加载停止时间.我的目标是通过一类"停止时间"来检测元素的存在.如果html中存在这些元素,我可以解析html.但在我解析html之前,我必须等待这些元素出现"停止时间".我读了很多其他的SO问题,但我不能把它拼凑起来.我正在实现didReceive消息函数,但我不确定如何加载javascript我需要检测元素的存在(具有"停止时间"类的元素).我成功注入了一些javascript以防止显示位置权限弹出窗口.
override func viewDidLoad() {
super.viewDidLoad()
let contentController = WKUserContentController()
let scriptSource = "navigator.geolocation.getCurrentPosition = function(success, error, options) {}; navigator.geolocation.watchPosition = function(success, error, options) {}; navigator.geolocation.clearWatch = function(id) {};"
let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentStart, forMainFrameOnly: true)
contentController.addUserScript(script)
let config = WKWebViewConfiguration()
config.userContentController = contentController
webView = WKWebView(frame: .zero, configuration: config)
self.view = self.webView!
loadStopTimes("https://www.website.com/stop/1000")
}
func loadStopTimes(_ busUrl: String) {
let urlString = busUrl
let url = URL(string: urlString)!
let urlRequest = URLRequest(url: url)
webView?.load(urlRequest)
}
func userContentController(_ …Run Code Online (Sandbox Code Playgroud) ios ×3
iphone ×2
javascript ×2
uiwebview ×2
cocoa ×1
cocoa-touch ×1
ios5 ×1
ipad ×1
json ×1
nsdictionary ×1
nsstring ×1
objective-c ×1
swift ×1
wkwebview ×1