Microsoft Sync Framework Toolkit iPhone example - date format wrong? - ios

I am currently having trouble with upload in the sample - doesn't work with below error message "Invalid Date Time value received."
It seem to be complaining about how the date is formatted as string - yyyy-MM-ddTHH:mm:ss.SS is how the iPhone sample code originally formatted it to, changed it to yy-mm-ddTHH:mm:ss:SSS as said in HTML5 example inline documentation, and few other formats, but no luck.
What is the correct format?
Here're what I've done to get the sample to work for download part...
As instructed, I have changed the URI in plist file.
Looking at HTML5 example, I realized the URL for webservice was in wrong format, so changed them as below..
//self.uploadURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#/DefaultScopeSyncService.svc/?syncScope=DefaultScope&operation=UploadChanges&userid=%#", self.baseURL, [anc valueForKey:#"userID"]]];
self.uploadURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#/DefaultScopeSyncService.svc/defaultscope/UploadChanges?userid=%#", self.baseURL, [anc valueForKey:#"userID"]]];
//self.downloadURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#/DefaultScopeSyncService.svc/?syncScope=DefaultScope&operation=DownloadChanges&userid=%#", self.baseURL, [anc valueForKey:#"userID"]]];
self.downloadURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#/DefaultScopeSyncService.svc/defaultscope/DownloadChanges?userid=%#", self.baseURL, anc.userID]];

In case this helps someone else...
Found the format that works. yyyy-MM-ddTHH:mm:ss.SSSSSSS
The time format conversion seem to be done in Formatters/FormatterUtilities.cs file

Related

Need to process a request to youtube inside an iOS app

