相关疑难解决方法(0)

如何将JSON字符串反序列化为NSDictionary?(适用于iOS 5+)

在我的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)

json nsdictionary nsstring ios ios5

151
推荐指数
4
解决办法
15万
查看次数

如何从Javascript调用Objective-C?

我有一个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)

iphone cocoa-touch uiwebview

41
推荐指数
3
解决办法
5万
查看次数

UIGestureRecognizer是否可以在UIWebView上运行?

我试图让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区域内的任何地方使用.

iphone cocoa objective-c ipad uigesturerecognizer

40
推荐指数
4
解决办法
4万
查看次数

使用UIWebView从HTML代码调用目标c代码中的方法

我的iOS应用程序中有.html文件.HTML文件使用onClick方法有几个div块.当我点击这些块时,我在Web视图中调用了一些javascript代码,但我还需要在源代码中了解这些事件.

例如,当我点击web元素并调用onClick时,我需要调用代码中的某些方法,例如 - (void)didTouchedWebElementWithId

我可以做这个吗 谢谢.

javascript uiwebview ios

21
推荐指数
1
解决办法
4万
查看次数

在WKWebView中加载网站后,如何检测元素何时可见?

我正在尝试加载一个公交网站,这样我就可以为一个停止时间刮掉停止时间.加载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)

javascript ios swift wkwebview

9
推荐指数
1
解决办法
519
查看次数