顯示具有 PhoneGap 標籤的文章。 顯示所有文章
顯示具有 PhoneGap 標籤的文章。 顯示所有文章

2014年10月20日

升級 iOS8 PhoneGap GeoLocation 無法開啟GPS問題。


今天遇到,我的iPhone升級為iOS8以後,GPS並無法正常被開啟。在網路上找到一個方法,分享給大家試試:

使用PhoneGap版本:2.9.0。

參考網址:

修改檔案路徑:
CordovaLib.xcodeproj/Classes/Commands/CDVLocation.m



請在CDVLocation.m加入以下「紅色」部份的Code
- (BOOL)isAuthorized
{
    BOOL authorizationStatusClassPropertyAvailable = [CLLocationManager respondsToSelector:@selector(authorizationStatus)]; // iOS 4.2+

    if (authorizationStatusClassPropertyAvailable) {
        NSUInteger authStatus = [CLLocationManager authorizationStatus];

#ifdef __IPHONE_8_0
        if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {  //iOS 8.0+
            return (authStatus == kCLAuthorizationStatusAuthorizedWhenInUse) || (authStatus == kCLAuthorizationStatusAuthorizedAlways) || (authStatus == kCLAuthorizationStatusNotDetermined);
        }
#endif

        return (authStatus == kCLAuthorizationStatusAuthorized) || (authStatus == kCLAuthorizationStatusNotDetermined);
    }

    // by default, assume YES (for iOS < 4.2)
    return YES;
}

#ifdef __IPHONE_8_0
    NSUInteger code = [CLLocationManager authorizationStatus];
    if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) { //iOS8+
        __highAccuracyEnabled = enableHighAccuracy;
        if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
            [self.locationManager requestAlwaysAuthorization];
        } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
            [self.locationManager  requestWhenInUseAuthorization];
        } else {
            NSLog(@"[Warning] No NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key is defined in the Info.plist file.");
        }
        return;
    }
#endif


    // Tell the location manager to start notifying us of location updates. We
    // first stop, and then start the updating to ensure we get at least one
    // update, even if our location did not change.
    [self.locationManager stopUpdatingLocation];
    [self.locationManager startUpdatingLocation];


//iOS8+
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if(!__locationStarted){
        [self startLocation:__highAccuracyEnabled];
    }
}


在檔案找,找到 檔案名稱-Info.plist 檔,請用「文件編輯器」打開。
加入以下Code

<key>NSLocationAlwaysUsageDescription</key>
<String>Permissions Message for App even when not in use</String>
<key>NSLocationWhenInUseUsageDescription</key>
<String>Permissions Message for App when in use</String>

如下圖:


2014年9月18日

Android PhoneGap 使用 Cookies 時

Android PhoneGap 使用 Cookies 時,需設定 CookieManager 方式及放置的位子:
import android.os.Bundle;
import android.webkit.CookieManager;
import org.apache.cordova.*;

public class App extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    CookieManager.setAcceptFileSchemeCookies(true);
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
}
}

2014年9月3日

PhoneGap Android 在Javascript使用Cookie無法正常進行存取!

    

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //Android Cookie無法正使進行存取,請於Native 加入以下兩行程式碼↓
        CookieManager.setAcceptFileSchemeCookies(true); //available in android level 12
        CookieManager.getInstance().setAcceptCookie(true); //available in android level 12
        //Android Cookie無法正使進行存取,請於Native 加入以上兩行程式碼↑
        super.loadUrl("file:///android_asset/www/index.html");
    }