If I take a browser and type http://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en into it, I get a bunch of text describing video with ID $id in a form of a file, that gets downloaded by a browser. The file contains a large string that is unique every time the request is performed.
Now, I need to gain access to that giant string inside iOS app.
Could you please tell me where to start digging? UIWebViews? Or maybe there's a simple solution?
Thanks in advance.
NSURL *url = [NSURL URLWithString:#"https://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *idString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

WCErrorDomain 7013 when trying to send image using Watch OS 2

I´m testing Apple Watch OS 2 and I´m trying to send a image from the application to the watch. According to Apple, I shall use WCSession transferFile to do this.
Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a simple dictionary of values. For example, use this method to send images or file-based documents.
for example:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
It all looks ok in the debugger, the path is correct and the file is accessible (checked with NSFileManager) and readable.
However, everytime I try I get a callback to the didFinishFileTransfer function, including an error:
Error Domain=WCErrorDomain Code=7013 "The operation couldn’t be completed. (WCErrorDomain error 7013.)"
Looking up the error:
WCErrorCodeFileAccessDenied
An error indicating that a file could not be transferred because it was inaccessible.
Available in watchOS 2.0 and later.
It seems the file is not accessible by the send function? I have tried things like resaving the file to another directory etc, but nothing seems to work.
Anyone got an idea?
The URL you are creating is not a fileURL. Try:
NSURL *path = [NSURL fileURLWithPath:string];
I managed to solve the issue!
It was because my path did not start with file://
The following code worked just fine:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
string = [NSString stringWithFormat:#"file://%#", string];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
So it´s quite picky regarding the path.

Convert NSURL from String

I am just learning Objective-C from the couple of days and I am confused with NSURL. Here is my code
NSString *n = [NSString stringWithFormat:#"%#",#"http://somedomain.com/api/x?q={\"order_by\":[{\"field\":\"t\",\"direction\":\"desc\"}]}"];
NSURL *url = [[NSURL alloc ]initWithString:n];
NSLog(#"%#",url);
But when I trying to print an url value its null.
It seems no problem when I try to init url without JSON parameter in url.
Can I get some explanation where my problem is?
NSString *n = [NSString stringWithFormat:#"%#",#"http://somedomain.com/api/x?q={\"order_by\":[{\"field\":\"t\",\"direction\":\"desc\"}]}"];
NSURL *url = [NSURL URLWithString:[n stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",url);
The proper way to compose a URL from strings is to use NSURLComponents helper class.
The reason for this seemingly elaborate approach is that each component of a URL (see RFC 3986) requires slightly different percent encodings or possibly none.
The exact structure of the query component is not defined in RFC 3986, though. Usually, its an array of key/value pairs that will be escaped as described at w3.org: x-www-form-urlencoded-encoding-algorithm. NSURLComponents provides a method to encode the query component as well.

What is difference between URLWithString and fileURLWithPath of NSURL?

In my code I have to use URLWithString to play streaming(HLS) video and fileURLWithPath to play local video.
What is the difference between these two methods?
How should I use single method to play both videos.
Also I need to show last frame as still image when HSL video ends. Its now showing blank screen when it ends. How should i achieve this?
+URLWithString: produces an NSURL that represents the string as given. So the string might be #"http://www.google.com" and the URL represents http://www.google.com.
+fileURLWithPath: takes a path, not a URL, and produces an NSURL that represents the path using a file:// URL. So if you give it /foo/bar/baz the URL would represent file:///foo/bar/baz.
You can of course construct a file URL string manually and pass it to +URLWithString:, but +fileURLWithPath: is simpler to use when you already have a path, as you don't have to deal with escaping the string and coercing it to a URL format.
Similar thing happened in my app which use AVAudioPlayer. I tried with [NSURL URLWithString:path] and found out it fails to open certain mp3 files. I looked into error by a line like [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] fileTypeHint:AVFileTypeMPEGLayer3 error:&error], but the error was simply nil
However it was resolved by replacing the url with [NSURL fileURLWithPath:path].
In both case, the path path NSString * #"/var/mobile/Containers/Data/Application/4D96D4AE-2ED4-40B0-85D2-230E1AFA90E7/Documents/01-AudioTrack 01.mp3" 0x1457a8f0 Still I don't know the reason but now I should be careful using [NSURL URLWithString:].
PS. In NSURL Reference document, Apple said as below:
IMPORTANT
To create NSURL objects for file system paths, use fileURLWithPath:isDirectory: instead.
which clearly indicates [NSURL fileURLWithPath:] should be used for open file, though [NSURL URLWithString] also works for some cases.

UIWebView loadHtmlString not working on device

I have a webview which i want to load using the loadHtmlString method. The problem is that I want to be able to change the img src's with images that i have previously downloaded. I also use google analitics in the html so I need to set the baseUrl to the actual url so it will work. Here comes the problem. If I put the baseUrl in, the images will not load. If I don't set the baseUrl, it works. How can I get around this, so I will be able to both use google analitycs and have the images store locally in my application? I would prefer not having to implement the google analitics sdk in my project.
A strange thing is that if I run it in simulator, and not put the "http://" prefix in front of my baseUrl, it works fine. However, when I run it on a device, I receive the following error and it doesn't work:
Domain=WebKitErrorDomain Code=101 "The URL can’t be shown"
Thanks
EDIT
If I do this, it works:
[appsWebView loadHTMLString:htmlString baseURL:nil];
However, I must provide a baseURL in order to have Google Analitics working, I have two further cases:
This one gives the above mentioned error: (it works ok in simulator but gives error when running on device)
[appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:#"test.com"]];
This one simply doesn't show anything: (neither loads the html string or the url)
[appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:#"http://test.com"]];
I incorrectly assumed that the problem was that the local image was not fully specifying the full path, but that does not appear to be the problem here. But, you are quite right that it appears (somewhat surprisingly) that you cannot specify some web-based baseURL and also reference a local image in your HTML string. No simple solutions are leaping out at me, but at the very least, it appears that you might have a couple of (not very good) options:
First, you could base64 encode the local image using some base64 library like Mike Gallagher's NSData+Base64 category, e.g.:
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSString *imageDataBase64 = [imageData base64EncodedString];
NSString *imageHtml = [NSString stringWithFormat:#"<img src='data:image/png;base64,%#'>", imageDataBase64];
This slows the initial rendering, but maybe it's better than nothing.
Second, you could always try leaving the baseURL as nil, removing the JavaScript that does the Google Analytics from the HTML string, and then try injecting that JavaScript via stringByEvaluatingJavaScriptFromString. This approach may or may not work depending upon the complexity of the Google Analytics JavaScript (e.g. what further web-based references it might have), but there's a outside chance you might be able to do something that way.
My apologies for assuming the problem was a trivial img URL. Clearly you had identified a more fundamental issue.
Original answer:
Create your image URLs in your HTML string to be fully qualified file URLs within your local file system:
The image is either in Documents:
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:imageName];
Or in the bundle:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName
ofType:nil];
But, once you have fully qualified path, you should be able to use that:
NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
NSString *imageHtml = [NSString stringWithFormat:#"<img src='%#'>", imageUrl];
I would bet it's a casing issue. Take into account that the Device is case sensitive whereas the Simulator is not. Check the URL and make sure it contains the right characters.

Resources