MWPhotoBrowser code update with directory - xcode

I am using the github project called MWPhotoBrowser that pulls image files from the web into a gallery on the iPhone. The problem is the code calls for the specific url of the image file. I want to pull images from inside a directory. Any ideas on how this can be done? Here's part of the code.
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:#"http://farm4.static.flickr.com/3567/3523321514_371d9ac42f_b.jpg"]]];

This solution involves use of the NSURL API is it can be used for web or local resources:
NSString *filePath = //path to your file
NSURL *filePathURL = [[NSURL alloc] initFileURLWithPath:filePath];
[photos addObject:[MWPhoto photoWithURL:filePathURL]];

Related

iOS8: Sharing common files between apps

Two iOS: AppA and AppB
Both Apps are created by me and I would like to share one single file between both of the apps.
i.e. AppA launches on deviceA and the User saves data on fileA. Later the User launches AppB on the same (deviceA) and also saves data on fileA. Both apps are saving data on the same file.
I'm aware that I can use NSUserDefaults and share Keychain between apps, but that's not what I'm looking for.
I read up on document extensions provider and app groups, but I'm confused if I can use these for this scenario? Or is there any other way to accomplish this?
You can do it using Application Group Container Directory:
NSFileManager *fm = [NSFileManager defaultManager];
NSString *appGroupName = #"Z123456789.com.example.app-group"; /* For example */
NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];
NSError* theError = nil;
if (![fm createDirectoryAtURL: groupContainerURL withIntermediateDirectories:YES attributes:nil error:&theError]) {
// Handle the error.
}
You could just upload the files after saving to your server and make both apps request updates for the file whenever they are launched.
Hope that helps :)

how to make NSURL point to local dir?

reading Adium code today, found an interesting usage of NSURL:
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:#"adium://%#/adium", [messageStyle.bundle bundleIdentifier]]];
[[webView mainFrame] loadHTMLString:[messageStyle baseTemplateForChat:chat] baseURL:baseURL];
I tried to log the url and got this adium://im.adium.Smooth Operator.style/adium, Then I created a blank project to see how to create such an NSURL but failed. When I sending loadHTMLString message to a webview's frame in my project, if the baseURL is nil, everything is fine, if not, I got a blank page in the view.
here is my code, the project name is webkit
NSURL *baseURL = [NSURL URLWithString:#"webkit://resource"];
//if baseURL is nil or [[NSBundle mainBundle] bundleURL], everything is fine
[[webView mainFrame] loadHTMLString:#"<html><head></head><body><div>helloworld</div></body></html>"
baseURL: baseURL];
[frameView setDocumentView:webView];
[[frameView documentView] setFrame:[frameView visibleRect]];
the question is how to make a self defined protocol instead of http://?
adium://%#/adium , first section is called protocol you can also register your protocol webkit: Take a look at How to map a custom protocol to an application on the Mac? and Launch Scripts from Webpage Links
[NSURLProtocol registerClass:[AIAdiumURLProtocol class]];
[ESWebView registerURLSchemeAsLocal:#"adium"];
I tried to find where did adium define the adium schema in the info.plist, unfortunately, there's nothing there, only some irc/xmpp protocols.
so I finally launched the debugger, and found the code above in the AIWebKitDelegate init method, anyway this is another way to register a self defined protocol~

Prevent IOS WebView from caching images

Situation:
A native IOS5 App downloads a website from FTP to local storage and a webview displays the downloaded page. If there is a new version on the FTP-Server, the App will automaticly download the new version and override the old one in the local storage. Now the webview should load the new version.
Problem:
When the new version overwrides the old version, the name of some images will be the same. Now the webview will load every image from cache insteat from filesystem. -.-
Question:
Is there a possibility to prevent the webview loading all images from cache or clear the whole cache? What I cant do is changing the html or js of the downloaded page. So the approache to add a timestamp behinde every image (test.img?timestamp=23444) cant be done. The change has to be done in the IOS App.
What ive tryed already but failed:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
NSURL *url = [NSURL fileURLWithPath:[ftpPath stringByAppendingPathComponent:#"index.html"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:1.0];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest: urlRequest];
[webView stopLoading];
[webView loadRequest:urlRequest];
[webView reload];
webView.delegate = self;
Thank you so much for any help, i dont get it.
Cya Nando

Downloading an mp3 from link inside app

i'm making an mac app that downloads an mp3 from a link.
For example, this link: http://media.soundcloud.com/stream/VGGUdzU69Ng5?stream_token=2U9W2
As you can see, it is an mp3 file.
How can i download it to a specific path?
Thank you
The simplest way is to use NSURLDownload:
NSURL* url = [NSURL URLWithString:#"http://media.soundcloud.com/stream/VGGUdzU69Ng5?stream_token=2U9W2"];
NSString* destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:#"someFile.mp3"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLDownload* download = [[NSURLDownload alloc] initWithRequest:request delegate:nil];
[download setDestination:destinationPath allowOverwrite:NO];
Ideally you'd set an object as the delegate so you can receive progress notifications and then release the NSURLDownload object when finished.
Probably the simplest way would be to set a policy delegate for your web view, and have that delegate respond to the question of what to do with that link by telling the listener to download.
Edit: Oh, missed the part about “to a specific path”. I've no idea on that; sorry. I hope someone else can fill in that aspect.

How to set up initial Core Data store in a Mac app?

I am an experienced iOS developer trying to make my first Mac app. I want to use Core data to store the data in my app. In my iOS apps, I generally have a pre-created SQLite file which is used as the initial state of the data store, and which is moved into place on the first time the app is run, like this:
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"Datafile.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:#"Datafile-DefaultData" ofType:#"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
I want to do something similar in the mac app, except put the data in the ~/Library/Application Support/MyApp directory. I can't seem to figure out how to do it. Any pointers?
In Xcode 4, the default Core Data project template, for some reason, uses the root ~/Library directory (ie ~/Library/Application to store your Core Data file. You should change this anyways (because it's a bad idea), but once you do that, it should work as you expect. I believe the default name on the Mac is storedata, and you should also note that you'll need to change the store type from XML, which is the default.

Resources