I am new to iphone application.I have 6 uiimages in a view.fifth image is facebook and sixth one is twiter.Under images,Iplaced two roundrectbuttons named as click5 and click6. My requirement is,when i click on those images,I have to go to login pages of facebook and twitter.how can i do this?can anyone send me sample example and explain it detai?
If you want to send the user to the website using Safari, you invoke the openURL: method in the UIApplication class, like this:
NSURL *facebookURL = [NSURL URLWithString:#"http://www.facebook.com"];
[[UIApplication sharedApplication] openURL:facebookURL];
When assigning a button to an action, you use the UIButton's addTarget:action:forControlEvents:, providing an event handler as action. Say you're setting this up in viewDidLoad, it might look like this:
- (void) viewDidLoad {
[myButton addTarget:self
action:#selector(btnClicked:)
forControlEvents:UIControlEventTouchUpInside];
}
/**
* Click/tap event handler for some buttons
*/
- (IBAction) btnClicked:(id)sender {
// Check which button was pressed.
if (sender == self.click5) {
// ...
}
else if (sender == self.click6) {
// ...
}
}
You know the very easiest way to do this? With the free 3rd party component ShareKit. It wraps the Facebook and Twitter (and several other social network) APIs to literally make it a three-line deal to post things.
I used it for the first time in the last app I built and I was SHOCKED at how easy to use it was.
Related
I have an app in which I am uploading data from a device, while the device is being read I would like to display a little loading notification or overlay. Does Cocoa have something like this built in? If I'm not mistaken I believe cocoa touch used to. Basically on a button event that starts the device reading I would also like to fire off the overlay.
The code I would add to is below:
- (IBAction)uploadEvent:(id)sender
{
// start loading overlay
char *encodedBuffer = NULL;
HEM6310FDriver *driver = [[HEM6310FDriver alloc] init];
int getDataFromDevice = [driver getData:encodedBuffer user: 1];
if (getDataFromDevice == Success) {
int status = upload(driver.deviceData);
//remove loading overlay
}
else{
// do something else and remove overlay
}
The appropriate thing to do is to add an NSProgressIndicator to your view hierarchy and display and animate when the request begins.
If it is a determinate progress, use callbacks to update.
You should also setEnabled: NO on views and controls that need the data until progress is complete.
You might also look at the new NSProgress API
Create a NSPanel. It can be as simple as you want. Maybe just a label that says "Loading" and a NSProgressView in indeterminate mode. You can then attach it to the window like this:
[[NSApplication sharedApplication] beginSheet:loadingPanel
modalForWindow:self.window
modalDelegate:self
didEndSelector:nil
contextInfo:nil];
When you want to dismiss it, do this:
[[NSApplication sharedApplication] endSheet:loadingPanel returnCode:0];
[loadingPanel orderOut:nil];
I load up a UIWebView in my app which displays html text and a sign out button.
Is it at all possible that when the user taps the html button it can then change the view? ie: it will go back in navigation of the app to the previous view?
Any examples of this being done?
Can this also be done in java for my Android version?
Edit: If I place an event using JS or something on the button can I then use a listener within the app's web view to go back a view?
What you could do is...
When one of your buttons is pressed, it actually attempts to send the user to a new URL (eg, through a href="", or javascript redirect). But, in your app's code you can use UIWebView's delegate methods to intercept the loading of the URL and instead run some app-based code.
Eg.
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *_url = [NSString stringWithFormat:#"%#", request.URL];
if ([_url rangeOfString:#"?signout"].location != NSNotFound) {
[self userRequestsLogout];
return NO;
}
return YES;
}
Don't forget that UIWebView also has;
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
Hopefully this helps.
I've just added iCloud support to an app that I am working on. Its working great, except that when I open the application without a document in focus the iCloud open file dialog appears and I don't want it to!
In my app delegate I have:
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[mainWindowController.window makeKeyAndOrderFront:self];
return NO;
}
Which I use to show my own custom window. However now, both the iCloud open file dialog and my own dialog are displayed. Any ideas on how I can get rid of the iCloud dialog?
https://developer.apple.com/library/prerelease/content/releasenotes/AppKit/RN-AppKitOlderNotes/index.html
NSDocument Support for iCloud
In 10.8, NSDocument-based applications with a ubiquity-container-identifiers entitlement gain new functionality and UI to facilitate iCloud document management.
When iCloud is enabled and an application is first launched or re-activated and no windows are visible or being restored, instead of creating a new Untitled document, NSDocumentController will display a non-modal open panel showing the user's iCloud library.
...
Applications that do not wish to use these features for any or all of their NSDocument subclasses can override +[NSDocument usesUbiquitousStorage] and return NO. If all of the application's declared NSDocument subclasses return NO from this method, then NSDocumentController will never show the new non-modal open panel.
So if you can give up using the features listed in this release note, return NO at +[NSDocument usesUbiquitousStorage].
I confirmed you can still open/save your file into iCloud storage from the normal dialog.
Putting below codes in your App Delegate lets you bypass that iCloud pop up New Document screen. Tested for High Sierra.
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Schedule "Checking whether document exists." into next UI Loop.
// Because document is not restored yet.
// So we don't know what do we have to create new one.
// Opened document can be identified here. (double click document file)
NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:#selector(openNewDocumentIfNeeded) object:nil];
[[NSOperationQueue mainQueue] addOperation: op];
}
-(void)openNewDocumentIfNeeded
{
NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];
// Open an untitled document what if there is no document. (restored, opened).
if(documentCount == 0){
[[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
}
}
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[mainWindowController.window makeKeyAndOrderFront:self];
return NO;
}
This part is correct. I've just tested it.
Just make sure your that this class is really your app delegate.
Make a new class called prefixAppDelegate
In your MainMenu.xib, drag a new object to the side and set it's custom class to the app delegate class
Right click Application and drag from Delegate down to your app delegate object.
Now just paste the code above into your app delegate class
If that still doesn't help, try logging something in applicationShouldOpenUntitledFile:.
Also, I recommend not to set [mainWindowController.window makeKeyAndOrderFront:self]; in this method. You should rather use the app delegate method applicationDidFinishLaunching: method.
My observation and fix:
[applicationShouldOpenUntitledFile:] won't be executed except you remove Key NSDocumentClass from *-info.plist. But this is harmful if your app is document based application, it won't open the document type you linked.
My fix is open my customised window directly in -(void)applicationWillFinishLaunching:(NSNotification *)notification method (Application delegate)
ETDocumentWindowController *windowController = (ETDocumentWindowController*)get your own window controller here...;
[windowController.window makeKeyAndOrderFront:nil];
I thought I would share my solution to this issue as I see others still looking for an answer. Its not a great solution but it does the trick.
Subclass NSDocumentController and add the following:
+ (void) setCanOpenUntitledDocument: (BOOL) _canOpenUntitledDocument
{
canOpenUntitledDocument = _canOpenUntitledDocument;
} // End of setCanOpenUntitledDocument:
- (void) openDocument: (id) sender
{
// With iCloud enabled, the app keeps trying to run openDocument: on first launch (before apphasfinishedlaunching gets set.
// This method lets us check and see if the app has finished launching or not. If we try to open a document before
// its finished, then don't let it.
if(!canOpenUntitledDocument)
{
return;
} // End of appHasFinishedLaunching not set
[super openDocument: sender];
} // End of openDocument:
Add the following to your app delegate:
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
// Finished launching. Let us open untitled documents.
[SQLProDocumentController setCanOpenUntitledDocument: true];
...
}
And the reasoning -- By setting a breakpoint in openDocument I've found that its called before applicationDidFinishLaunching, applicationShouldOpenUntitledFile or applicationShouldHandleReopen:hasVisibleWindows: get called, meaning adding those methods is useless. Again, it's not great code but it works and does the trick. (None of the other solutions have worked for me).
I ran into a similar problem -- it turned out that in my case, I had to remove the NSDocumentClass key and value from my Info.plist in the CFBundleDocumentTypes array. Only then would the applicationShouldOpenUntitledFile: method get called and thus allow me to prevent the iCloud/Document window from opening.
i have a button in my app a button that submit score to gamecenter and works.
this is the code:
-(void)subScore{
GKScore *scoreRepoter = [[[GKScore alloc] initWithCategory:#"123456"] autorelease];
scoreRepoter.value=100;
[scoreRepoter reportScoreWithCompletionHandler:^(NSError *error) {
if (error!=nil) {
NSLog(#"errr submitting");
}else
NSLog(#"ok!");
}];
now i'd like to submit score before app is closed with home button.
i thought to customize an action of home button (if it is possible)
or perhaps i make the same line of code in viewDidUload...or something like that...
will i be sure that that action will be performed before unloading the app?
i should make that code in dealloc method?
thanks
You can't customize behaviour of Home button directly, but iOS provides some methods in your application's delegate, by which you can control lifecycle of the application.
Method called right before the application goes to background is applicationWillResignActive: in your application's delegate (usually this method is located in AppDelegate.m file).
I think you can get needed effect by calling your method like that:
- (void)applicationWillResignActive:(UIApplication *)application {
[mygame subScore];
}
Also please note that iOS has time limit of execution for this method: you must do all saving-the-game work in less that five seconds or your application will be killed.
I'm working on an iPad app which is based on a UIWebView: to explain it in the simplest possible terms, the app shows one big interactive webview and in addition it supports custom gestures.
I need to catch events that represent single taps on the webview, but only when the taps have not already been consumed by the webview (i.e. they are not the beginning of a scroll/zoom operation, they are not taps on links, they are not taps that trigger some javascript).
The UIWebView is very greedy with its events, and in my experience it tends not to propagate them, even when they are not consumed. To catch the events, I ended up subclassing the main UIWindow (see http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/). This is working well, but the problem is I'm not able to recognize whether the taps I'm getting have triggered some javascript in the webview or not.
As an additional restriction, I have no control over the javascript that's going to run in the UIWebView or the HTML that it's going to be displayed.
So, the question goes like this: what would be a way to detect all and only the tap events which did not trigger any other action in the UIWebView itself, especially javascript actions?
~
Should you be curious, the source code of the project I'm working on is on GitHub: to find it, just ask Google to point you to Baker Framework.
Do this with JavaScript. First, after your webview finishes loading, attach a javascript click handler to the body to soak up any unused clicks. Have it load a made-up domain as it's action:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *script = #"document.body.onclick = function () { document.location.href = 'http://clickhandler'; }"
[webView stringByEvaluatingJavaScriptFromString:script];
}
Then in your webview delegate, look out for attempts to load that made up domain and intercept them. You now have a way to intercept that javascript click handler with native code and react accordingly:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.host isEqualToString:#"clickhandler"])
{
//handle click
//your logic here
return NO;
}
return YES;
}